Remove HSLColor.

This commit is contained in:
Paul Chote
2018-12-31 08:41:23 +00:00
parent ab4a7e3558
commit 3e404f6ac2
76 changed files with 256 additions and 416 deletions

View File

@@ -228,7 +228,7 @@ namespace OpenRA
else if (fieldType == typeof(Color))
{
Color color;
if (value != null && HSLColor.TryParseRGB(value, out color))
if (value != null && Color.TryParse(value, out color))
return color;
return InvalidValueAction(value, fieldType, fieldName);
@@ -241,7 +241,7 @@ namespace OpenRA
var colors = new Color[parts.Length];
for (var i = 0; i < colors.Length; i++)
if (!HSLColor.TryParseRGB(parts[i], out colors[i]))
if (!Color.TryParse(parts[i], out colors[i]))
return InvalidValueAction(value, fieldType, fieldName);
return colors;
@@ -249,25 +249,6 @@ namespace OpenRA
return InvalidValueAction(value, fieldType, fieldName);
}
else if (fieldType == typeof(HSLColor))
{
if (value != null)
{
Color rgb;
if (HSLColor.TryParseRGB(value, out rgb))
return new HSLColor(rgb);
// Allow old HSLColor/ColorRamp formats to be parsed as HSLColor
var parts = value.Split(',');
if (parts.Length == 3 || parts.Length == 4)
return new HSLColor(
(byte)Exts.ParseIntegerInvariant(parts[0]).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[1]).Clamp(0, 255),
(byte)Exts.ParseIntegerInvariant(parts[2]).Clamp(0, 255));
}
return InvalidValueAction(value, fieldType, fieldName);
}
else if (fieldType == typeof(Hotkey))
{
Hotkey res;

View File

@@ -74,16 +74,9 @@ namespace OpenRA
var t = v.GetType();
// Color.ToString() does the wrong thing; force it to format as rgb[a] hex
if (t == typeof(Color))
{
return HSLColor.ToHexString((Color)v);
}
// HSLColor.ToString() does the wrong thing; force it to format as rgb[a] hex
if (t == typeof(HSLColor))
{
return ((HSLColor)v).ToHexString();
return ((Color)v).ToString();
}
if (t == typeof(Rectangle))

View File

@@ -12,8 +12,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Network;
using OpenRA.Primitives;
namespace OpenRA
{
@@ -155,7 +155,7 @@ namespace OpenRA
/// <summary>The faction ID, a.k.a. the faction's internal name.</summary>
public string FactionId;
public HSLColor Color;
public Color Color;
/// <summary>The team ID on start-up, or 0 if the player is not part of a team.</summary>
public int Team;

View File

@@ -1,147 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2019 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 System.Globalization;
using OpenRA.Primitives;
using OpenRA.Scripting;
namespace OpenRA.Graphics
{
public struct HSLColor : IScriptBindable
{
public readonly byte H;
public readonly byte S;
public readonly byte L;
public readonly Color RGB;
public static HSLColor FromHSV(float h, float s, float v)
{
var ll = 0.5f * (2 - s) * v;
var ss = (ll >= 1 || v <= 0) ? 0 : 0.5f * s * v / (ll <= 0.5f ? ll : 1 - ll);
return new HSLColor((byte)(255 * h), (byte)(255 * ss), (byte)(255 * ll));
}
public HSLColor(Color color)
{
RGB = color;
H = (byte)((color.GetHue() / 360.0f) * 255);
S = (byte)(color.GetSaturation() * 255);
L = (byte)(color.GetBrightness() * 255);
}
public static HSLColor FromRGB(int r, int g, int b)
{
return new HSLColor(Color.FromArgb(r, g, b));
}
public static Color RGBFromHSL(float h, float s, float l)
{
// Convert from HSL to RGB
var q = (l < 0.5f) ? l * (1 + s) : l + s - (l * s);
var p = 2 * l - q;
float[] trgb = { h + 1 / 3.0f, h, h - 1 / 3.0f };
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 Color.FromArgb((int)(rgb[0] * 255), (int)(rgb[1] * 255), (int)(rgb[2] * 255));
}
public static bool TryParseRGB(string value, out Color color)
{
color = new Color();
value = value.Trim();
if (value.Length != 6 && value.Length != 8)
return false;
byte red, green, blue, alpha = 255;
if (!byte.TryParse(value.Substring(0, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out red)
|| !byte.TryParse(value.Substring(2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out green)
|| !byte.TryParse(value.Substring(4, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out blue))
return false;
if (value.Length == 8
&& !byte.TryParse(value.Substring(6, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out alpha))
return false;
color = Color.FromArgb(alpha, red, green, blue);
return true;
}
public static bool operator ==(HSLColor me, HSLColor other)
{
// Binary floating point numbers (float, double) calculations can yield the same RGB color created by different functions with little different HSL representation
return (me.H == other.H && me.S == other.S && me.L == other.L) || me.RGB == other.RGB;
}
public static bool operator !=(HSLColor me, HSLColor other) { return !(me == other); }
public HSLColor(byte h, byte s, byte l)
{
H = h;
S = s;
L = l;
RGB = RGBFromHSL(H / 255f, S / 255f, L / 255f);
}
public void ToHSV(out float h, out float s, out float v)
{
var ll = 2 * L / 255f;
var ss = S / 255f * ((ll <= 1) ? ll : 2 - ll);
h = H / 255f;
s = (2 * ss) / (ll + ss);
v = (ll + ss) / 2;
}
public override string ToString()
{
return "{0},{1},{2}".F(H, S, L);
}
public static string ToHexString(Color color)
{
if (color.A == 255)
return color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
return color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2") + color.A.ToString("X2");
}
public string ToHexString()
{
return ToHexString(RGB);
}
public override int GetHashCode() { return H.GetHashCode() ^ S.GetHashCode() ^ L.GetHashCode(); }
public override bool Equals(object obj)
{
var o = obj as HSLColor?;
return o != null && o == this;
}
}
}

View File

@@ -25,12 +25,16 @@ namespace OpenRA.Graphics
return ramp[i];
}
public PlayerColorRemap(int[] ramp, HSLColor c, float rampFraction)
public PlayerColorRemap(int[] ramp, Color c, float rampFraction)
{
var h = c.GetHue() / 360.0f;
var s = c.GetSaturation();
var l = c.GetBrightness();
// Increase luminosity if required to represent the full ramp
var rampRange = (byte)((1 - rampFraction) * c.L);
var c1 = new HSLColor(c.H, c.S, Math.Max(rampRange, c.L)).RGB;
var c2 = new HSLColor(c.H, c.S, (byte)Math.Max(0, c.L - rampRange)).RGB;
var rampRange = (byte)((1 - rampFraction) * l);
var c1 = Color.FromAhsl(h, s, Math.Max(rampRange, l));
var c2 = Color.FromAhsl(h, s, (byte)Math.Max(0, l - rampRange));
var baseIndex = ramp[0];
var remapRamp = ramp.Select(r => r - ramp[0]);
var rampMaxIndex = ramp.Length - 1;

View File

@@ -67,7 +67,7 @@ namespace OpenRA.Graphics
debugVis = Exts.Lazy(() => world.WorldActor.TraitOrDefault<DebugVisualizations>());
}
public void UpdatePalettesForPlayer(string internalName, HSLColor color, bool replaceExisting)
public void UpdatePalettesForPlayer(string internalName, Color color, bool replaceExisting)
{
foreach (var pal in World.WorldActor.TraitsImplementing<ILoadsPlayerPalettes>())
pal.LoadPlayerPalettes(this, internalName, color, replaceExisting);

View File

@@ -9,7 +9,7 @@
*/
#endregion
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA
{
@@ -30,7 +30,7 @@ namespace OpenRA
public string Faction;
public bool LockColor = false;
public HSLColor Color = new HSLColor(0, 0, 238);
public Color Color = Color.FromAhsl(0, 0, 238);
public bool LockSpawn = false;
public int Spawn = 0;

View File

@@ -13,7 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Network
{
@@ -21,7 +21,7 @@ namespace OpenRA.Network
{
public readonly string Name;
public readonly string Fingerprint;
public readonly HSLColor Color;
public readonly Color Color;
public readonly string Faction;
public readonly int Team;
public readonly int SpawnPoint;

View File

@@ -12,7 +12,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Network
{
@@ -110,8 +110,8 @@ namespace OpenRA.Network
}
public int Index;
public HSLColor PreferredColor; // Color that the client normally uses from settings.yaml.
public HSLColor Color; // Actual color that the client is using. Usually the same as PreferredColor but can be different on maps with locked colors.
public Color PreferredColor; // Color that the client normally uses from settings.yaml.
public Color Color; // Actual color that the client is using. Usually the same as PreferredColor but can be different on maps with locked colors.
public string Faction;
public int SpawnPoint;
public string Name;

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Network
if (orderManager.LocalClient != null && client != orderManager.LocalClient && client.Team > 0 && client.Team == orderManager.LocalClient.Team)
suffix += " (Ally)";
Game.AddChatLine(client.Color.RGB, client.Name + suffix, message);
Game.AddChatLine(client.Color, client.Name + suffix, message);
}
else
Game.AddChatLine(Color.White, "(player {0})".F(clientId), message);
@@ -86,17 +86,17 @@ namespace OpenRA.Network
if (world == null)
{
if (orderManager.LocalClient != null && client.Team == orderManager.LocalClient.Team)
Game.AddChatLine(client.Color.RGB, "[Team] " + client.Name, order.TargetString);
Game.AddChatLine(client.Color, "[Team] " + client.Name, order.TargetString);
}
else
{
var player = world.FindPlayerByClient(client);
if (player != null && player.WinState == WinState.Lost)
Game.AddChatLine(client.Color.RGB, client.Name + " (Dead)", order.TargetString);
Game.AddChatLine(client.Color, client.Name + " (Dead)", order.TargetString);
else if ((player != null && world.LocalPlayer != null && player.Stances[world.LocalPlayer] == Stance.Ally) || (world.IsReplay && player != null))
Game.AddChatLine(client.Color.RGB, "[Team" + (world.IsReplay ? " " + client.Team : "") + "] " + client.Name, order.TargetString);
Game.AddChatLine(client.Color, "[Team" + (world.IsReplay ? " " + client.Team : "") + "] " + client.Name, order.TargetString);
else if ((orderManager.LocalClient != null && orderManager.LocalClient.IsObserver && client.IsObserver) || (world.IsReplay && client.IsObserver))
Game.AddChatLine(client.Color.RGB, "[Spectators] " + client.Name, order.TargetString);
Game.AddChatLine(client.Color, "[Spectators] " + client.Name, order.TargetString);
}
}

View File

@@ -296,7 +296,6 @@
<Compile Include="Map\Map.cs" />
<Compile Include="Map\MapCache.cs" />
<Compile Include="Map\MapPreview.cs" />
<Compile Include="Graphics\HSLColor.cs" />
<Compile Include="CPos.cs" />
<Compile Include="CVec.cs" />
<Compile Include="WAngle.cs" />

View File

@@ -44,7 +44,7 @@ namespace OpenRA
}
public readonly Actor PlayerActor;
public readonly HSLColor Color;
public readonly Color Color;
public readonly string PlayerName;
public readonly string InternalName;

View File

@@ -378,7 +378,7 @@ namespace OpenRA.Server
if (client.Slot != null)
SyncClientToPlayerReference(client, Map.Players.Players[client.Slot]);
else
client.Color = HSLColor.FromRGB(255, 255, 255);
client.Color = Color.White;
// Promote connection to a valid client
PreConns.Remove(newConn);

View File

@@ -14,6 +14,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA
@@ -186,9 +187,9 @@ namespace OpenRA
{
[Desc("Sets the player nickname for in-game and IRC chat.")]
public string Name = "Newbie";
public HSLColor Color = new HSLColor(75, 255, 180);
public Color Color = Color.FromAhsl(75, 255, 180);
public string LastServer = "localhost:1234";
public HSLColor[] CustomColors = { };
public Color[] CustomColors = { };
}
public class GameSettings

View File

@@ -10,6 +10,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Traits
{
@@ -26,7 +27,7 @@ namespace OpenRA.Traits
public readonly int[] RemapIndex = { };
[Desc("The fixed color to remap.")]
public readonly HSLColor Color;
public readonly Color Color;
[Desc("Luminosity range to span.")]
public readonly float Ramp = 0.05f;

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Traits
this.info = info;
}
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, HSLColor color, bool replaceExisting)
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting)
{
var basePalette = wr.Palette(info.BasePalette).Palette;
ImmutablePalette pal;

View File

@@ -10,6 +10,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Traits
{
@@ -43,7 +44,7 @@ namespace OpenRA.Traits
this.info = info;
}
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, HSLColor color, bool replaceExisting)
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting)
{
var remap = new PlayerColorRemap(info.RemapIndex, color, info.Ramp);
var pal = new ImmutablePalette(wr.Palette(info.BasePalette).Palette, remap);

View File

@@ -33,9 +33,9 @@ namespace OpenRA.Traits
this.info = info;
}
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, HSLColor color, bool replaceExisting)
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting)
{
var argb = (uint)Color.FromArgb(128, color.RGB).ToArgb();
var argb = (uint)Color.FromArgb(128, color).ToArgb();
var pal = new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => i == 0 ? 0 : argb));
wr.AddPalette(info.BaseName + playerName, pal, false, replaceExisting);
}

View File

@@ -278,7 +278,7 @@ namespace OpenRA.Traits
}
public interface ILoadsPalettes { void LoadPalettes(WorldRenderer wr); }
public interface ILoadsPlayerPalettes { void LoadPlayerPalettes(WorldRenderer wr, string playerName, HSLColor playerColor, bool replaceExisting); }
public interface ILoadsPlayerPalettes { void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color playerColor, bool replaceExisting); }
public interface IPaletteModifier { void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> b); }
public interface IPips { IEnumerable<PipType> GetPips(Actor self); }

View File

@@ -256,8 +256,7 @@ namespace OpenRA.Widgets
public static Color GetContrastColor(Color fgColor, Color bgDark, Color bgLight)
{
var fg = new HSLColor(fgColor);
return fg.RGB == Color.White || fg.L > 80 ? bgDark : bgLight;
return fgColor == Color.White || fgColor.GetBrightness() > 0.33 ? bgDark : bgLight;
}
}