From 4d4f1d6068ec5ce100e17ff6694805708223f5f4 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 24 Oct 2019 14:03:33 +0200 Subject: [PATCH] Avoid null vectors when making colors valid If the picked color and a forbidden color are identical (like if they both picked the same palette color and in the special case when a picked color is outside of the allowed range and the method returns the picked color as the forbidden color), the vector between them is zero and the maths for adjusting the color fails by hitting the iteration limit. This changes the zero vector to the smallest possible vector in order to avoid the issue. This can result in some seriously close adjustments in the case of picking identical palette colors, which might be undesirable compared to picking a new palette color. --- OpenRA.Mods.Common/ColorValidator.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/OpenRA.Mods.Common/ColorValidator.cs b/OpenRA.Mods.Common/ColorValidator.cs index 2eb8ff3a07..869019537c 100644 --- a/OpenRA.Mods.Common/ColorValidator.cs +++ b/OpenRA.Mods.Common/ColorValidator.cs @@ -137,7 +137,14 @@ namespace OpenRA.Mods.Common // Reduce vector by it's biggest value (more calculations, but more accuracy too) var vectorMax = vector.Max(vv => Math.Abs(vv)); if (vectorMax == 0) - vectorMax = 1; // Avoid division by 0 + { + vectorMax = 1; // Avoid division by 0 + + // Create a tiny vector to make the while loop maths work + vector[0] = 1; + vector[1] = 1; + vector[2] = 1; + } vector[0] /= vectorMax; vector[1] /= vectorMax;