feat: Refactor color handling in NoGameDataScreen and ColorPalette for improved clarity and maintainability

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-24 14:58:47 +01:00
parent a08af99b6f
commit 62dca47d1d
3 changed files with 48 additions and 47 deletions
+26 -18
View File
@@ -53,26 +53,34 @@ class NoGameDataScreen extends StatelessWidget {
/// Currently selected ready version.
final GameVersion? selectedReadyVersion;
static Color _colorFromVgaIndex(int index) {
final int packed = ColorPalette.vga32Bit[index];
final int r = packed & 0xFF;
final int g = (packed >> 8) & 0xFF;
final int b = (packed >> 16) & 0xFF;
return Color((0xFF << 24) | (r << 16) | (g << 8) | b);
}
static final Color _backgroundColor = _colorFromVgaIndex(111);
static final Color _panelColor = _colorFromVgaIndex(103);
static final Color _borderColor = _colorFromVgaIndex(87);
static final Color _titleColor = _colorFromVgaIndex(
WolfMenuPalette.headerTextIndex,
static final Color _backgroundColor = Color(
ColorPalette.argbFromVgaIndex(WolfMenuPalette.backgroundIndex),
);
static final Color _bodyColor = _colorFromVgaIndex(
WolfMenuPalette.unselectedTextIndex,
static final Color _panelColor = Color(
ColorPalette.argbFromVgaIndex(WolfMenuPalette.panelIndex),
);
static final Color _borderColor = Color(
ColorPalette.argbFromVgaIndex(WolfMenuPalette.borderIndex),
);
static final Color _titleColor = Color(
ColorPalette.argbFromVgaIndex(
WolfMenuPalette.headerTextIndex,
),
);
static final Color _bodyColor = Color(
ColorPalette.argbFromVgaIndex(
WolfMenuPalette.unselectedTextIndex,
),
);
static final Color _emphasisColor = Color(
ColorPalette.argbFromVgaIndex(WolfMenuPalette.emphasisIndex),
);
static final Color _warningColor = Color(
ColorPalette.argbFromVgaIndex(WolfMenuPalette.warningIndex),
);
static final Color _mutedColor = Color(
ColorPalette.argbFromVgaIndex(WolfMenuPalette.mutedIndex),
);
static final Color _emphasisColor = _colorFromVgaIndex(10);
static final Color _warningColor = _colorFromVgaIndex(14);
static final Color _mutedColor = _colorFromVgaIndex(8);
static String _stateLabel(GameDataVersionState state) {
switch (state) {
@@ -262,6 +262,19 @@ abstract class ColorPalette {
0xFF890099,
]);
/// Converts a VGA palette index to a packed 32-bit ARGB color.
///
/// Internally, [vga32Bit] stores channel bytes in ABGR order for renderer
/// pipelines that write raw bytes directly. This helper returns standard
/// ARGB (`0xAARRGGBB`) for APIs that expect Flutter/Dart UI color packing.
static int argbFromVgaIndex(int index) {
final int packed = vga32Bit[index];
final int r = packed & 0xFF;
final int g = (packed >> 8) & 0xFF;
final int b = (packed >> 16) & 0xFF;
return (0xFF << 24) | (r << 16) | (g << 8) | b;
}
static int findClosestPaletteIndex(int argb) {
final int targetR = (argb >> 16) & 0xFF;
final int targetG = (argb >> 8) & 0xFF;
+9 -29
View File
@@ -49,6 +49,13 @@ abstract class WolfMenuPic {
/// Keep menu color choices centralized so renderers don't duplicate
/// hard-coded palette slots or RGB conversion logic.
abstract class WolfMenuPalette {
static const int backgroundIndex = 111;
static const int panelIndex = 103;
static const int borderIndex = 87;
static const int emphasisIndex = 10;
static const int warningIndex = 14;
static const int mutedIndex = 8;
static const int selectedTextIndex = 19;
static const int unselectedTextIndex = 23;
static const int disabledTextIndex = 4;
@@ -56,8 +63,8 @@ abstract class WolfMenuPalette {
static int? _cachedHeaderTextIndex;
static int get headerTextIndex =>
_cachedHeaderTextIndex ??= _nearestPaletteIndex(_headerTargetRgb);
static int get headerTextIndex => _cachedHeaderTextIndex ??=
ColorPalette.findClosestPaletteIndex(_headerTargetRgb);
static int get selectedTextColor => ColorPalette.vga32Bit[selectedTextIndex];
@@ -67,33 +74,6 @@ abstract class WolfMenuPalette {
static int get disabledTextColor => ColorPalette.vga32Bit[disabledTextIndex];
static int get headerTextColor => ColorPalette.vga32Bit[headerTextIndex];
static int _nearestPaletteIndex(int rgb) {
final int targetR = (rgb >> 16) & 0xFF;
final int targetG = (rgb >> 8) & 0xFF;
final int targetB = rgb & 0xFF;
int bestIndex = 0;
int bestDistance = 1 << 30;
for (int i = 0; i < 256; i++) {
final int color = ColorPalette.vga32Bit[i];
final int r = color & 0xFF;
final int g = (color >> 8) & 0xFF;
final int b = (color >> 16) & 0xFF;
final int dr = targetR - r;
final int dg = targetG - g;
final int db = targetB - b;
final int distance = (dr * dr) + (dg * dg) + (db * db);
if (distance < bestDistance) {
bestDistance = distance;
bestIndex = i;
}
}
return bestIndex;
}
}
/// Structured accessors for classic Wolf3D menu art.