Skip to content

Commit

Permalink
0.1.2 add RGB565 16 bit (#2)
Browse files Browse the repository at this point in the history
* 0.1.2 add RGB565 16 bit + some refactor
  • Loading branch information
RobTillaart committed Jun 2, 2021
1 parent 47f6c21 commit e74767d
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 67 deletions.
53 changes: 40 additions & 13 deletions Kelvin2RGB.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
//
// FILE: Kelvin2RGB.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.2
// DATE: 2018-01-31
// PURPOSE: Arduino library for converting temperature to RGB values
// URL: https://github.com/RobTillaart/Kelvin2RGB

// HISTORY
// 0.1.0 2018-01-31 initial version
// 0.1.1 2020-12-30 ??
// 0.1.2 2021-06-01 add RGB565() - 16 bit color - output


#include "Kelvin2RGB.h"

#define DIVIDE_255 0.0039215686274509803921568627451

#define DIVIDE_255 (0.0039215686274509803921568627451)


Kelvin2RGB::Kelvin2RGB()
{
Expand All @@ -19,6 +27,7 @@ Kelvin2RGB::Kelvin2RGB()
_blue = 0;
}


void Kelvin2RGB::begin()
{
}
Expand All @@ -30,7 +39,7 @@ void Kelvin2RGB::convert_TH(float temperature, float brightness)
_temperature = constrain(temperature, 0, 65500);
_brightness = constrain(brightness, 0, 100);

_red = _green = _blue = 0;
_red = _green = _blue = 0;
float t = _temperature * 0.01;

if (t <= 66)
Expand All @@ -49,16 +58,8 @@ void Kelvin2RGB::convert_TH(float temperature, float brightness)
_green = 288.1221695283 * pow(t - 60, -0.0755148492);
_blue = 255;
}
float f = 0.01 * _brightness;
_red = constrain(f * _red, 0, 255);
_green = constrain(f * _green, 0, 255);
_blue = constrain(f * _blue, 0, 255);
_rgb = round(_red) * 65536UL + round(_green) * 256UL + round(_blue);

// divide by 255 to get factors between 0..1
_red *= DIVIDE_255;
_green *= DIVIDE_255;
_blue *= DIVIDE_255;

_normalize();
}


Expand Down Expand Up @@ -92,10 +93,35 @@ void Kelvin2RGB::convert_NB(float temperature, float brightness)
_blue = 255;
}

_normalize();
}


// 16 bit color
uint16_t Kelvin2RGB::RGB565()
{
uint16_t val = 0;
val = uint8_t(_red * 32);
val <<= 6;
val |= uint8_t(_green * 64);
val <<= 5;
val |= uint8_t(_blue * 32);
return val;
}


/////////////////////////////////////////////////////////////
//
// private
//
void Kelvin2RGB::_normalize()
{
float f = 0.01 * _brightness;

_red = constrain(f * _red, 0, 255);
_green = constrain(f * _green, 0, 255);
_blue = constrain(f * _blue, 0, 255);

_rgb = round(_red) * 65536UL + round(_green) * 256UL + round(_blue);

// divide by 255 to get factors between 0..1
Expand All @@ -104,4 +130,5 @@ void Kelvin2RGB::convert_NB(float temperature, float brightness)
_blue *= DIVIDE_255;
}


// -- END OF FILE --
65 changes: 20 additions & 45 deletions Kelvin2RGB.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,24 @@
//
// FILE: Kelvin2RGB.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// DATE: 2018-01-31
// PURPOSE: Arduino library for converting temperature to RGB values
// URL: https://github.com/RobTillaart/Kelvin2RGB
//
// Based upon article Tanner Helland and Neil Bartlett
// http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
// http://www.zombieprototypes.com/?p=210
// https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting

#define KELVIN2RGB_LIB_VERSION "0.1.1"

#include "Arduino.h"
#define KELVIN2RGB_LIB_VERSION (F("0.1.2"))

// based on https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting
// TODO a memory efficient storage -> uint8_t 17 .. 255 (factor 100)
// how? hash function? parameter settings convert(dayLightSetting)
// enum DLS ?
#include "Arduino.h"

/*
NAME TEMPERATURE
=============================
dark = 0;
match = 1700;
sodiumLamp = 1700;
candleFlame = 1850;
sunrise = 1850;
sunset = 1850;
bulb = 2400;
bulbSoftWhite = 2550;
LEDlamp = 2700;
warmWhite = 3000;
studioLight = 3200;
studioCPlight = 3350;
daylightHorizon = 5000;
flashLight = 5700;
xenonLight = 6200;
dayLightBright = 6500;
normal = 6500;
screenlow = 6500;
screenMed = 8000;
screenHigh = 9500;
polewardSky0 = 15000;
polewardSky1 = 19000;
polewardSky2 = 23000;
polewardSky3 = 27000;
*/

//
// Based upon article Tanner Helland and Neil Bartlett
// http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
// http://www.zombieprototypes.com/?p=210
// https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting
//
//
// based on https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting
//
Expand Down Expand Up @@ -86,21 +56,26 @@ class Kelvin2RGB
{
public:
Kelvin2RGB();
void begin();
void begin(); // empty function - obsolete?

// temp = 0..65500 brightness = 0.0 .. 100.0%
void convert_TH(float temperature, float brightness = 100);
void convert_NB(float temperature, float brightness = 100);

float temperature() { return _temperature; };
float brightness() { return _brightness; };
inline float red() { return _red; };
inline float green() { return _green; };
inline float blue() { return _blue; };
uint32_t RGB() { return _rgb; }


// returns 0.0 .. 1.0
float red() { return _red; };
float green() { return _green; };
float blue() { return _blue; };

uint32_t RGB() { return _rgb; }; // 32 bit color
uint16_t RGB565(); // 16 bit color

private:
void _normalize();

float _temperature;
float _brightness;
float _red;
Expand Down
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

# Kelvin2RGB

Arduino library for converting temperature to RGB values
Arduino library for converting temperature and brightness to RGB values


## Credentials

Expand All @@ -17,19 +18,21 @@ http://www.zombieprototypes.com/?p=210
https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting

There are more approximation formulas, some claim to be better,
however these are not investigated.
however these are not investigated. On request these can be added.


## Description

The library converts a temperature in Kelvin and a brightness (0..100%)
to 3 numbers red, green and blue.
These numbers are weights can be used to correct a colorimage for virtual white temperature.

There are 2 convert functions where the **convert_NB()** is claimed to be the
more accurate one.
There are 2 convert functions where the **convert_NB()** is claimed to be
the more accurate one.

With the numbers R,G,B calculated one can convert images so they will look
more like taken with candle light, sunrise or sunset etc.

With the numbers R,G,B calculated one can convert images so they will look more like
taken with candle light, sunrise or sunset etc.

**pseudo code**
```cpp
Expand Down Expand Up @@ -75,11 +78,20 @@ note this is different from Helland / Bartlett who both use an integer value 0 .
- **float blue()** returns blue channel weight 0.0 .. 1.0
- **uint32_t RGB()** retuns a 24 bit RGB value,
more efficient than 3 floats for communication.
- **uint16_t RGB565()** retuns a 16 bit RGB value,
5 bits for R 6 for green and 5 for blue.


## future
## Future

- separate brightness per color channel to mimic "artificial illumination"
- define constants like candleLight as parameter.
- investigate other formulas.
- investigate usability for RGB ledstrip.
- remove begin() ?
- add reset() ?
- add BGR() ?
- add CMYK() ?


## Operations
Expand Down
48 changes: 48 additions & 0 deletions examples/Kelvin2RGB565_diff_hex/Kelvin2RGB565_diff_hex.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// FILE: Kelvin2RGB565_diff_hex.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: demo - shows difference between 2 convert functions for the 565 16 bit coding
// DATE: 2021-06-01
// URL: https://github.com/RobTillaart/Kelvin2RGB
//

// shows difference between the two calculations
// best viewed in plotter.

#include "Kelvin2RGB.h"

Kelvin2RGB KRGB;
Kelvin2RGB KRGB2;

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

KRGB.begin();
KRGB2.begin();

test_difference();
}

void loop()
{
}

void test_difference()
{
float bright = 100.0;
for (uint32_t temp = 0; temp < 70000; temp += 200)
{
KRGB.convert_TH(temp, bright);
KRGB2.convert_NB(temp, bright);

Serial.print(KRGB.RGB565(), HEX);
Serial.print("\t");
Serial.print(KRGB2.RGB565(), HEX);
Serial.print("\n");
}
}

// -- END OF FILE --
48 changes: 48 additions & 0 deletions examples/Kelvin2RGB_diff_hex/Kelvin2RGB_diff_hex.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// FILE: Kelvin2RGB_diff_hex.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: demo - shows difference between 2 convert functions for the 565 16 bit coding
// DATE: 2021-06-01
// URL: https://github.com/RobTillaart/Kelvin2RGB
//

// shows difference between the two calculations
// best viewed in plotter.

#include "Kelvin2RGB.h"

Kelvin2RGB KRGB;
Kelvin2RGB KRGB2;

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);

KRGB.begin();
KRGB2.begin();

test_difference();
}

void loop()
{
}

void test_difference()
{
float bright = 100.0;
for (uint32_t temp = 0; temp < 70000; temp += 200)
{
KRGB.convert_TH(temp, bright);
KRGB2.convert_NB(temp, bright);

Serial.print(KRGB.RGB(), HEX);
Serial.print("\t");
Serial.print(KRGB2.RGB(), HEX);
Serial.print("\n");
}
}

// -- END OF FILE --
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ red KEYWORD2
green KEYWORD2
blue KEYWORD2
RGB KEYWORD2
RGB565 KEYWORD2

# Constants (LITERAL1)

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Kelvin2RGB.git"
},
"version": "0.1.1",
"version": "0.1.2",
"license": "MIT",
"frameworks": "*",
"platforms": "*"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Kelvin2RGB
version=0.1.1
version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for converting temperature to RGB values
Expand Down

0 comments on commit e74767d

Please sign in to comment.