Light Temperature to RGB converter

So, today was spent thinking about my lighting for my lounge. I wanted to be able to control the colours but not be able to make the colours garish (by mistake or design!) So, I hunted down an algorithm for converting colour temperature into RGB values. It will work from 0K up to 65,500K. There is no guarantee of accuracy though and I wouldn't use it above about 20K, but it will accept up to 65,500K

It's as simple as including the library then defining a variable with the colour temperature and the brightness (in percent.) You can then access the RGB components that will be adjusted for brightness. You can then drive your RGB LEDs with those values.

A warm light at 75% brightness

Kelvin2RGB kel(4100, 75);  //Warm light at 75% brightness
byte red = kel.Red;              //191
byte green = kel.Green;      //156
byte blue = kel.Blue;           //127

A cool light at 50% brightness

Kelvin2RGB kel(9500, 50);  //Cool light at 50% brightness
byte red = kel.Red;              //102
byte green = kel.Green;      //110
byte blue = kel.Blue;           //127

You can get the code here: Kelvin2RGB

Thank you very much Tim!
I was looking for exactly this, i think. Or at least it’s a close starting point.
I couldn’t get the Arduino example to work though, with just a bare Sparkfun Pro micro board attached (doing nothing).

Since i wasn’t interested in the serial inputs anyway, i striped it down using the simple few lines you put in the readme github file … and it worked as expected.

I only added the beginning of an opening to sensors or pots control (declaring color temp and brightness vars)
The resulting code is the following and is working well as a test module for further dev. Thanks again.


#include <Kelvin2RGB.h>

 /* declare color temperature and brightness variables */
int mycolor = 4100; // Warm light 
int mybrite = 75;   // at 75% brightness

void setup() {
  //add code if needed 

void loop() {

  Kelvin2RGB kel(mycolor, mybrite);  
  /* values resulting from the Kelvin2RGB.h (main algorythm and more code) gives the following: */
  byte red = kel.Red;             //which should return this value: 191 
  byte green = kel.Green;         //which should return this value: 156
  byte blue = kel.Blue;           //which should return this value: 127

/* temp serial print debug */
  Serial.println(" ");
  delay(2000); // so it's not too overwelming