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.
This commit is contained in:
RoosterDragon
2024-03-02 12:02:06 +00:00
committed by Gustas
parent 7b82d85b27
commit 5f97e2de5a
15 changed files with 35 additions and 40 deletions

View File

@@ -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;
}

View File

@@ -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++;
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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<MPos> CellTerrainColorChanged = null;
@@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Traits
public void WorldLoaded(World w, WorldRenderer wr)
{
radarTerrainLayers = w.WorldActor.TraitsImplementing<IRadarTerrainLayer>().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))

View File

@@ -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++)

View File

@@ -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();

View File

@@ -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<IRadarSignature>())
{