Define additional constraints in HSV space.

This commit is contained in:
Paul Chote
2016-01-26 23:04:08 +00:00
parent 617295cc91
commit 7638746069

View File

@@ -21,6 +21,8 @@ namespace OpenRA.Mods.Common
{ {
// The bigger the color threshold, the less permissive is the algorithm // The bigger the color threshold, the less permissive is the algorithm
public readonly int Threshold = 0x50; public readonly int Threshold = 0x50;
public readonly float[] HsvSaturationRange = new[] { 0.25f, 1f };
public readonly float[] HsvValueRange = new[] { 0.2f, 1.0f };
double GetColorDelta(Color colorA, Color colorB) double GetColorDelta(Color colorA, Color colorB)
{ {
@@ -51,9 +53,18 @@ namespace OpenRA.Mods.Common
return true; return true;
} }
public bool IsValid(Color askedColor, out Color forbiddenColor, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors, Action<string> onError) public bool IsValid(Color askedColor, out Color forbiddenColor, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors, Action<string> onError)
{ {
// Validate color against HSV
float h, s, v;
new HSLColor(askedColor).ToHSV(out h, out s, out v);
if (s < HsvSaturationRange[0] || s > HsvSaturationRange[1] || v < HsvValueRange[0] || v > HsvValueRange[1])
{
onError("Color was adjusted to be inside the allowed range.");
forbiddenColor = askedColor;
return false;
}
// Validate color against the current map tileset // Validate color against the current map tileset
if (!IsValid(askedColor, terrainColors, out forbiddenColor)) if (!IsValid(askedColor, terrainColors, out forbiddenColor))
{ {
@@ -81,10 +92,10 @@ namespace OpenRA.Mods.Common
Action<string> ignoreError = _ => { }; Action<string> ignoreError = _ => { };
do do
{ {
var hue = (byte)random.Next(255); var h = random.Next(255) / 255f;
var sat = (byte)random.Next(255); var s = float2.Lerp(HsvSaturationRange[0], HsvSaturationRange[1], random.NextFloat());
var lum = (byte)random.Next(129, 255); var v = float2.Lerp(HsvValueRange[0], HsvValueRange[1], random.NextFloat());
color = new HSLColor(hue, sat, lum); color = HSLColor.FromHSV(h, s, v);
} while (!IsValid(color.RGB, out forbidden, terrainColors, playerColors, ignoreError)); } while (!IsValid(color.RGB, out forbidden, terrainColors, playerColors, ignoreError));
return color; return color;