From 5f97e2de5af63c7a78dd9309b608362defd65d27 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 2 Mar 2024 12:02:06 +0000 Subject: [PATCH] Make Color use uint for ARGB. This is a more natural representation than int that allows removal of casts in many places that require uint. Additionally, we can change the internal representation from long to uint, making the Color struct smaller. Since arrays of colors are common, this can save on memory. --- OpenRA.Game/Graphics/Palette.cs | 8 ++++---- OpenRA.Game/Graphics/Util.cs | 4 ++-- OpenRA.Game/Primitives/Color.cs | 17 ++++++----------- OpenRA.Mods.Cnc/Traits/World/ShroudPalette.cs | 2 +- .../UtilityCommands/RemapShpCommand.cs | 4 ++-- .../SpriteLoaders/PngSheetLoader.cs | 2 +- .../Palettes/PaletteFromGimpOrJascFile.cs | 6 +++--- .../Traits/Palettes/PaletteFromGrayscale.cs | 2 +- .../Traits/Palettes/PaletteFromPng.cs | 2 +- .../Traits/Palettes/PaletteFromRGBA.cs | 2 +- .../Traits/Player/PlayerRadarTerrain.cs | 8 ++++---- OpenRA.Mods.Common/Widgets/ColorMixerWidget.cs | 2 +- OpenRA.Mods.Common/Widgets/HueSliderWidget.cs | 2 +- OpenRA.Mods.Common/Widgets/RadarWidget.cs | 12 ++++++------ OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs | 2 +- 15 files changed, 35 insertions(+), 40 deletions(-) diff --git a/OpenRA.Game/Graphics/Palette.cs b/OpenRA.Game/Graphics/Palette.cs index 97c61b3ea9..4d71d4a59a 100644 --- a/OpenRA.Game/Graphics/Palette.cs +++ b/OpenRA.Game/Graphics/Palette.cs @@ -30,7 +30,7 @@ namespace OpenRA.Graphics public static Color GetColor(this IPalette palette, int index) { - return Color.FromArgb((int)palette[index]); + return Color.FromArgb(palette[index]); } public static IPalette AsReadOnly(this IPalette palette) @@ -103,7 +103,7 @@ namespace OpenRA.Graphics : this(p) { for (var i = 0; i < Palette.Size; i++) - colors[i] = (uint)r.GetRemappedColor(this.GetColor(i), i).ToArgb(); + colors[i] = r.GetRemappedColor(this.GetColor(i), i).ToArgb(); } public ImmutablePalette(IPalette p) @@ -142,7 +142,7 @@ namespace OpenRA.Graphics public void SetColor(int index, Color color) { - colors[index] = (uint)color.ToArgb(); + colors[index] = color.ToArgb(); } public void SetFromPalette(IPalette p) @@ -153,7 +153,7 @@ namespace OpenRA.Graphics public void ApplyRemap(IPaletteRemap r) { for (var i = 0; i < Palette.Size; i++) - colors[i] = (uint)r.GetRemappedColor(this.GetColor(i), i).ToArgb(); + colors[i] = r.GetRemappedColor(this.GetColor(i), i).ToArgb(); } } } diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index 4df6d26c7c..65076727dc 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -117,7 +117,7 @@ namespace OpenRA.Graphics // Cast the data to an int array so we can copy the src data directly fixed (byte* bd = &destData[0]) { - var data = (int*)bd; + var data = (uint*)bd; var x = dest.Bounds.Left; var y = dest.Bounds.Top; @@ -195,7 +195,7 @@ namespace OpenRA.Graphics // Cast the data to an int array so we can copy the src data directly fixed (byte* bd = &destData[0]) { - var data = (int*)bd; + var data = (uint*)bd; var x = dest.Bounds.Left; var y = dest.Bounds.Top; diff --git a/OpenRA.Game/Primitives/Color.cs b/OpenRA.Game/Primitives/Color.cs index 9fc71faeb7..3d54074baa 100644 --- a/OpenRA.Game/Primitives/Color.cs +++ b/OpenRA.Game/Primitives/Color.cs @@ -17,7 +17,7 @@ namespace OpenRA.Primitives { public readonly struct Color : IEquatable, IScriptBindable { - readonly long argb; + readonly uint argb; public static Color FromArgb(int red, int green, int blue) { @@ -26,7 +26,7 @@ namespace OpenRA.Primitives public static Color FromArgb(int alpha, int red, int green, int blue) { - return new Color(((byte)alpha << 24) + ((byte)red << 16) + ((byte)green << 8) + (byte)blue); + return new Color((uint)(((byte)alpha << 24) + ((byte)red << 16) + ((byte)green << 8) + (byte)blue)); } public static Color FromAhsl(int alpha, float h, float s, float l) @@ -55,14 +55,14 @@ namespace OpenRA.Primitives return (A, h, s, v); } - Color(long argb) + Color(uint argb) { this.argb = argb; } - public int ToArgb() + public uint ToArgb() { - return (int)argb; + return argb; } public static Color FromArgb(int alpha, Color baseColor) @@ -70,14 +70,9 @@ namespace OpenRA.Primitives return FromArgb(alpha, baseColor.R, baseColor.G, baseColor.B); } - public static Color FromArgb(int argb) - { - return FromArgb((byte)(argb >> 24), (byte)(argb >> 16), (byte)(argb >> 8), (byte)argb); - } - public static Color FromArgb(uint argb) { - return FromArgb((byte)(argb >> 24), (byte)(argb >> 16), (byte)(argb >> 8), (byte)argb); + return new Color(argb); } static float SrgbToLinear(float c) diff --git a/OpenRA.Mods.Cnc/Traits/World/ShroudPalette.cs b/OpenRA.Mods.Cnc/Traits/World/ShroudPalette.cs index 7f4d290e71..73e48f4720 100644 --- a/OpenRA.Mods.Cnc/Traits/World/ShroudPalette.cs +++ b/OpenRA.Mods.Cnc/Traits/World/ShroudPalette.cs @@ -42,7 +42,7 @@ namespace OpenRA.Mods.Cnc.Traits public void LoadPalettes(WorldRenderer wr) { var c = info.Fog ? Fog : Shroud; - wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (uint)c[i % 8].ToArgb()))); + wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => c[i % 8].ToArgb()))); } static readonly Color[] Fog = new[] diff --git a/OpenRA.Mods.Cnc/UtilityCommands/RemapShpCommand.cs b/OpenRA.Mods.Cnc/UtilityCommands/RemapShpCommand.cs index 97c44a3e96..cb49ddb9e3 100644 --- a/OpenRA.Mods.Cnc/UtilityCommands/RemapShpCommand.cs +++ b/OpenRA.Mods.Cnc/UtilityCommands/RemapShpCommand.cs @@ -77,8 +77,8 @@ namespace OpenRA.Mods.Cnc.UtilityCommands static int ColorDistance(uint a, uint b) { - var ca = Color.FromArgb((int)a); - var cb = Color.FromArgb((int)b); + var ca = Color.FromArgb(a); + var cb = Color.FromArgb(b); return Math.Abs(ca.R - cb.R) + Math.Abs(ca.G - cb.G) + diff --git a/OpenRA.Mods.Common/SpriteLoaders/PngSheetLoader.cs b/OpenRA.Mods.Common/SpriteLoaders/PngSheetLoader.cs index 52aad5f2f6..113b400d1d 100644 --- a/OpenRA.Mods.Common/SpriteLoaders/PngSheetLoader.cs +++ b/OpenRA.Mods.Common/SpriteLoaders/PngSheetLoader.cs @@ -85,7 +85,7 @@ namespace OpenRA.Mods.Common.SpriteLoaders }; if (png.Palette != null) - metadata.Add(new EmbeddedSpritePalette(png.Palette.Select(x => (uint)x.ToArgb()).ToArray())); + metadata.Add(new EmbeddedSpritePalette(png.Palette.Select(x => x.ToArgb()).ToArray())); return true; } diff --git a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGimpOrJascFile.cs b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGimpOrJascFile.cs index 3c11dfad0a..222b5263dc 100644 --- a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGimpOrJascFile.cs +++ b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGimpOrJascFile.cs @@ -95,11 +95,11 @@ namespace OpenRA.Mods.Common.Traits if (i == TransparentIndex) colors[i] = 0; else if (noAlpha) - colors[i] = (uint)Color.FromArgb(r, g, b).ToArgb(); + colors[i] = Color.FromArgb(r, g, b).ToArgb(); else if (Premultiply) - colors[i] = (uint)Color.FromArgb(a, r * a / 255, g * a / 255, b * a / 255).ToArgb(); + colors[i] = Color.FromArgb(a, r * a / 255, g * a / 255, b * a / 255).ToArgb(); else - colors[i] = (uint)Color.FromArgb(a, r, g, b).ToArgb(); + colors[i] = Color.FromArgb(a, r, g, b).ToArgb(); i++; } diff --git a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs index 88252cb3e2..886f35ec4d 100644 --- a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs +++ b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs @@ -55,7 +55,7 @@ namespace OpenRA.Mods.Common.Traits if (info.Tileset != null && !string.Equals(info.Tileset, world.Map.Tileset, StringComparison.InvariantCultureIgnoreCase)) return; - wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == info.TransparentIndex) ? 0 : (uint)Color.FromArgb(255, i, i, i).ToArgb())), info.AllowModifiers); + wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == info.TransparentIndex) ? 0 : Color.FromArgb(255, i, i, i).ToArgb())), info.AllowModifiers); } } } diff --git a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromPng.cs b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromPng.cs index 5b7ac9e57e..3f8fff0e5d 100644 --- a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromPng.cs +++ b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromPng.cs @@ -58,7 +58,7 @@ namespace OpenRA.Mods.Common.Traits var colors = new uint[Palette.Size]; for (var i = 0; i < png.Palette.Length; i++) - colors[i] = (uint)png.Palette[i].ToArgb(); + colors[i] = png.Palette[i].ToArgb(); return new ImmutablePalette(colors); } diff --git a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromRGBA.cs b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromRGBA.cs index a65eb06d7d..8c148bc3c1 100644 --- a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromRGBA.cs +++ b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromRGBA.cs @@ -71,7 +71,7 @@ namespace OpenRA.Mods.Common.Traits var r = (int)(a * info.R + 0.5f).Clamp(0, 255); var g = (int)(a * info.G + 0.5f).Clamp(0, 255); var b = (int)(a * info.B + 0.5f).Clamp(0, 255); - var c = (uint)Color.FromArgb(info.A, r, g, b).ToArgb(); + var c = Color.FromArgb(info.A, r, g, b).ToArgb(); wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == info.TransparentIndex) ? 0 : c)), info.AllowModifiers); } } diff --git a/OpenRA.Mods.Common/Traits/Player/PlayerRadarTerrain.cs b/OpenRA.Mods.Common/Traits/Player/PlayerRadarTerrain.cs index bf72d6439a..3500563130 100644 --- a/OpenRA.Mods.Common/Traits/Player/PlayerRadarTerrain.cs +++ b/OpenRA.Mods.Common/Traits/Player/PlayerRadarTerrain.cs @@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Traits readonly World world; IRadarTerrainLayer[] radarTerrainLayers; - CellLayer<(int, int)> terrainColor; + CellLayer<(uint, uint)> terrainColor; readonly Shroud shroud; public event Action CellTerrainColorChanged = null; @@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Traits public void WorldLoaded(World w, WorldRenderer wr) { radarTerrainLayers = w.WorldActor.TraitsImplementing().ToArray(); - terrainColor = new CellLayer<(int, int)>(w.Map); + terrainColor = new CellLayer<(uint, uint)>(w.Map); w.AddFrameEndTask(_ => { @@ -82,9 +82,9 @@ namespace OpenRA.Mods.Common.Traits }); } - public (int Left, int Right) this[MPos uv] => terrainColor[uv]; + public (uint Left, uint Right) this[MPos uv] => terrainColor[uv]; - public static (int Left, int Right) GetColor(Map map, IRadarTerrainLayer[] radarTerrainLayers, MPos uv) + public static (uint Left, uint Right) GetColor(Map map, IRadarTerrainLayer[] radarTerrainLayers, MPos uv) { foreach (var rtl in radarTerrainLayers) if (rtl.TryGetTerrainColorPair(uv, out var c)) diff --git a/OpenRA.Mods.Common/Widgets/ColorMixerWidget.cs b/OpenRA.Mods.Common/Widgets/ColorMixerWidget.cs index 9689f647ff..26334df641 100644 --- a/OpenRA.Mods.Common/Widgets/ColorMixerWidget.cs +++ b/OpenRA.Mods.Common/Widgets/ColorMixerWidget.cs @@ -68,7 +68,7 @@ namespace OpenRA.Mods.Common.Widgets // Generate palette in HSV fixed (byte* cc = &buffer[0]) { - var c = (int*)cc; + var c = (uint*)cc; for (var v = 0; v < 256; v++) { for (var s = 0; s < 256; s++) diff --git a/OpenRA.Mods.Common/Widgets/HueSliderWidget.cs b/OpenRA.Mods.Common/Widgets/HueSliderWidget.cs index 1d12756740..ec711881db 100644 --- a/OpenRA.Mods.Common/Widgets/HueSliderWidget.cs +++ b/OpenRA.Mods.Common/Widgets/HueSliderWidget.cs @@ -36,7 +36,7 @@ namespace OpenRA.Mods.Common.Widgets { fixed (byte* cc = &buffer[0]) { - var c = (int*)cc; + var c = (uint*)cc; for (var h = 0; h < 256; h++) { *(c + 0 * 256 + h) = Color.FromAhsv(h / 255f, 1, 1).ToArgb(); diff --git a/OpenRA.Mods.Common/Widgets/RadarWidget.cs b/OpenRA.Mods.Common/Widgets/RadarWidget.cs index e459b14c2e..df80dc4518 100644 --- a/OpenRA.Mods.Common/Widgets/RadarWidget.cs +++ b/OpenRA.Mods.Common/Widgets/RadarWidget.cs @@ -22,8 +22,8 @@ namespace OpenRA.Mods.Common.Widgets { public sealed class RadarWidget : Widget, IDisposable { - public readonly int ColorFog = Color.FromArgb(128, Color.Black).ToArgb(); - public readonly int ColorShroud = Color.Black.ToArgb(); + public readonly uint ColorFog = Color.FromArgb(128, Color.Black).ToArgb(); + public readonly uint ColorShroud = Color.Black.ToArgb(); public string WorldInteractionController = null; public int AnimationLength = 5; @@ -230,7 +230,7 @@ namespace OpenRA.Mods.Common.Widgets { fixed (byte* colorBytes = &radarData[0]) { - var colors = (int*)colorBytes; + var colors = (uint*)colorBytes; if (isRectangularIsometric) { // Odd rows are shifted right by 1px @@ -249,7 +249,7 @@ namespace OpenRA.Mods.Common.Widgets void UpdateShroudCell(PPos puv) { - var color = 0; + var color = 0u; var cv = currentPlayer.Shroud.GetVisibility(puv); if (!cv.HasFlag(Shroud.CellVisibility.Explored)) color = ColorShroud; @@ -261,7 +261,7 @@ namespace OpenRA.Mods.Common.Widgets { fixed (byte* colorBytes = &radarData[0]) { - var colors = (int*)colorBytes; + var colors = (uint*)colorBytes; foreach (var iuv in world.Map.Unproject(puv)) { if (isRectangularIsometric) @@ -407,7 +407,7 @@ namespace OpenRA.Mods.Common.Widgets { fixed (byte* colorBytes = &radarData[0]) { - var colors = (int*)colorBytes; + var colors = (uint*)colorBytes; foreach (var t in world.ActorsWithTrait()) { diff --git a/OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs b/OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs index 68b2cf7fc6..928aa36e4a 100644 --- a/OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs +++ b/OpenRA.Mods.D2k/SpriteLoaders/R8Loader.cs @@ -71,7 +71,7 @@ namespace OpenRA.Mods.D2k.SpriteLoaders { var r = new PlayerColorRemap(Enumerable.Range(240, 16).ToArray(), remap); for (var i = 240; i < 256; i++) - palette[i] = (uint)r.GetRemappedColor(Color.FromArgb(palette[i]), i).ToArgb(); + palette[i] = r.GetRemappedColor(Color.FromArgb(palette[i]), i).ToArgb(); } unsafe