The file defines a static collection of color palettes for use in the game project. It provides methods to retrieve palettes by name, copy color arrays, and convert between Color32 and hexadecimal strings.
namespace PixelPusher;
public sealed class PalettePack
{
public string Name { get; init; }
public Color32[] Colors { get; init; }
}
public static class Palettes
{
public static readonly PalettePack[] Packs =
{
Pack( "CRT Night",
"#00000000", "#0b1020", "#1a2450", "#3d4d8a",
"#7a8cc8", "#c8d4f0", "#ff4d8d", "#ffd166",
"#4dffc8", "#ffffff", "#6b3a5a", "#c06040",
"#4080c0", "#20c070", "#e0e040", "#8890a0" ),
Pack( "Handheld Green",
"#00000000", "#0b140b", "#1a3318", "#2d5a28",
"#4a8a38", "#7ab848", "#c8e060", "#f0f8c0",
"#204018", "#386028", "#88c050", "#d0f078",
"#143010", "#609040", "#a0d058", "#e8f8a0" ),
Pack( "Arcade Candy",
"#00000000", "#100018", "#ff2d6a", "#ff8a3d",
"#ffe14a", "#3dff8a", "#3dd6ff", "#7a5cff",
"#ffffff", "#2a1038", "#ff6ad5", "#8aff3d",
"#4a90ff", "#ff4a4a", "#c0c0d0", "#3a2848" ),
Pack( "Dawn Pastel",
"#00000000", "#1c1420", "#3a2a38", "#f2c6c2",
"#f7d9b8", "#f4ead4", "#c8e4d4", "#b8c8e8",
"#e8b8d4", "#ffffff", "#8a6070", "#d09070",
"#70a0c0", "#80c090", "#e0d080", "#a090a8" ),
Pack( "Mono Paper",
"#00000000", "#0c0c0c", "#2a2a2a", "#4a4a4a",
"#6e6e6e", "#929292", "#b6b6b6", "#dadada",
"#f4f4f4", "#ffffff", "#1a1a1a", "#3a3a3a",
"#5a5a5a", "#808080", "#c8c8c8", "#ececec" )
};
public static PalettePack Default => Packs[0];
public static PalettePack Named( string name )
{
foreach ( var p in Packs )
{
if ( string.Equals( p.Name, name, StringComparison.OrdinalIgnoreCase ) )
return p;
}
return Default;
}
public static Color32[] Copy( PalettePack pack )
{
var src = pack.Colors;
var dst = new Color32[src.Length];
Array.Copy( src, dst, src.Length );
return dst;
}
public static string Hex( Color32 c )
{
Color col = c;
if ( col.a < 0.04f )
return "transparent";
return $"#{(int)MathF.Round( col.r * 255f ):x2}{(int)MathF.Round( col.g * 255f ):x2}{(int)MathF.Round( col.b * 255f ):x2}";
}
static PalettePack Pack( string name, params string[] hex )
{
var colors = new Color32[hex.Length];
for ( var i = 0; i < hex.Length; i++ )
colors[i] = Parse( hex[i] );
return new PalettePack { Name = name, Colors = colors };
}
public static Color32 Parse( string hex )
{
if ( string.IsNullOrWhiteSpace( hex ) )
return Color32.Transparent;
var s = hex.Trim();
if ( s[0] == '#' )
s = s[1..];
if ( s.Length == 8 )
{
var r = Convert.ToByte( s[0..2], 16 );
var g = Convert.ToByte( s[2..4], 16 );
var b = Convert.ToByte( s[4..6], 16 );
var a = Convert.ToByte( s[6..8], 16 );
return new Color32( r, g, b, a );
}
if ( s.Length == 6 )
{
var r = Convert.ToByte( s[0..2], 16 );
var g = Convert.ToByte( s[2..4], 16 );
var b = Convert.ToByte( s[4..6], 16 );
return new Color32( r, g, b, 255 );
}
return Color32.Transparent;
}
}