import 'dart:typed_data';

/// Provides the standard VGA color palette used by Wolfenstein 3D.
abstract class ColorPalette {
  /// The 256-color palette converted to 32-bit ARGB values for modern rendering.
  static final Uint32List vga32Bit = Uint32List.fromList([
    0xFF000000,
    0xFFAA0000,
    0xFF00AA00,
    0xFFAAAA00,
    0xFF0000AA,
    0xFFAA00AA,
    0xFF0055AA,
    0xFFAAAAAA,
    0xFF555555,
    0xFFFF5555,
    0xFF55FF55,
    0xFFFFFF55,
    0xFF5555FF,
    0xFFFF55FF,
    0xFF55FFFF,
    0xFFFFFFFF,
    0xFFEEEEEE,
    0xFFDEDEDE,
    0xFFD2D2D2,
    0xFFC2C2C2,
    0xFFB6B6B6,
    0xFFAAAAAA,
    0xFF999999,
    0xFF8D8D8D,
    0xFF7D7D7D,
    0xFF717171,
    0xFF656565,
    0xFF555555,
    0xFF484848,
    0xFF383838,
    0xFF2C2C2C,
    0xFF202020,
    0xFF0000FF,
    0xFF0000EE,
    0xFF0000E2,
    0xFF0000D6,
    0xFF0000CA,
    0xFF0000BE,
    0xFF0000B2,
    0xFF0000A5,
    0xFF000099,
    0xFF000089,
    0xFF00007D,
    0xFF000071,
    0xFF000065,
    0xFF000059,
    0xFF00004C,
    0xFF000040,
    0xFFDADAFF,
    0xFFBABAFF,
    0xFF9D9DFF,
    0xFF7D7DFF,
    0xFF5D5DFF,
    0xFF4040FF,
    0xFF2020FF,
    0xFF0000FF,
    0xFF5DAAFF,
    0xFF4099FF,
    0xFF2089FF,
    0xFF0079FF,
    0xFF006DE6,
    0xFF0061CE,
    0xFF0055B6,
    0xFF004C9D,
    0xFFDAFFFF,
    0xFFBAFFFF,
    0xFF9DFFFF,
    0xFF7DFFFF,
    0xFF5DFAFF,
    0xFF40F6FF,
    0xFF20F6FF,
    0xFF00F6FF,
    0xFF00DAE6,
    0xFF00C6CE,
    0xFF00AEB6,
    0xFF009D9D,
    0xFF008585,
    0xFF006D71,
    0xFF005559,
    0xFF004040,
    0xFF5DFFD2,
    0xFF40FFC6,
    0xFF20FFB6,
    0xFF00FFA1,
    0xFF00E691,
    0xFF00CE81,
    0xFF00B675,
    0xFF009D61,
    0xFFDAFFDA,
    0xFFBAFFBE,
    0xFF9DFF9D,
    0xFF7DFF81,
    0xFF5DFF61,
    0xFF40FF40,
    0xFF20FF20,
    0xFF00FF00,
    0xFF00FF00,
    0xFF00EE00,
    0xFF00E200,
    0xFF00D600,
    0xFF00CA04,
    0xFF00BE04,
    0xFF00B204,
    0xFF00A504,
    0xFF009904,
    0xFF008904,
    0xFF007D04,
    0xFF007104,
    0xFF006504,
    0xFF005904,
    0xFF004C04,
    0xFF004004,
    0xFFFFFFDA,
    0xFFFFFFBA,
    0xFFFFFF9D,
    0xFFFAFF7D,
    0xFFFFFF5D,
    0xFFFFFF40,
    0xFFFFFF20,
    0xFFFFFF00,
    0xFFE6E600,
    0xFFCECE00,
    0xFFB6B600,
    0xFF9D9D00,
    0xFF858500,
    0xFF717100,
    0xFF595900,
    0xFF404000,
    0xFFBE5D,
    0xFFB240,
    0xFFAA20,
    0xFF9D00,
    0xFFE68D00,
    0xFFCE7D00,
    0xFFB66D00,
    0xFF9D5D00,
    0xFFDADADA,
    0xFFBEBA,
    0xFF9D9D,
    0xFF817D,
    0xFF615D,
    0xFF4040,
    0xFF2420,
    0xFF0400,
    0xFF0000,
    0xFFEE0000,
    0xFFE20000,
    0xFFD60000,
    0xFFCA0000,
    0xFFBE0000,
    0xFFB20000,
    0xFFA50000,
    0xFF990000,
    0xFF890000,
    0xFF7D0000,
    0xFF710000,
    0xFF650000,
    0xFF590000,
    0xFF4C0000,
    0xFF400000,
    0xFF282828,
    0xFF34E2FF,
    0xFF24D6FF,
    0xFF18CEFF,
    0xFF08C2FF,
    0xFF00B6FF,
    0xFF20B6,
    0xFF00AA,
    0xFFE60099,
    0xFFCE0081,
    0xFFB60075,
    0xFF9D0061,
    0xFF850050,
    0xFF710044,
    0xFF590034,
    0xFF400028,
    0xFFDAFF,
    0xFFBAFF,
    0xFF9DFF,
    0xFF7DFF,
    0xFF5DFF,
    0xFF40FF,
    0xFF20FF,
    0xFF00FF,
    0xFFE600E2,
    0xFFCE00CA,
    0xFFB600B6,
    0xFF9D009D,
    0xFF850085,
    0xFF71006D,
    0xFF590059,
    0xFF400040,
    0xFFDEEAFF,
    0xFFD2E2FF,
    0xFFC6DAFF,
    0xFFBED6FF,
    0xFFB2CEFF,
    0xFFA5C6FF,
    0xFF9DBEFF,
    0xFF91BAFF,
    0xFF81B2FF,
    0xFF1F57FA,
    0xFF619DFF,
    0xFF5D95F2,
    0xFF598DEA,
    0xFF5589DE,
    0xFF5081D2,
    0xFF4C7DCA,
    0xFF4879BE,
    0xFF4471B6,
    0xFF4069AA,
    0xFF3C65A1,
    0xFF38619D,
    0xFF345D91,
    0xFF305989,
    0xFF2C5081,
    0xFF284C75,
    0xFF24486D,
    0xFF20405D,
    0xFF1C3C55,
    0xFF183848,
    0xFF183040,
    0xFF142C38,
    0xFF0C2028,
    0xFF650061,
    0xFF656500,
    0xFF616100,
    0xFF1C0000,
    0xFF2C0000,
    0xFF102430,
    0xFF480048,
    0xFF500050,
    0xFF340000,
    0xFF1C1C1C,
    0xFF4C4C4C,
    0xFF5D5D5D,
    0xFF404040,
    0xFF303030,
    0xFF343434,
    0xFFF6F6DA,
    0xFFEAEABA,
    0xFFDEDED9,
    0xFFCACA75,
    0xFFC2C248,
    0xFFB6B620,
    0xFFB2B220,
    0xFFA5A500,
    0xFF999900,
    0xFF8D8D00,
    0xFF858500,
    0xFF7D7D00,
    0xFF797900,
    0xFF757500,
    0xFF717100,
    0xFF6D6D00,
    0xFF890099,
  ]);

  static int findClosestPaletteIndex(int argb) {
    final int targetR = (argb >> 16) & 0xFF;
    final int targetG = (argb >> 8) & 0xFF;
    final int targetB = argb & 0xFF;

    int bestIndex = 0;
    int bestDistance = 1 << 30;

    for (int i = 0; i < 256; i++) {
      final int color = vga32Bit[i];
      final int r = color & 0xFF;
      final int g = (color >> 8) & 0xFF;
      final int b = (color >> 16) & 0xFF;

      final int dr = r - targetR;
      final int dg = g - targetG;
      final int db = b - targetB;
      final int distance = dr * dr + dg * dg + db * db;

      if (distance < bestDistance) {
        bestDistance = distance;
        bestIndex = i;
      }
    }

    return bestIndex;
  }
}
