rgbToAnsi256 static method

int rgbToAnsi256(
  1. int red,
  2. int green,
  3. int blue
)

Implementation

static int rgbToAnsi256(int red, int green, int blue) {
  // We use the extended greyscale palette here, with the exception of
  // black and white. normal palette only has 4 greyscale shades.
  if (red == green && green == blue) {
    if (red < 8) {
      return 16;
    }

    if (red > 248) {
      return 231;
    }

    return (((red - 8) / 247) * 24).round() + 232;
  }

  return 16 +
      (36 * (red / 255 * 5).round()) +
      (6 * (green / 255 * 5).round()) +
      (blue / 255 * 5).round();
}