import 'dart:ui';

int findClosestColorIndex(Color sourceColor) {
  int bestMatchIndex = 0;
  double smallestDistance = double.infinity; // Start with a 'large' distance

  // Iterate through possible controller colors (0-255)
  for (int i = 0; i < 256; i++) {
    // Example: Assume your controller just uses an even R/G/B scaling
    int r = (i >> 5) * 32; // Assuming an even split to get 8 values
    int g = ((i >> 2) & 0x7) * 32;
    int b = (i & 0x3) * 64;

    // Color controllerColor = Color.fromARGB(255, r, g, b);

    // Calculate distance (you can use different formulas)
    int distance = (sourceColor.red - r) * (sourceColor.red - r) +
        (sourceColor.green - g) * (sourceColor.green - g) +
        (sourceColor.blue - b) * (sourceColor.blue - b);

    if (distance < smallestDistance) {
      smallestDistance = distance.toDouble();
      bestMatchIndex = i;
    }
  }

  return bestMatchIndex;
}
