0

I am looking for a way to convert a color temperature to its approximate RGB color in Flutter. If you have a look at this website, a color temperature of 1800 equals some kind of orange:

enter image description here

Is there a convenient way to do this in Flutter? The website I found seems to have hardcoded the colors. Providing me with a formula would also be appreaciated.

Josip Domazet
  • 2,246
  • 2
  • 14
  • 37

1 Answers1

2

This blog has a formula available in several languages. Below is my port to Dart/Flutter.

Color colorTempToRGB(double colorTemp) {
  final temp = colorTemp / 100;

  final red = temp <= 66
      ? 255
      : (pow(temp - 60, -0.1332047592) * 329.698727446).round().clamp(0, 255);

  final green = temp <= 66
      ? (99.4708025861 * log(temp) - 161.1195681661).round().clamp(0, 255)
      : (pow(temp - 60, -0.0755148492) * 288.1221695283).round().clamp(0, 255);

  final blue = temp >= 66
      ? 255
      : temp <= 19
          ? 0
          : (138.5177312231 * log(temp - 10) - 305.0447927307)
              .round()
              .clamp(0, 255);

  return Color.fromARGB(255, red, green, blue);
}
Richard Heap
  • 48,344
  • 9
  • 130
  • 112