From e74767dca776bdb0335e49e5c603ccb0246fce68 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Wed, 2 Jun 2021 09:46:48 +0200 Subject: [PATCH] 0.1.2 add RGB565 16 bit (#2) * 0.1.2 add RGB565 16 bit + some refactor --- Kelvin2RGB.cpp | 53 +++++++++++---- Kelvin2RGB.h | 65 ++++++------------- README.md | 26 ++++++-- .../Kelvin2RGB565_diff_hex.ino | 48 ++++++++++++++ .../Kelvin2RGB_diff_hex.ino | 48 ++++++++++++++ keywords.txt | 1 + library.json | 2 +- library.properties | 2 +- 8 files changed, 178 insertions(+), 67 deletions(-) create mode 100644 examples/Kelvin2RGB565_diff_hex/Kelvin2RGB565_diff_hex.ino create mode 100644 examples/Kelvin2RGB_diff_hex/Kelvin2RGB_diff_hex.ino diff --git a/Kelvin2RGB.cpp b/Kelvin2RGB.cpp index 08a840e..6d8e00c 100644 --- a/Kelvin2RGB.cpp +++ b/Kelvin2RGB.cpp @@ -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() { @@ -19,6 +27,7 @@ Kelvin2RGB::Kelvin2RGB() _blue = 0; } + void Kelvin2RGB::begin() { } @@ -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) @@ -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(); } @@ -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 @@ -104,4 +130,5 @@ void Kelvin2RGB::convert_NB(float temperature, float brightness) _blue *= DIVIDE_255; } + // -- END OF FILE -- diff --git a/Kelvin2RGB.h b/Kelvin2RGB.h index 945fce0..69e2bb4 100644 --- a/Kelvin2RGB.h +++ b/Kelvin2RGB.h @@ -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 // @@ -86,7 +56,7 @@ 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); @@ -94,13 +64,18 @@ class Kelvin2RGB 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; diff --git a/README.md b/README.md index 050acc6..c21d638 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ # Kelvin2RGB -Arduino library for converting temperature to RGB values +Arduino library for converting temperature and brightness to RGB values + ## Credentials @@ -17,7 +18,8 @@ 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 @@ -25,11 +27,12 @@ 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 @@ -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 diff --git a/examples/Kelvin2RGB565_diff_hex/Kelvin2RGB565_diff_hex.ino b/examples/Kelvin2RGB565_diff_hex/Kelvin2RGB565_diff_hex.ino new file mode 100644 index 0000000..a2ffcb1 --- /dev/null +++ b/examples/Kelvin2RGB565_diff_hex/Kelvin2RGB565_diff_hex.ino @@ -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 -- diff --git a/examples/Kelvin2RGB_diff_hex/Kelvin2RGB_diff_hex.ino b/examples/Kelvin2RGB_diff_hex/Kelvin2RGB_diff_hex.ino new file mode 100644 index 0000000..ab058d9 --- /dev/null +++ b/examples/Kelvin2RGB_diff_hex/Kelvin2RGB_diff_hex.ino @@ -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 -- diff --git a/keywords.txt b/keywords.txt index 44bd07d..edb008f 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,6 +12,7 @@ red KEYWORD2 green KEYWORD2 blue KEYWORD2 RGB KEYWORD2 +RGB565 KEYWORD2 # Constants (LITERAL1) diff --git a/library.json b/library.json index 3a2c38f..9c64254 100644 --- a/library.json +++ b/library.json @@ -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": "*" diff --git a/library.properties b/library.properties index efc2e6d..ebbbbb7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Kelvin2RGB -version=0.1.1 +version=0.1.2 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for converting temperature to RGB values