import 'dart:math';

import 'package:flutter/material.dart';

extension WolfPaletteMatch on Color {
  /// Finds the index of the closest color in the wolfPalette
  int findClosestIndex(List<Color> palette) {
    int closestIndex = 0;
    double minDistance = double.infinity;

    for (int i = 0; i < palette.length; i++) {
      final Color pColor = palette[i];

      // Calculate squared Euclidean distance (skipping sqrt for performance)
      double distance =
          pow(r - pColor.r, 2).toDouble() +
          pow(g - pColor.g, 2).toDouble() +
          pow(b - pColor.b, 2).toDouble();

      if (distance < minDistance) {
        minDistance = distance;
        closestIndex = i;
      }
    }
    return closestIndex;
  }

  /// Returns the actual Color object from the palette that matches best
  Color toWolfColor(List<Color> palette) {
    return palette[findClosestIndex(palette)];
  }
}

abstract class ColorPalette {
  static const List<Color> vga = [
    Color(0xFF000000),
    Color(0xFF0000AA),
    Color(0xFF00AA00),
    Color(0xFF00AAAA),
    Color(0xFFAA0000),
    Color(0xFFAA00AA),
    Color(0xFFAA5500),
    Color(0xFFAAAAAA),
    Color(0xFF555555),
    Color(0xFF5555FF),
    Color(0xFF55FF55),
    Color(0xFF55FFFF),
    Color(0xFFFF5555),
    Color(0xFFFF55FF),
    Color(0xFFFFFF55),
    Color(0xFFFFFFFF),
    Color(0xFFEEEEEE),
    Color(0xFFDEDEDE),
    Color(0xFFD2D2D2),
    Color(0xFFC2C2C2),
    Color(0xFFB6B6B6),
    Color(0xFFAAAAAA),
    Color(0xFF999999),
    Color(0xFF8D8D8D),
    Color(0xFF7D7D7D),
    Color(0xFF717171),
    Color(0xFF656565),
    Color(0xFF555555),
    Color(0xFF484848),
    Color(0xFF383838),
    Color(0xFF2C2C2C),
    Color(0xFF202020),
    Color(0xFFFF0000),
    Color(0xFFEE0000),
    Color(0xFFE20000),
    Color(0xFFD60000),
    Color(0xFFCA0000),
    Color(0xFFBE0000),
    Color(0xFFB20000),
    Color(0xFFA50000),
    Color(0xFF990000),
    Color(0xFF890000),
    Color(0xFF7D0000),
    Color(0xFF710000),
    Color(0xFF650000),
    Color(0xFF590000),
    Color(0xFF4C0000),
    Color(0xFF400000),
    Color(0xFFFFDADA),
    Color(0xFFFFBABA),
    Color(0xFFFF9D9D),
    Color(0xFFFF7D7D),
    Color(0xFFFF5D5D),
    Color(0xFFFF4040),
    Color(0xFFFF2020),
    Color(0xFFFF0000),
    Color(0xFFFFAA5D),
    Color(0xFFFF9940),
    Color(0xFFFF8920),
    Color(0xFFFF7900),
    Color(0xFFE66D00),
    Color(0xFFCE6100),
    Color(0xFFB65500),
    Color(0xFF9D4C00),
    Color(0xFFFFFFDA),
    Color(0xFFFFFFBA),
    Color(0xFFFFFF9D),
    Color(0xFFFFFF7D),
    Color(0xFFFFFA5D),
    Color(0xFFFFF640),
    Color(0xFFFFF620),
    Color(0xFFFFF600),
    Color(0xFFE6DA00),
    Color(0xFFCEC600),
    Color(0xFFB6AE00),
    Color(0xFF9D9D00),
    Color(0xFF858500),
    Color(0xFF716D00),
    Color(0xFF595500),
    Color(0xFF404000),
    Color(0xFFD2FF5D),
    Color(0xFFC6FF40),
    Color(0xFFB6FF20),
    Color(0xFFA1FF00),
    Color(0xFF91E600),
    Color(0xFF81CE00),
    Color(0xFF75B600),
    Color(0xFF619D00),
    Color(0xFFDAFFDA),
    Color(0xFFBEFFBA),
    Color(0xFF9DFF9D),
    Color(0xFF81FF7D),
    Color(0xFF61FF5D),
    Color(0xFF40FF40),
    Color(0xFF20FF20),
    Color(0xFF00FF00),
    Color(0xFF00FF00),
    Color(0xFF00EE00),
    Color(0xFF00E200),
    Color(0xFF00D600),
    Color(0xFF04CA00),
    Color(0xFF04BE00),
    Color(0xFF04B200),
    Color(0xFF04A500),
    Color(0xFF049900),
    Color(0xFF048900),
    Color(0xFF047D00),
    Color(0xFF047100),
    Color(0xFF046500),
    Color(0xFF045900),
    Color(0xFF044C00),
    Color(0xFF044000),
    Color(0xFFDAFFFF),
    Color(0xFFBAFFFF),
    Color(0xFF9DFFFF),
    Color(0xFF7DFFFA),
    Color(0xFF5DFFFF),
    Color(0xFF40FFFF),
    Color(0xFF20FFFF),
    Color(0xFF00FFFF),
    Color(0xFF00E6E6),
    Color(0xFF00CECE),
    Color(0xFF00B6B6),
    Color(0xFF009D9D),
    Color(0xFF008585),
    Color(0xFF007171),
    Color(0xFF005959),
    Color(0xFF004040),
    Color(0xFF5DBEFF),
    Color(0xFF40B2FF),
    Color(0xFF20AAFF),
    Color(0xFF009DFF),
    Color(0xFF008DE6),
    Color(0xFF007DCE),
    Color(0xFF006DB6),
    Color(0xFF005D9D),
    Color(0xFFDADADA),
    Color(0xFFBABEFF),
    Color(0xFF9D9DFF),
    Color(0xFF7D81FF),
    Color(0xFF5D61FF),
    Color(0xFF4040FF),
    Color(0xFF2024FF),
    Color(0xFF0004FF),
    Color(0xFF0000FF),
    Color(0xFF0000EE),
    Color(0xFF0000E2),
    Color(0xFF0000D6),
    Color(0xFF0000CA),
    Color(0xFF0000BE),
    Color(0xFF0000B2),
    Color(0xFF0000A5),
    Color(0xFF000099),
    Color(0xFF000089),
    Color(0xFF00007D),
    Color(0xFF000071),
    Color(0xFF000065),
    Color(0xFF000059),
    Color(0xFF00004C),
    Color(0xFF000040),
    Color(0xFF282828),
    Color(0xFFFFE234),
    Color(0xFFFFD624),
    Color(0xFFFFCE18),
    Color(0xFFFFC208),
    Color(0xFFFFB600),
    Color(0xFFB620FF),
    Color(0xFFAA00FF),
    Color(0xFF9900E6),
    Color(0xFF8100CE),
    Color(0xFF7500B6),
    Color(0xFF61009D),
    Color(0xFF500085),
    Color(0xFF440071),
    Color(0xFF340059),
    Color(0xFF280040),
    Color(0xFFFFDAFF),
    Color(0xFFFFBAFF),
    Color(0xFFFF9DFF),
    Color(0xFFFF7DFF),
    Color(0xFFFF5DFF),
    Color(0xFFFF40FF),
    Color(0xFFFF20FF),
    Color(0xFFFF00FF),
    Color(0xFFE200E6),
    Color(0xFFCA00CE),
    Color(0xFFB600B6),
    Color(0xFF9D009D),
    Color(0xFF850085),
    Color(0xFF6D0071),
    Color(0xFF590059),
    Color(0xFF400040),
    Color(0xFFFFEADE),
    Color(0xFFFFE2D2),
    Color(0xFFFFDAC6),
    Color(0xFFFFD6BE),
    Color(0xFFFFCEB2),
    Color(0xFFFFC6A5),
    Color(0xFFFFBE9D),
    Color(0xFFFFBA91),
    Color(0xFFFFB281),
    Color(0xFFFA571F),
    Color(0xFFFF9D61),
    Color(0xFFF2955D),
    Color(0xFFEA8D59),
    Color(0xFFDE8955),
    Color(0xFFD28150),
    Color(0xFFCA7D4C),
    Color(0xFFBE7948),
    Color(0xFFB67144),
    Color(0xFFAA6940),
    Color(0xFFA1653C),
    Color(0xFF9D6138),
    Color(0xFF915D34),
    Color(0xFF895930),
    Color(0xFF81502C),
    Color(0xFF754C28),
    Color(0xFF6D4824),
    Color(0xFF5D4020),
    Color(0xFF553C1C),
    Color(0xFF483818),
    Color(0xFF403018),
    Color(0xFF382C14),
    Color(0xFF28200C),
    Color(0xFF610065),
    Color(0xFF006565),
    Color(0xFF006161),
    Color(0xFF00001C),
    Color(0xFF00002C),
    Color(0xFF302410),
    Color(0xFF480048),
    Color(0xFF500050),
    Color(0xFF000034),
    Color(0xFF1C1C1C),
    Color(0xFF4C4C4C),
    Color(0xFF5D5D5D),
    Color(0xFF404040),
    Color(0xFF303030),
    Color(0xFF343434),
    Color(0xFFDAF6F6),
    Color(0xFFBAEAEA),
    Color(0xFF9DDEDE),
    Color(0xFF75CACA),
    Color(0xFF48C2C2),
    Color(0xFF20B6B6),
    Color(0xFF20B2B2),
    Color(0xFF00A5A5),
    Color(0xFF009999),
    Color(0xFF008D8D),
    Color(0xFF008585),
    Color(0xFF007D7D),
    Color(0xFF007979),
    Color(0xFF007575),
    Color(0xFF007171),
    Color(0xFF006D6D),
    Color(0xFF990089),
  ];
}
