Restrict player color choices to the hue-saturation plane.
This commit is contained in:
@@ -15,6 +15,6 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
public class DefaultPlayer : IGlobalModData
|
public class DefaultPlayer : IGlobalModData
|
||||||
{
|
{
|
||||||
public readonly Color Color = Color.FromAhsl(0, 0, 238);
|
public readonly Color Color = Color.FromArgb(0xEE, 0xEE, 0xEE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ namespace OpenRA.Graphics
|
|||||||
var (r, g, b) = original.ToLinear();
|
var (r, g, b) = original.ToLinear();
|
||||||
|
|
||||||
// Calculate the brightness (i.e HSV value) of the original colour
|
// Calculate the brightness (i.e HSV value) of the original colour
|
||||||
|
// This inlines the single line of Color.RgbToHsv() that we need
|
||||||
var value = Math.Max(Math.Max(r, g), b);
|
var value = Math.Max(Math.Max(r, g), b);
|
||||||
|
|
||||||
// Construct the new RGB color
|
// Construct the new RGB color
|
||||||
|
|||||||
@@ -31,49 +31,17 @@ namespace OpenRA.Primitives
|
|||||||
|
|
||||||
public static Color FromAhsl(int alpha, float h, float s, float l)
|
public static Color FromAhsl(int alpha, float h, float s, float l)
|
||||||
{
|
{
|
||||||
// Convert from HSL to RGB
|
// Convert HSL to HSV
|
||||||
var q = (l < 0.5f) ? l * (1 + s) : l + s - (l * s);
|
var v = l + s * Math.Min(l, 1 - l);
|
||||||
var p = 2 * l - q;
|
var sv = v > 0 ? 2 * (1 - l / v) : 0;
|
||||||
|
|
||||||
float[] trgb = { h + 1 / 3.0f, h, h - 1 / 3.0f };
|
return FromAhsv(alpha, h, sv, v);
|
||||||
float[] rgb = { 0, 0, 0 };
|
|
||||||
|
|
||||||
for (var k = 0; k < 3; k++)
|
|
||||||
{
|
|
||||||
while (trgb[k] < 0) trgb[k] += 1.0f;
|
|
||||||
while (trgb[k] > 1) trgb[k] -= 1.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var k = 0; k < 3; k++)
|
|
||||||
{
|
|
||||||
if (trgb[k] < 1 / 6.0f)
|
|
||||||
rgb[k] = p + ((q - p) * 6 * trgb[k]);
|
|
||||||
else if (trgb[k] >= 1 / 6.0f && trgb[k] < 0.5)
|
|
||||||
rgb[k] = q;
|
|
||||||
else if (trgb[k] >= 0.5f && trgb[k] < 2.0f / 3)
|
|
||||||
rgb[k] = p + ((q - p) * 6 * (2.0f / 3 - trgb[k]));
|
|
||||||
else
|
|
||||||
rgb[k] = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
return FromArgb(alpha, (int)(rgb[0] * 255), (int)(rgb[1] * 255), (int)(rgb[2] * 255));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Color FromAhsl(int h, int s, int l)
|
|
||||||
{
|
|
||||||
return FromAhsl(255, h / 255f, s / 255f, l / 255f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Color FromAhsl(float h, float s, float l)
|
|
||||||
{
|
|
||||||
return FromAhsl(255, h, s, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Color FromAhsv(int alpha, float h, float s, float v)
|
public static Color FromAhsv(int alpha, float h, float s, float v)
|
||||||
{
|
{
|
||||||
var ll = 0.5f * (2 - s) * v;
|
var (r, g, b) = HsvToRgb(h, s, v);
|
||||||
var ss = (ll >= 1 || v <= 0) ? 0 : 0.5f * s * v / (ll <= 0.5f ? ll : 1 - ll);
|
return FromArgb(alpha, (byte)Math.Round(255 * r), (byte)Math.Round(255 * g), (byte)Math.Round(255 * b));
|
||||||
return FromAhsl(alpha, h, ss, ll);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Color FromAhsv(float h, float s, float v)
|
public static Color FromAhsv(float h, float s, float v)
|
||||||
@@ -83,13 +51,8 @@ namespace OpenRA.Primitives
|
|||||||
|
|
||||||
public void ToAhsv(out int a, out float h, out float s, out float v)
|
public void ToAhsv(out int a, out float h, out float s, out float v)
|
||||||
{
|
{
|
||||||
var ll = 2 * GetBrightness();
|
|
||||||
var ss = GetSaturation() * ((ll <= 1) ? ll : 2 - ll);
|
|
||||||
|
|
||||||
a = A;
|
a = A;
|
||||||
h = GetHue() / 360f;
|
(h, s, v) = RgbToHsv(R, G, B);
|
||||||
s = (2 * ss) / (ll + ss);
|
|
||||||
v = (ll + ss) / 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Color(long argb)
|
Color(long argb)
|
||||||
@@ -117,9 +80,84 @@ namespace OpenRA.Primitives
|
|||||||
return FromArgb((byte)(argb >> 24), (byte)(argb >> 16), (byte)(argb >> 8), (byte)argb);
|
return FromArgb((byte)(argb >> 24), (byte)(argb >> 16), (byte)(argb >> 8), (byte)argb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static float SrgbToLinear(float c)
|
||||||
|
{
|
||||||
|
// Standard gamma conversion equation: see e.g. http://entropymine.com/imageworsener/srgbformula/
|
||||||
|
return c <= 0.04045f ? c / 12.92f : (float)Math.Pow((c + 0.055f) / 1.055f, 2.4f);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float LinearToSrgb(float c)
|
||||||
|
{
|
||||||
|
// Standard gamma conversion equation: see e.g. http://entropymine.com/imageworsener/srgbformula/
|
||||||
|
return c <= 0.0031308f ? c * 12.92f : 1.055f * (float)Math.Pow(c, 1.0f / 2.4f) - 0.055f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public (float R, float G, float B) ToLinear()
|
||||||
|
{
|
||||||
|
// Undo pre-multiplied alpha and gamma correction
|
||||||
|
var r = SrgbToLinear((float)R / A);
|
||||||
|
var g = SrgbToLinear((float)G / A);
|
||||||
|
var b = SrgbToLinear((float)B / A);
|
||||||
|
|
||||||
|
return (r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Color FromLinear(byte a, float r, float g, float b)
|
||||||
|
{
|
||||||
|
// Apply gamma correction and pre-multiplied alpha
|
||||||
|
return FromArgb(a,
|
||||||
|
(byte)Math.Round(LinearToSrgb(r) * a),
|
||||||
|
(byte)Math.Round(LinearToSrgb(g) * a),
|
||||||
|
(byte)Math.Round(LinearToSrgb(b) * a));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (float R, float G, float B) HsvToRgb(float h, float s, float v)
|
||||||
|
{
|
||||||
|
// Based on maths explained in http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
|
||||||
|
var px = Math.Abs(h * 6f - 3);
|
||||||
|
var py = Math.Abs((h + 2f / 3) % 1 * 6f - 3);
|
||||||
|
var pz = Math.Abs((h + 1f / 3) % 1 * 6f - 3);
|
||||||
|
|
||||||
|
var r = v * float2.Lerp(1f, (px - 1).Clamp(0, 1), s);
|
||||||
|
var g = v * float2.Lerp(1f, (py - 1).Clamp(0, 1), s);
|
||||||
|
var b = v * float2.Lerp(1f, (pz - 1).Clamp(0, 1), s);
|
||||||
|
|
||||||
|
return (r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (float H, float S, float V) RgbToHsv(byte r, byte g, byte b)
|
||||||
|
{
|
||||||
|
return RgbToHsv(r / 255f, g / 255f, b / 255f);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (float H, float S, float V) RgbToHsv(float r, float g, float b)
|
||||||
|
{
|
||||||
|
// Based on maths explained in http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
|
||||||
|
var rgbMax = Math.Max(r, Math.Max(g, b));
|
||||||
|
var rgbMin = Math.Min(r, Math.Min(g, b));
|
||||||
|
var delta = rgbMax - rgbMin;
|
||||||
|
var v = rgbMax;
|
||||||
|
|
||||||
|
// Greyscale colors are defined to have hue and saturation 0
|
||||||
|
if (delta == 0.0f)
|
||||||
|
return (0, 0, v);
|
||||||
|
|
||||||
|
float hue;
|
||||||
|
if (r == rgbMax)
|
||||||
|
hue = (g - b) / (6 * delta);
|
||||||
|
else if (g == rgbMax)
|
||||||
|
hue = (b - r) / (6 * delta) + 1 / 3f;
|
||||||
|
else
|
||||||
|
hue = (r - g) / (6 * delta) + 2 / 3f;
|
||||||
|
|
||||||
|
var h = hue - (int)hue;
|
||||||
|
var s = delta / rgbMax;
|
||||||
|
return (h, s, v);
|
||||||
|
}
|
||||||
|
|
||||||
public static bool TryParse(string value, out Color color)
|
public static bool TryParse(string value, out Color color)
|
||||||
{
|
{
|
||||||
color = default(Color);
|
color = default;
|
||||||
value = value.Trim();
|
value = value.Trim();
|
||||||
if (value.Length != 6 && value.Length != 8)
|
if (value.Length != 6 && value.Length != 8)
|
||||||
return false;
|
return false;
|
||||||
@@ -155,46 +193,6 @@ namespace OpenRA.Primitives
|
|||||||
return (max + min) / 510f;
|
return (max + min) / 510f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetSaturation()
|
|
||||||
{
|
|
||||||
var min = Math.Min(R, Math.Min(G, B));
|
|
||||||
var max = Math.Max(R, Math.Max(G, B));
|
|
||||||
if (max == min)
|
|
||||||
return 0.0f;
|
|
||||||
|
|
||||||
var sum = max + min;
|
|
||||||
if (sum > byte.MaxValue)
|
|
||||||
sum = 510 - sum;
|
|
||||||
|
|
||||||
return (float)(max - min) / sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float GetHue()
|
|
||||||
{
|
|
||||||
var min = Math.Min(R, Math.Min(G, B));
|
|
||||||
var max = Math.Max(R, Math.Max(G, B));
|
|
||||||
if (max == min)
|
|
||||||
return 0.0f;
|
|
||||||
|
|
||||||
var diff = (float)(max - min);
|
|
||||||
var rNorm = (max - R) / diff;
|
|
||||||
var gNorm = (max - G) / diff;
|
|
||||||
var bNorm = (max - B) / diff;
|
|
||||||
|
|
||||||
var hue = 0.0f;
|
|
||||||
if (R == max)
|
|
||||||
hue = 60.0f * (6.0f + bNorm - gNorm);
|
|
||||||
if (G == max)
|
|
||||||
hue = 60.0f * (2.0f + rNorm - bNorm);
|
|
||||||
if (B == max)
|
|
||||||
hue = 60.0f * (4.0f + gNorm - rNorm);
|
|
||||||
|
|
||||||
if (hue > 360.0f)
|
|
||||||
hue -= 360f;
|
|
||||||
|
|
||||||
return hue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte A => (byte)(argb >> 24);
|
public byte A => (byte)(argb >> 24);
|
||||||
public byte R => (byte)(argb >> 16);
|
public byte R => (byte)(argb >> 16);
|
||||||
public byte G => (byte)(argb >> 8);
|
public byte G => (byte)(argb >> 8);
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Scripting.Global
|
|||||||
var s = (byte)saturation.Clamp(0, 255);
|
var s = (byte)saturation.Clamp(0, 255);
|
||||||
var l = (byte)luminosity.Clamp(0, 255);
|
var l = (byte)luminosity.Clamp(0, 255);
|
||||||
|
|
||||||
return Color.FromAhsl(h, s, l);
|
return Color.FromAhsl(255, h / 255f, s / 255f, l / 255f);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Desc("Create a new color with the specified red/green/blue/[alpha] values.")]
|
[Desc("Create a new color with the specified red/green/blue/[alpha] values.")]
|
||||||
|
|||||||
@@ -20,13 +20,22 @@ using OpenRA.Traits;
|
|||||||
namespace OpenRA.Mods.Common.Traits
|
namespace OpenRA.Mods.Common.Traits
|
||||||
{
|
{
|
||||||
[Desc("Configuration options for the lobby player color picker. Attach this to the world actor.")]
|
[Desc("Configuration options for the lobby player color picker. Attach this to the world actor.")]
|
||||||
public class ColorPickerManagerInfo : TraitInfo<ColorPickerManager>
|
public class ColorPickerManagerInfo : TraitInfo<ColorPickerManager>, IRulesetLoaded
|
||||||
{
|
{
|
||||||
// The bigger the color threshold, the less permissive is the algorithm
|
[Desc("Minimum and maximum saturation levels that are valid for use.")]
|
||||||
public readonly int Threshold = 0x50;
|
public readonly float[] HsvSaturationRange = { 0.3f, 0.95f };
|
||||||
public readonly float[] HsvSaturationRange = new[] { 0.25f, 1f };
|
|
||||||
public readonly float[] HsvValueRange = new[] { 0.2f, 1.0f };
|
[Desc("HSV value component for player colors.")]
|
||||||
public readonly Color[] TeamColorPresets = { };
|
public readonly float V = 0.95f;
|
||||||
|
|
||||||
|
[Desc("Perceptual color threshold for determining whether two colors are too similar.")]
|
||||||
|
public readonly float SimilarityThreshold = 0.314f;
|
||||||
|
|
||||||
|
[Desc("List of hue components for the preset colors in the palette tab. Each entry must have a corresponding PresetSaturations definition.")]
|
||||||
|
public readonly float[] PresetHues = { };
|
||||||
|
|
||||||
|
[Desc("List of saturation components for the preset colors in the palette tab. Each entry must have a corresponding PresetHues definition.")]
|
||||||
|
public readonly float[] PresetSaturations = { };
|
||||||
|
|
||||||
[PaletteReference]
|
[PaletteReference]
|
||||||
public readonly string PaletteName = "colorpicker";
|
public readonly string PaletteName = "colorpicker";
|
||||||
@@ -40,9 +49,22 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
"A dictionary of [faction name]: [actor name].")]
|
"A dictionary of [faction name]: [actor name].")]
|
||||||
public readonly Dictionary<string, string> FactionPreviewActors = new Dictionary<string, string>();
|
public readonly Dictionary<string, string> FactionPreviewActors = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
[FieldLoader.Require]
|
||||||
[Desc("Remap these indices to player colors.")]
|
[Desc("Remap these indices to player colors.")]
|
||||||
public readonly int[] RemapIndices = { };
|
public readonly int[] RemapIndices = { };
|
||||||
|
|
||||||
|
public void RulesetLoaded(Ruleset rules, ActorInfo ai)
|
||||||
|
{
|
||||||
|
if (PresetHues.Length != PresetSaturations.Length)
|
||||||
|
throw new YamlException("PresetHues and PresetSaturations must have the same number of elements.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Color> PresetColors()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < PresetHues.Length; i++)
|
||||||
|
yield return Color.FromAhsv(PresetHues[i], PresetSaturations[i], V);
|
||||||
|
}
|
||||||
|
|
||||||
public Color Color { get; private set; }
|
public Color Color { get; private set; }
|
||||||
|
|
||||||
public void Update(WorldRenderer worldRenderer, Color color)
|
public void Update(WorldRenderer worldRenderer, Color color)
|
||||||
@@ -56,161 +78,120 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
worldRenderer.ReplacePalette(PaletteName, newPalette);
|
worldRenderer.ReplacePalette(PaletteName, newPalette);
|
||||||
}
|
}
|
||||||
|
|
||||||
double GetColorDelta(Color colorA, Color colorB)
|
bool TryGetBlockingColor((float R, float G, float B) color, List<(float R, float G, float B)> candidateBlockers, out (float R, float G, float B) closestBlocker)
|
||||||
{
|
{
|
||||||
var rmean = (colorA.R + colorB.R) / 2.0;
|
var closestDistance = SimilarityThreshold;
|
||||||
var r = colorA.R - colorB.R;
|
closestBlocker = default;
|
||||||
var g = colorA.G - colorB.G;
|
|
||||||
var b = colorA.B - colorB.B;
|
|
||||||
var weightR = 2.0 + rmean / 256;
|
|
||||||
var weightG = 4.0;
|
|
||||||
var weightB = 2.0 + (255 - rmean) / 256;
|
|
||||||
return Math.Sqrt(weightR * r * r + weightG * g * g + weightB * b * b);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsValid(Color askedColor, IEnumerable<Color> forbiddenColors, out Color forbiddenColor)
|
foreach (var candidate in candidateBlockers)
|
||||||
{
|
|
||||||
var blockingColors = forbiddenColors
|
|
||||||
.Where(playerColor => GetColorDelta(askedColor, playerColor) < Threshold)
|
|
||||||
.Select(playerColor => new { Delta = GetColorDelta(askedColor, playerColor), Color = playerColor });
|
|
||||||
|
|
||||||
// Return the player that holds with the lowest difference
|
|
||||||
if (blockingColors.Any())
|
|
||||||
{
|
{
|
||||||
forbiddenColor = blockingColors.MinBy(aa => aa.Delta).Color;
|
// Uses the perceptually based color metric explained by https://www.compuphase.com/cmetric.htm
|
||||||
return false;
|
// Input colors are expected to be in the linear (non-gamma corrected) color space
|
||||||
|
var rmean = (color.R + candidate.R) / 2.0;
|
||||||
|
var r = color.R - candidate.R;
|
||||||
|
var g = color.G - candidate.G;
|
||||||
|
var b = color.B - candidate.B;
|
||||||
|
var weightR = 2.0 + rmean;
|
||||||
|
var weightG = 4.0;
|
||||||
|
var weightB = 3.0 - rmean;
|
||||||
|
|
||||||
|
var distance = (float)Math.Sqrt(weightR * r * r + weightG * g * g + weightB * b * b);
|
||||||
|
if (distance < closestDistance)
|
||||||
|
{
|
||||||
|
closestBlocker = candidate;
|
||||||
|
closestDistance = distance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
forbiddenColor = default(Color);
|
return closestDistance < SimilarityThreshold;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsValid(Color askedColor, out Color forbiddenColor, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors, HashSet<string> errorMessages = null)
|
|
||||||
{
|
|
||||||
// Validate color against HSV
|
|
||||||
askedColor.ToAhsv(out _, out _, out var s, out var v);
|
|
||||||
if (s < HsvSaturationRange[0] || s > HsvSaturationRange[1] || v < HsvValueRange[0] || v > HsvValueRange[1])
|
|
||||||
{
|
|
||||||
errorMessages?.Add("Color was adjusted to be inside the allowed range.");
|
|
||||||
forbiddenColor = askedColor;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate color against the current map tileset
|
|
||||||
if (!IsValid(askedColor, terrainColors, out forbiddenColor))
|
|
||||||
{
|
|
||||||
errorMessages?.Add("Color was adjusted to be less similar to the terrain.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate color against other clients
|
|
||||||
if (!IsValid(askedColor, playerColors, out forbiddenColor))
|
|
||||||
{
|
|
||||||
errorMessages?.Add("Color was adjusted to be less similar to another player.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Color is valid
|
|
||||||
forbiddenColor = default(Color);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color RandomPresetColor(MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors)
|
public Color RandomPresetColor(MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors)
|
||||||
{
|
{
|
||||||
if (TeamColorPresets.Any())
|
var terrainLinear = terrainColors.Select(c => c.ToLinear()).ToList();
|
||||||
|
var playerLinear = playerColors.Select(c => c.ToLinear()).ToList();
|
||||||
|
|
||||||
|
if (PresetHues.Any())
|
||||||
{
|
{
|
||||||
foreach (var c in TeamColorPresets.Shuffle(random))
|
foreach (var i in Exts.MakeArray(PresetHues.Length, x => x).Shuffle(random))
|
||||||
if (IsValid(c, out _, terrainColors, playerColors))
|
{
|
||||||
return c;
|
var h = PresetHues[i];
|
||||||
|
var s = PresetSaturations[i];
|
||||||
|
var preset = Color.FromAhsv(h, s, V);
|
||||||
|
|
||||||
|
// Color may already be taken
|
||||||
|
var linear = preset.ToLinear();
|
||||||
|
if (!TryGetBlockingColor(linear, terrainLinear, out _) && !TryGetBlockingColor(linear, playerLinear, out _))
|
||||||
|
return preset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return RandomValidColor(random, terrainColors, playerColors);
|
// Fall back to a random non-preset color
|
||||||
|
var randomHue = random.NextFloat();
|
||||||
|
var randomSat = float2.Lerp(HsvSaturationRange[0], HsvSaturationRange[1], random.NextFloat());
|
||||||
|
return MakeValid(randomHue, randomSat, random, terrainLinear, playerLinear, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color RandomValidColor(MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors)
|
public Color RandomValidColor(MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors)
|
||||||
{
|
{
|
||||||
Color color;
|
var h = random.NextFloat();
|
||||||
do
|
var s = float2.Lerp(HsvSaturationRange[0], HsvSaturationRange[1], random.NextFloat());
|
||||||
{
|
return MakeValid(h, s, random, terrainColors, playerColors, null);
|
||||||
var h = random.Next(255) / 255f;
|
|
||||||
var s = float2.Lerp(HsvSaturationRange[0], HsvSaturationRange[1], random.NextFloat());
|
|
||||||
var v = float2.Lerp(HsvValueRange[0], HsvValueRange[1], random.NextFloat());
|
|
||||||
color = Color.FromAhsv(h, s, v);
|
|
||||||
}
|
|
||||||
while (!IsValid(color, out _, terrainColors, playerColors));
|
|
||||||
|
|
||||||
return color;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color MakeValid(Color askedColor, MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors, Action<string> onError)
|
public Color MakeValid(Color color, MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors, Action<string> onError = null)
|
||||||
{
|
{
|
||||||
var errorMessages = new HashSet<string>();
|
color.ToAhsv(out _, out var h, out var s, out _);
|
||||||
|
return MakeValid(h, s, random, terrainColors, playerColors, onError);
|
||||||
|
}
|
||||||
|
|
||||||
if (IsValid(askedColor, out var forbiddenColor, terrainColors, playerColors, errorMessages))
|
Color MakeValid(float hue, float sat, MersenneTwister random, IEnumerable<Color> terrainColors, IEnumerable<Color> playerColors, Action<string> onError)
|
||||||
return askedColor;
|
{
|
||||||
|
var terrainLinear = terrainColors.Select(c => c.ToLinear()).ToList();
|
||||||
|
var playerLinear = playerColors.Select(c => c.ToLinear()).ToList();
|
||||||
|
|
||||||
// Vector between the 2 colors
|
return MakeValid(hue, sat, random, terrainLinear, playerLinear, onError);
|
||||||
var vector = new double[]
|
}
|
||||||
|
|
||||||
|
Color MakeValid(float hue, float sat, MersenneTwister random, List<(float R, float G, float B)> terrainLinear, List<(float R, float G, float B)> playerLinear, Action<string> onError)
|
||||||
|
{
|
||||||
|
// Clamp saturation without triggering a warning
|
||||||
|
// This can only happen due to rounding errors (common) or modified clients (rare)
|
||||||
|
sat = sat.Clamp(HsvSaturationRange[0], HsvSaturationRange[1]);
|
||||||
|
|
||||||
|
// Limit to 100 attempts, which is enough to move all the way around the hue range
|
||||||
|
string errorMessage = null;
|
||||||
|
var stepSign = 0;
|
||||||
|
for (var i = 0; i < 101; i++)
|
||||||
{
|
{
|
||||||
askedColor.R - forbiddenColor.R,
|
var linear = Color.FromAhsv(hue, sat, V).ToLinear();
|
||||||
askedColor.G - forbiddenColor.G,
|
if (TryGetBlockingColor(linear, terrainLinear, out var blocker))
|
||||||
askedColor.B - forbiddenColor.B
|
errorMessage = "Color was adjusted to be less similar to the terrain.";
|
||||||
};
|
else if (TryGetBlockingColor(linear, playerLinear, out blocker))
|
||||||
|
errorMessage = "Color was adjusted to be less similar to another player.";
|
||||||
// Reduce vector by it's biggest value (more calculations, but more accuracy too)
|
else
|
||||||
var vectorMax = vector.Max(vv => Math.Abs(vv));
|
|
||||||
if (vectorMax == 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;
|
|
||||||
vector[2] /= vectorMax;
|
|
||||||
|
|
||||||
// Color weights
|
|
||||||
var rmean = (double)(askedColor.R + forbiddenColor.R) / 2;
|
|
||||||
var weightVector = new[]
|
|
||||||
{
|
|
||||||
2.0 + rmean / 256,
|
|
||||||
4.0,
|
|
||||||
2.0 + (255 - rmean) / 256,
|
|
||||||
};
|
|
||||||
|
|
||||||
var attempt = 1;
|
|
||||||
Color color;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
// If we reached the limit (The ii >= 255 prevents too much calculations)
|
|
||||||
if (attempt >= 255)
|
|
||||||
{
|
{
|
||||||
color = RandomPresetColor(random, terrainColors, playerColors);
|
if (errorMessage != null)
|
||||||
errorMessages.Add("Color could not be adjusted enough, a new color has been picked.");
|
onError?.Invoke(errorMessage);
|
||||||
break;
|
|
||||||
|
return Color.FromAhsv(hue, sat, V);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply vector to forbidden color
|
// Pick a direction based on the first blocking color and step in hue
|
||||||
var r = (forbiddenColor.R + (int)(vector[0] * weightVector[0] * attempt)).Clamp(0, 255);
|
// until we either find a suitable color or loop back to where we started.
|
||||||
var g = (forbiddenColor.G + (int)(vector[1] * weightVector[1] * attempt)).Clamp(0, 255);
|
// This is a simple way to avoid being trapped between two blocking colors.
|
||||||
var b = (forbiddenColor.B + (int)(vector[2] * weightVector[2] * attempt)).Clamp(0, 255);
|
if (stepSign == 0)
|
||||||
|
{
|
||||||
|
Color.FromLinear(255, blocker.R, blocker.G, blocker.B).ToAhsv(out _, out var blockerHue, out _, out _);
|
||||||
|
stepSign = blockerHue > hue ? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Get the alternative color attempt
|
hue += stepSign * 0.01f;
|
||||||
color = Color.FromArgb(r, g, b);
|
|
||||||
|
|
||||||
attempt++;
|
|
||||||
}
|
}
|
||||||
while (!IsValid(color, out forbiddenColor, terrainColors, playerColors, errorMessages));
|
|
||||||
|
|
||||||
// Coalesce the error messages to only print one of each type despite up to 255 iterations
|
// Failed to find a solution within a reasonable time: return a random color without any validation
|
||||||
errorMessages.Do(onError);
|
onError?.Invoke("Unable to determine a valid player color. A random color has been selected.");
|
||||||
|
return Color.FromAhsv(random.NextFloat(), float2.Lerp(HsvSaturationRange[0], HsvSaturationRange[1], random.NextFloat()), V);
|
||||||
return color;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,29 +19,22 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
{
|
{
|
||||||
public class ColorMixerWidget : Widget
|
public class ColorMixerWidget : Widget
|
||||||
{
|
{
|
||||||
public float STrim = 0.025f;
|
|
||||||
public float VTrim = 0.025f;
|
|
||||||
|
|
||||||
public event Action OnChange = () => { };
|
public event Action OnChange = () => { };
|
||||||
|
|
||||||
public float H { get; private set; }
|
public float H { get; private set; }
|
||||||
public float S { get; private set; }
|
public float S { get; private set; }
|
||||||
public float V { get; private set; }
|
public float V { get; private set; }
|
||||||
|
float minSat, maxSat;
|
||||||
|
|
||||||
byte[] front, back;
|
Sheet mixerSheet;
|
||||||
Sprite mixerSprite;
|
Sprite mixerSprite;
|
||||||
bool isMoving;
|
bool isMoving;
|
||||||
|
|
||||||
bool update;
|
public ColorMixerWidget()
|
||||||
readonly object syncWorker = new object();
|
{
|
||||||
readonly object bufferSync = new object();
|
V = 1.0f;
|
||||||
Thread workerThread;
|
}
|
||||||
bool workerAlive;
|
|
||||||
|
|
||||||
float[] sRange = { 0.0f, 1.0f };
|
|
||||||
float[] vRange = { 0.0f, 1.0f };
|
|
||||||
|
|
||||||
public ColorMixerWidget() { }
|
|
||||||
public ColorMixerWidget(ColorMixerWidget other)
|
public ColorMixerWidget(ColorMixerWidget other)
|
||||||
: base(other)
|
: base(other)
|
||||||
{
|
{
|
||||||
@@ -49,118 +42,45 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
H = other.H;
|
H = other.H;
|
||||||
S = other.S;
|
S = other.S;
|
||||||
V = other.V;
|
V = other.V;
|
||||||
|
minSat = other.minSat;
|
||||||
sRange = (float[])other.sRange.Clone();
|
maxSat = other.maxSat;
|
||||||
vRange = (float[])other.vRange.Clone();
|
|
||||||
|
|
||||||
STrim = other.STrim;
|
|
||||||
VTrim = other.VTrim;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPaletteRange(float sMin, float sMax, float vMin, float vMax)
|
public void SetColorLimits(float minSaturation, float maxSaturation, float v)
|
||||||
{
|
{
|
||||||
sRange[0] = sMin + STrim;
|
minSat = minSaturation;
|
||||||
sRange[1] = sMax - STrim;
|
maxSat = maxSaturation;
|
||||||
vRange[0] = vMin + VTrim;
|
V = v;
|
||||||
vRange[1] = vMax - VTrim;
|
|
||||||
|
|
||||||
var rect = new Rectangle((int)(255 * sRange[0]), (int)(255 * (1 - vRange[1])), (int)(255 * (sRange[1] - sRange[0])) + 1, (int)(255 * (vRange[1] - vRange[0])) + 1);
|
var buffer = new byte[4 * 256 * 256];
|
||||||
mixerSprite = new Sprite(mixerSprite.Sheet, rect, TextureChannel.RGBA);
|
unsafe
|
||||||
|
{
|
||||||
|
// Generate palette in HSV
|
||||||
|
fixed (byte* cc = &buffer[0])
|
||||||
|
{
|
||||||
|
var c = (int*)cc;
|
||||||
|
for (var s = 0; s < 256; s++)
|
||||||
|
for (var h = 0; h < 256; h++)
|
||||||
|
(*(c + s * 256 + h)) = Color.FromAhsv(h / 255f, 1 - s / 255f, V).ToArgb();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var rect = new Rectangle(0, (int)(255 * (1 - maxSat)), 255, (int)(255 * (maxSat - minSat)) + 1);
|
||||||
|
mixerSprite = new Sprite(mixerSheet, rect, TextureChannel.RGBA);
|
||||||
|
|
||||||
|
mixerSheet.GetTexture().SetData(buffer, 256, 256);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Initialize(WidgetArgs args)
|
public override void Initialize(WidgetArgs args)
|
||||||
{
|
{
|
||||||
base.Initialize(args);
|
base.Initialize(args);
|
||||||
|
|
||||||
sRange[0] += STrim;
|
mixerSheet = new Sheet(SheetType.BGRA, new Size(256, 256));
|
||||||
sRange[1] -= STrim;
|
SetColorLimits(minSat, maxSat, V);
|
||||||
vRange[0] += VTrim;
|
|
||||||
vRange[1] -= VTrim;
|
|
||||||
|
|
||||||
// Bitmap data is generated in a background thread and then flipped
|
|
||||||
front = new byte[4 * 256 * 256];
|
|
||||||
back = new byte[4 * 256 * 256];
|
|
||||||
|
|
||||||
var rect = new Rectangle((int)(255 * sRange[0]), (int)(255 * (1 - vRange[1])), (int)(255 * (sRange[1] - sRange[0])) + 1, (int)(255 * (vRange[1] - vRange[0])) + 1);
|
|
||||||
var mixerSheet = new Sheet(SheetType.BGRA, new Size(256, 256));
|
|
||||||
mixerSheet.GetTexture().SetData(front, 256, 256);
|
|
||||||
mixerSprite = new Sprite(mixerSheet, rect, TextureChannel.RGBA);
|
|
||||||
GenerateBitmap();
|
|
||||||
}
|
|
||||||
|
|
||||||
void GenerateBitmap()
|
|
||||||
{
|
|
||||||
// Generating the selection bitmap is slow,
|
|
||||||
// so we do it in a background thread
|
|
||||||
lock (syncWorker)
|
|
||||||
{
|
|
||||||
update = true;
|
|
||||||
|
|
||||||
if (workerThread == null || !workerAlive)
|
|
||||||
{
|
|
||||||
workerThread = new Thread(GenerateBitmapWorker);
|
|
||||||
workerThread.Start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GenerateBitmapWorker()
|
|
||||||
{
|
|
||||||
lock (syncWorker)
|
|
||||||
workerAlive = true;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
float hue;
|
|
||||||
lock (syncWorker)
|
|
||||||
{
|
|
||||||
if (!update)
|
|
||||||
{
|
|
||||||
workerAlive = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
update = false;
|
|
||||||
|
|
||||||
// Take a local copy of the hue to generate to avoid tearing
|
|
||||||
hue = H;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
// Generate palette in HSV
|
|
||||||
fixed (byte* cc = &back[0])
|
|
||||||
{
|
|
||||||
var c = (int*)cc;
|
|
||||||
for (var v = 0; v < 256; v++)
|
|
||||||
for (var s = 0; s < 256; s++)
|
|
||||||
(*(c + (v * 256) + s)) = Color.FromAhsv(hue, s / 255f, (255 - v) / 255f).ToArgb();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lock (bufferSync)
|
|
||||||
{
|
|
||||||
var swap = front;
|
|
||||||
front = back;
|
|
||||||
back = swap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Draw()
|
public override void Draw()
|
||||||
{
|
{
|
||||||
if (Monitor.TryEnter(bufferSync))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
mixerSprite.Sheet.GetTexture().SetData(front, 256, 256);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Monitor.Exit(bufferSync);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(mixerSprite, RenderOrigin, new float2(RenderBounds.Size));
|
Game.Renderer.RgbaSpriteRenderer.DrawSprite(mixerSprite, RenderOrigin, new float2(RenderBounds.Size));
|
||||||
|
|
||||||
var sprite = ChromeProvider.GetImage("lobby-bits", "colorpicker");
|
var sprite = ChromeProvider.GetImage("lobby-bits", "colorpicker");
|
||||||
@@ -172,17 +92,17 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
void SetValueFromPx(int2 xy)
|
void SetValueFromPx(int2 xy)
|
||||||
{
|
{
|
||||||
var rb = RenderBounds;
|
var rb = RenderBounds;
|
||||||
var s = sRange[0] + xy.X * (sRange[1] - sRange[0]) / rb.Width;
|
var h = xy.X * 1f / rb.Width;
|
||||||
var v = sRange[1] - xy.Y * (vRange[1] - vRange[0]) / rb.Height;
|
var s = float2.Lerp(minSat, maxSat, 1 - xy.Y * 1f / rb.Height);
|
||||||
S = s.Clamp(sRange[0], sRange[1]);
|
H = h.Clamp(0, 1f);
|
||||||
V = v.Clamp(vRange[0], vRange[1]);
|
S = s.Clamp(minSat, maxSat);
|
||||||
}
|
}
|
||||||
|
|
||||||
int2 PxFromValue()
|
int2 PxFromValue()
|
||||||
{
|
{
|
||||||
var rb = RenderBounds;
|
var rb = RenderBounds;
|
||||||
var x = RenderBounds.Width * (S - sRange[0]) / (sRange[1] - sRange[0]);
|
var x = RenderBounds.Width * H;
|
||||||
var y = RenderBounds.Height * (1 - (V - vRange[0]) / (vRange[1] - vRange[0]));
|
var y = RenderBounds.Height * (1 - (S - minSat) / (maxSat - minSat));
|
||||||
return new int2((int)x.Clamp(0, rb.Width), (int)y.Clamp(0, rb.Height));
|
return new int2((int)x.Clamp(0, rb.Width), (int)y.Clamp(0, rb.Height));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,30 +143,18 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
|
|
||||||
public Color Color => Color.FromAhsv(H, S, V);
|
public Color Color => Color.FromAhsv(H, S, V);
|
||||||
|
|
||||||
public void Set(float hue)
|
/// <summary>
|
||||||
{
|
/// Set the color picker to nearest valid color to the given value.
|
||||||
if (H != hue)
|
/// The saturation and brightness may be adjusted.
|
||||||
{
|
/// </summary>
|
||||||
H = hue;
|
|
||||||
GenerateBitmap();
|
|
||||||
OnChange();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Set(Color color)
|
public void Set(Color color)
|
||||||
{
|
{
|
||||||
color.ToAhsv(out _, out var h, out var s, out var v);
|
color.ToAhsv(out _, out var h, out var s, out _);
|
||||||
|
|
||||||
if (H != h || S != s || V != v)
|
if (H != h || S != s)
|
||||||
{
|
{
|
||||||
if (H != h)
|
H = h;
|
||||||
{
|
S = s.Clamp(minSat, maxSat);
|
||||||
H = h;
|
|
||||||
GenerateBitmap();
|
|
||||||
}
|
|
||||||
|
|
||||||
S = s.Clamp(sRange[0], sRange[1]);
|
|
||||||
V = v.Clamp(vRange[0], vRange[1]);
|
|
||||||
OnChange();
|
OnChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
#region Copyright & License Information
|
|
||||||
/*
|
|
||||||
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
|
|
||||||
* This file is part of OpenRA, which is free software. It is made
|
|
||||||
* available to you under the terms of the GNU General Public License
|
|
||||||
* as published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version. For more
|
|
||||||
* information, see COPYING.
|
|
||||||
*/
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
using OpenRA.Graphics;
|
|
||||||
using OpenRA.Primitives;
|
|
||||||
using OpenRA.Widgets;
|
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.Widgets
|
|
||||||
{
|
|
||||||
public class HueSliderWidget : SliderWidget
|
|
||||||
{
|
|
||||||
Sprite hueSprite;
|
|
||||||
Sprite pickerSprite;
|
|
||||||
|
|
||||||
public HueSliderWidget() { }
|
|
||||||
public HueSliderWidget(HueSliderWidget other)
|
|
||||||
: base(other) { }
|
|
||||||
|
|
||||||
public override void Initialize(WidgetArgs args)
|
|
||||||
{
|
|
||||||
base.Initialize(args);
|
|
||||||
|
|
||||||
var hueSheet = new Sheet(SheetType.BGRA, new Size(256, 1));
|
|
||||||
hueSprite = new Sprite(hueSheet, new Rectangle(0, 0, 256, 1), TextureChannel.RGBA);
|
|
||||||
|
|
||||||
var hueData = new uint[1, 256];
|
|
||||||
for (var x = 0; x < 256; x++)
|
|
||||||
hueData[0, x] = (uint)Color.FromAhsv(x / 255f, 1, 1).ToArgb();
|
|
||||||
|
|
||||||
hueSheet.GetTexture().SetData(hueData);
|
|
||||||
|
|
||||||
pickerSprite = ChromeProvider.GetImage("lobby-bits", "huepicker");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Draw()
|
|
||||||
{
|
|
||||||
if (!IsVisible())
|
|
||||||
return;
|
|
||||||
|
|
||||||
var ro = RenderOrigin;
|
|
||||||
var rb = RenderBounds;
|
|
||||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(hueSprite, ro, new float2(rb.Size));
|
|
||||||
|
|
||||||
var pos = RenderOrigin + new int2(PxFromValue(Value).Clamp(0, rb.Width - 1) - (int)pickerSprite.Size.X / 2, (rb.Height - (int)pickerSprite.Size.Y) / 2);
|
|
||||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(pickerSprite, pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -28,33 +28,24 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
public ColorPickerLogic(Widget widget, ModData modData, World world, Color initialColor, string initialFaction, Action<Color> onChange,
|
public ColorPickerLogic(Widget widget, ModData modData, World world, Color initialColor, string initialFaction, Action<Color> onChange,
|
||||||
Dictionary<string, MiniYaml> logicArgs)
|
Dictionary<string, MiniYaml> logicArgs)
|
||||||
{
|
{
|
||||||
var hueSlider = widget.Get<SliderWidget>("HUE");
|
|
||||||
var mixer = widget.Get<ColorMixerWidget>("MIXER");
|
var mixer = widget.Get<ColorMixerWidget>("MIXER");
|
||||||
var randomButton = widget.GetOrNull<ButtonWidget>("RANDOM_BUTTON");
|
|
||||||
|
|
||||||
hueSlider.OnChange += _ => mixer.Set(hueSlider.Value);
|
|
||||||
mixer.OnChange += () => onChange(mixer.Color);
|
|
||||||
|
|
||||||
if (randomButton != null)
|
|
||||||
{
|
|
||||||
randomButton.OnClick = () =>
|
|
||||||
{
|
|
||||||
// Avoid colors with low sat or lum
|
|
||||||
var hue = (byte)Game.CosmeticRandom.Next(255);
|
|
||||||
var sat = (byte)Game.CosmeticRandom.Next(70, 255);
|
|
||||||
var lum = (byte)Game.CosmeticRandom.Next(70, 255);
|
|
||||||
var color = Color.FromAhsl(hue, sat, lum);
|
|
||||||
|
|
||||||
mixer.Set(color);
|
|
||||||
hueSlider.Value = HueFromColor(color);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the initial state
|
// Set the initial state
|
||||||
var colorManager = world.WorldActor.Info.TraitInfo<ColorPickerManagerInfo>();
|
var colorManager = world.WorldActor.Info.TraitInfo<ColorPickerManagerInfo>();
|
||||||
mixer.SetPaletteRange(colorManager.HsvSaturationRange[0], colorManager.HsvSaturationRange[1], colorManager.HsvValueRange[0], colorManager.HsvValueRange[1]);
|
mixer.SetColorLimits(colorManager.HsvSaturationRange[0], colorManager.HsvSaturationRange[1], colorManager.V);
|
||||||
|
mixer.OnChange += () => onChange(mixer.Color);
|
||||||
mixer.Set(initialColor);
|
mixer.Set(initialColor);
|
||||||
hueSlider.Value = HueFromColor(initialColor);
|
|
||||||
|
var randomButton = widget.GetOrNull<ButtonWidget>("RANDOM_BUTTON");
|
||||||
|
if (randomButton != null)
|
||||||
|
{
|
||||||
|
var terrainColors = modData.DefaultTerrainInfo
|
||||||
|
.SelectMany(t => t.Value.RestrictedPlayerColors)
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
var playerColors = Enumerable.Empty<Color>();
|
||||||
|
randomButton.OnClick = () => mixer.Set(colorManager.RandomValidColor(world.LocalRandom, terrainColors, playerColors));
|
||||||
|
}
|
||||||
|
|
||||||
if (initialFaction == null || !colorManager.FactionPreviewActors.TryGetValue(initialFaction, out var actorType))
|
if (initialFaction == null || !colorManager.FactionPreviewActors.TryGetValue(initialFaction, out var actorType))
|
||||||
actorType = colorManager.PreviewActor;
|
actorType = colorManager.PreviewActor;
|
||||||
@@ -125,15 +116,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
if (!int.TryParse(yaml.Value, out paletteCustomRows))
|
if (!int.TryParse(yaml.Value, out paletteCustomRows))
|
||||||
throw new YamlException($"Invalid value for PaletteCustomRows: {yaml.Value}");
|
throw new YamlException($"Invalid value for PaletteCustomRows: {yaml.Value}");
|
||||||
|
|
||||||
|
var presetColors = colorManager.PresetColors().ToList();
|
||||||
for (var j = 0; j < palettePresetRows; j++)
|
for (var j = 0; j < palettePresetRows; j++)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < paletteCols; i++)
|
for (var i = 0; i < paletteCols; i++)
|
||||||
{
|
{
|
||||||
var colorIndex = j * paletteCols + i;
|
var colorIndex = j * paletteCols + i;
|
||||||
if (colorIndex >= colorManager.TeamColorPresets.Length)
|
if (colorIndex >= presetColors.Count)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
var color = colorManager.TeamColorPresets[colorIndex];
|
var color = presetColors[colorIndex];
|
||||||
|
|
||||||
var newSwatch = (ColorBlockWidget)presetColorTemplate.Clone();
|
var newSwatch = (ColorBlockWidget)presetColorTemplate.Clone();
|
||||||
newSwatch.GetColor = () => color;
|
newSwatch.GetColor = () => color;
|
||||||
@@ -143,7 +135,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
newSwatch.OnMouseUp = m =>
|
newSwatch.OnMouseUp = m =>
|
||||||
{
|
{
|
||||||
mixer.Set(color);
|
mixer.Set(color);
|
||||||
hueSlider.Value = HueFromColor(color);
|
|
||||||
onChange(color);
|
onChange(color);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -166,7 +157,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
{
|
{
|
||||||
var color = Game.Settings.Player.CustomColors[colorIndex];
|
var color = Game.Settings.Player.CustomColors[colorIndex];
|
||||||
mixer.Set(color);
|
mixer.Set(color);
|
||||||
hueSlider.Value = HueFromColor(color);
|
|
||||||
onChange(color);
|
onChange(color);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -198,12 +188,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static float HueFromColor(Color c)
|
|
||||||
{
|
|
||||||
c.ToAhsv(out _, out var h, out _, out _);
|
|
||||||
return h;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ShowColorDropDown(DropDownButtonWidget color, ColorPickerManagerInfo colorManager, WorldRenderer worldRenderer, Action onExit = null)
|
public static void ShowColorDropDown(DropDownButtonWidget color, ColorPickerManagerInfo colorManager, WorldRenderer worldRenderer, Action onExit = null)
|
||||||
{
|
{
|
||||||
color.RemovePanel();
|
color.RemovePanel();
|
||||||
|
|||||||
@@ -47,25 +47,12 @@ Background@COLOR_CHOOSER:
|
|||||||
Width: PARENT_RIGHT - 91
|
Width: PARENT_RIGHT - 91
|
||||||
Height: PARENT_BOTTOM - 34
|
Height: PARENT_BOTTOM - 34
|
||||||
Children:
|
Children:
|
||||||
Background@HUEBG:
|
Background@MIXERBG:
|
||||||
Background: panel-black
|
Background: panel-black
|
||||||
X: 0
|
X: 0
|
||||||
Y: 0
|
Y: 0
|
||||||
Width: PARENT_RIGHT
|
Width: PARENT_RIGHT
|
||||||
Height: 17
|
Height: 114
|
||||||
Children:
|
|
||||||
HueSlider@HUE:
|
|
||||||
X: 2
|
|
||||||
Y: 2
|
|
||||||
Width: PARENT_RIGHT - 4
|
|
||||||
Height: PARENT_BOTTOM - 4
|
|
||||||
Ticks: 5
|
|
||||||
Background@MIXERBG:
|
|
||||||
Background: panel-black
|
|
||||||
X: 0
|
|
||||||
Y: 22
|
|
||||||
Width: PARENT_RIGHT
|
|
||||||
Height: 92
|
|
||||||
Children:
|
Children:
|
||||||
ColorMixer@MIXER:
|
ColorMixer@MIXER:
|
||||||
X: 2
|
X: 2
|
||||||
|
|||||||
@@ -257,7 +257,8 @@ World:
|
|||||||
ColorPickerManager:
|
ColorPickerManager:
|
||||||
PreviewActor: fact.colorpicker
|
PreviewActor: fact.colorpicker
|
||||||
RemapIndices: 176, 178, 180, 182, 184, 186, 189, 191, 177, 179, 181, 183, 185, 187, 188, 190
|
RemapIndices: 176, 178, 180, 182, 184, 186, 189, 191, 177, 179, 181, 183, 185, 187, 188, 190
|
||||||
TeamColorPresets: f70606, ff7a22, f8d3b3, f8e947, 94b319, f335a0, a64d6c, ce08f9, f5b2db, 12b572, 502048, 1d06f7, 328dff, 78dbf8, cef6b1, 391d1d
|
PresetHues: 0, 0.125, 0.185, 0.4, 0.54, 0.66, 0.79, 0.875, 0, 0.14, 0.23, 0.43, 0.54, 0.625, 0.77, 0.85
|
||||||
|
PresetSaturations: 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.4, 0.5, 0.4, 0.5, 0.4, 0.5, 0.4, 0.5
|
||||||
|
|
||||||
EditorWorld:
|
EditorWorld:
|
||||||
Inherits: ^BaseWorld
|
Inherits: ^BaseWorld
|
||||||
|
|||||||
@@ -41,37 +41,18 @@ Background@COLOR_CHOOSER:
|
|||||||
Width: 80
|
Width: 80
|
||||||
Text: Palette
|
Text: Palette
|
||||||
Font: Bold
|
Font: Bold
|
||||||
Container@MIXER_TAB:
|
Background@MIXER_TAB:
|
||||||
|
Background: dialog3
|
||||||
X: 5
|
X: 5
|
||||||
Y: 5
|
Y: 5
|
||||||
Width: PARENT_RIGHT - 90
|
Width: PARENT_RIGHT - 90
|
||||||
Height: PARENT_BOTTOM - 34
|
Height: PARENT_BOTTOM - 34
|
||||||
Children:
|
Children:
|
||||||
Background@HUEBG:
|
ColorMixer@MIXER:
|
||||||
Background: dialog3
|
X: 2
|
||||||
X: 0
|
Y: 2
|
||||||
Y: 0
|
Width: PARENT_RIGHT - 4
|
||||||
Width: PARENT_RIGHT
|
Height: PARENT_BOTTOM - 4
|
||||||
Height: 17
|
|
||||||
Children:
|
|
||||||
HueSlider@HUE:
|
|
||||||
X: 2
|
|
||||||
Y: 2
|
|
||||||
Width: PARENT_RIGHT - 4
|
|
||||||
Height: PARENT_BOTTOM - 4
|
|
||||||
Ticks: 5
|
|
||||||
Background@MIXERBG:
|
|
||||||
Background: dialog3
|
|
||||||
X: 0
|
|
||||||
Y: 22
|
|
||||||
Width: PARENT_RIGHT
|
|
||||||
Height: PARENT_BOTTOM - 22
|
|
||||||
Children:
|
|
||||||
ColorMixer@MIXER:
|
|
||||||
X: 2
|
|
||||||
Y: 2
|
|
||||||
Width: PARENT_RIGHT - 4
|
|
||||||
Height: PARENT_BOTTOM - 4
|
|
||||||
Background@PALETTE_TAB:
|
Background@PALETTE_TAB:
|
||||||
Background: dialog3
|
Background: dialog3
|
||||||
X: 5
|
X: 5
|
||||||
|
|||||||
@@ -238,7 +238,8 @@ World:
|
|||||||
ColorPickerManager:
|
ColorPickerManager:
|
||||||
PreviewActor: carryall.colorpicker
|
PreviewActor: carryall.colorpicker
|
||||||
RemapIndices: 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240
|
RemapIndices: 255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243, 242, 241, 240
|
||||||
TeamColorPresets: 9023cd, f53333, ffae00, fff830, 87f506, f872ad, da06f3, ddb8ff, def7b2, 39c46f, 200738, 280df6, 2f86f2, 76d2f8, 498221, 392929
|
PresetHues: 0, 0.13, 0.18, 0.3, 0.475, 0.625, 0.82, 0.89, 0.97, 0.05, 0.23, 0.375, 0.525, 0.6, 0.75, 0.85
|
||||||
|
PresetSaturations: 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.5, 0.35, 0.4, 0.4, 0.5, 0.5, 0.4, 0.35
|
||||||
|
|
||||||
EditorWorld:
|
EditorWorld:
|
||||||
Inherits: ^BaseWorld
|
Inherits: ^BaseWorld
|
||||||
|
|||||||
@@ -283,7 +283,8 @@ World:
|
|||||||
ColorPickerManager:
|
ColorPickerManager:
|
||||||
PreviewActor: fact.colorpicker
|
PreviewActor: fact.colorpicker
|
||||||
RemapIndices: 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95
|
RemapIndices: 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95
|
||||||
TeamColorPresets: f7b3b3, f50606, 98331f, f57606, f7bb06, f861a4, da06f3, ddb8ff, 06f739, cef7b2, 200738, 280df6, 2f86f2, 76d2f8, 34ba93, 391d1d
|
PresetHues: 0, 0.125, 0.22, 0.375, 0.5, 0.56, 0.8, 0.88, 0, 0.15, 0.235, 0.4, 0.47, 0.55, 0.75, 0.85
|
||||||
|
PresetSaturations: 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.4, 0.5, 0.4, 0.5, 0.4, 0.5, 0.4, 0.5
|
||||||
|
|
||||||
EditorWorld:
|
EditorWorld:
|
||||||
Inherits: ^BaseWorld
|
Inherits: ^BaseWorld
|
||||||
|
|||||||
@@ -42,37 +42,18 @@ Background@COLOR_CHOOSER:
|
|||||||
Width: 80
|
Width: 80
|
||||||
Text: Palette
|
Text: Palette
|
||||||
Font: Bold
|
Font: Bold
|
||||||
Container@MIXER_TAB:
|
Background@MIXER_TAB:
|
||||||
|
Background: dialog3
|
||||||
X: 5
|
X: 5
|
||||||
Y: 5
|
Y: 5
|
||||||
Width: PARENT_RIGHT - 90
|
Width: PARENT_RIGHT - 90
|
||||||
Height: PARENT_BOTTOM - 34
|
Height: PARENT_BOTTOM - 34
|
||||||
Children:
|
Children:
|
||||||
Background@HUEBG:
|
ColorMixer@MIXER:
|
||||||
Background: dialog3
|
X: 2
|
||||||
X: 0
|
Y: 2
|
||||||
Y: 0
|
Width: PARENT_RIGHT - 4
|
||||||
Width: PARENT_RIGHT
|
Height: PARENT_BOTTOM - 4
|
||||||
Height: 17
|
|
||||||
Children:
|
|
||||||
HueSlider@HUE:
|
|
||||||
X: 2
|
|
||||||
Y: 2
|
|
||||||
Width: PARENT_RIGHT - 4
|
|
||||||
Height: PARENT_BOTTOM - 4
|
|
||||||
Ticks: 5
|
|
||||||
Background@MIXERBG:
|
|
||||||
Background: dialog3
|
|
||||||
X: 0
|
|
||||||
Y: 22
|
|
||||||
Width: PARENT_RIGHT
|
|
||||||
Height: PARENT_BOTTOM - 22
|
|
||||||
Children:
|
|
||||||
ColorMixer@MIXER:
|
|
||||||
X: 2
|
|
||||||
Y: 2
|
|
||||||
Width: PARENT_RIGHT - 4
|
|
||||||
Height: PARENT_BOTTOM - 4
|
|
||||||
Background@PALETTE_TAB:
|
Background@PALETTE_TAB:
|
||||||
Background: dialog3
|
Background: dialog3
|
||||||
X: 5
|
X: 5
|
||||||
|
|||||||
@@ -383,7 +383,8 @@ World:
|
|||||||
ColorPickerManager:
|
ColorPickerManager:
|
||||||
PreviewActor: mmch.colorpicker
|
PreviewActor: mmch.colorpicker
|
||||||
RemapIndices: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
|
RemapIndices: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
|
||||||
TeamColorPresets: f70606, ff7a22, f8d3b3, f8e947, 94b319, f335a0, a64d6c, ce08f9, f5b2db, 12b572, 4A1948, 1d06f7, 328dff, 78dbf8, cef6b1, 391d1d
|
PresetHues: 0, 0.125, 0.185, 0.4, 0.54, 0.66, 0.79, 0.875, 0, 0.14, 0.23, 0.43, 0.54, 0.625, 0.77, 0.85
|
||||||
|
PresetSaturations: 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.4, 0.5, 0.4, 0.5, 0.4, 0.5, 0.4, 0.5
|
||||||
|
|
||||||
EditorWorld:
|
EditorWorld:
|
||||||
Inherits: ^BaseWorld
|
Inherits: ^BaseWorld
|
||||||
|
|||||||
Reference in New Issue
Block a user