diff --git a/.arduino-ci.yml b/.arduino-ci.yml new file mode 100644 index 0000000..ff5659b --- /dev/null +++ b/.arduino-ci.yml @@ -0,0 +1,7 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + - leonardo + - due + - zero diff --git a/.github/workflows/arduino_test_runner.yml b/.github/workflows/arduino_test_runner.yml new file mode 100644 index 0000000..476456b --- /dev/null +++ b/.github/workflows/arduino_test_runner.yml @@ -0,0 +1,13 @@ +--- +name: Arduino CI + +on: [push, pull_request] + +jobs: + arduino_ci: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: Arduino-CI/action@master + # Arduino-CI/action@v0.1.1 diff --git a/Kelvin2RGB.cpp b/Kelvin2RGB.cpp index 4a92551..08a840e 100644 --- a/Kelvin2RGB.cpp +++ b/Kelvin2RGB.cpp @@ -13,7 +13,7 @@ Kelvin2RGB::Kelvin2RGB() { _temperature = 0; - _brightness = 0; + _brightness = 0; // default = darkness _red = 0; _green = 0; _blue = 0; @@ -45,15 +45,15 @@ void Kelvin2RGB::convert_TH(float temperature, float brightness) } else { - _red = 329.698727466 * pow(t - 60, -0.1332047592); + _red = 329.698727466 * pow(t - 60, -0.1332047592); _green = 288.1221695283 * pow(t - 60, -0.0755148492); - _blue = 255; + _blue = 255; } float f = 0.01 * _brightness; - _red = f * constrain(_red, 0, 255); - _green = f * constrain(_green, 0, 255); - _blue = f * constrain(_blue, 0, 255); - _rgb = round(_red) * 65536UL + round(_green) * 256UL + round(_blue); + _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; @@ -93,10 +93,10 @@ void Kelvin2RGB::convert_NB(float temperature, float brightness) } float f = 0.01 * _brightness; - _red = f * constrain(_red, 0, 255); - _green = f * constrain(_green, 0, 255); - _blue = f * constrain(_blue, 0, 255); - _rgb = round(_red) * 65536UL + round(_green) * 256UL + round(_blue); + _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; diff --git a/Kelvin2RGB.h b/Kelvin2RGB.h index a1bbe6b..945fce0 100644 --- a/Kelvin2RGB.h +++ b/Kelvin2RGB.h @@ -2,7 +2,7 @@ // // FILE: Kelvin2RGB.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2018-01-31 // PURPOSE: Arduino library for converting temperature to RGB values // URL: https://github.com/RobTillaart/Kelvin2RGB @@ -12,16 +12,19 @@ // http://www.zombieprototypes.com/?p=210 // https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting -#define KELVIN2RGB_LIB_VERSION "0.1.0" +#define KELVIN2RGB_LIB_VERSION "0.1.1" #include "Arduino.h" // 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 ? + /* NAME TEMPERATURE ============================= + dark = 0; match = 1700; sodiumLamp = 1700; candleFlame = 1850; @@ -47,6 +50,37 @@ polewardSky3 = 27000; */ +// +// based on https://en.wikipedia.org/wiki/Color_temperature#Categorizing_different_lighting +// +// DAY LIGHT SETTING TEMPERATURE +// +#define DLS_dark 0 +#define DLS_match 1700 +#define DLS_sodiumLamp 1700 +#define DLS_candleFlame 1850 +#define DLS_sunrise 1850 +#define DLS_sunset 1850 +#define DLS_bulb 2400 +#define DLS_bulbSoftWhite 2550 +#define DLS_LEDlamp 2700 +#define DLS_warmWhite 3000 +#define DLS_studioLight 3200 +#define DLS_studioCPlight 3350 +#define DLS_daylightHorizon 5000 +#define DLS_flashLight 5700 +#define DLS_xenonLight 6200 +#define DLS_dayLightBright 6500 +#define DLS_normal 6500 +#define DLS_screenlow 6500 +#define DLS_screenMed 8000 +#define DLS_screenHigh 9500 +#define DLS_polewardSky0 15000 +#define DLS_polewardSky1 19000 +#define DLS_polewardSky2 23000 +#define DLS_polewardSky3 27000 + + class Kelvin2RGB { diff --git a/README.md b/README.md index 57d47c8..050acc6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ + +[![Arduino CI](https://github.com/RobTillaart/Kelvin2RGB/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Kelvin2RGB/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/Kelvin2RGB.svg?maxAge=3600)](https://github.com/RobTillaart/Kelvin2RGB/releases) + + # Kelvin2RGB Arduino library for converting temperature to RGB values @@ -15,7 +21,7 @@ however these are not investigated. ## Description -The library converts a temperature in Kelvin and an brightness (0..100%) +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. diff --git a/library.json b/library.json index 2963f24..d5758f9 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/Kelvin2RGB.git" }, - "version":"0.1.0", + "version":"0.1.1", "frameworks": "*", "platforms": "*" } diff --git a/library.properties b/library.properties index e4d6065..efc2e6d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Kelvin2RGB -version=0.1.0 +version=0.1.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for converting temperature to RGB values diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp new file mode 100644 index 0000000..1f291bd --- /dev/null +++ b/test/unit_test_001.cpp @@ -0,0 +1,111 @@ +// +// FILE: unit_test_001.cpp +// AUTHOR: Rob Tillaart +// DATE: 2020-12-30 +// PURPOSE: unit tests for the Kelvin2RGB library +// https://github.com/RobTillaart/Kelvin2RGB +// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md +// + +// supported assertions +// ---------------------------- +// assertEqual(expected, actual); // a == b +// assertNotEqual(unwanted, actual); // a != b +// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b)) +// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b)) +// assertLess(upperBound, actual); // a < b +// assertMore(lowerBound, actual); // a > b +// assertLessOrEqual(upperBound, actual); // a <= b +// assertMoreOrEqual(lowerBound, actual); // a >= b +// assertTrue(actual); +// assertFalse(actual); +// assertNull(actual); + +// // special cases for floats +// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon +// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon +// assertInfinity(actual); // isinf(a) +// assertNotInfinity(actual); // !isinf(a) +// assertNAN(arg); // isnan(a) +// assertNotNAN(arg); // !isnan(a) + +#include + +#define assertEqualFloat(arg1, arg2, arg3) assertOp("assertEqualFloat", "expected", fabs(arg1 - arg2), compareLessOrEqual, "<=", "actual", arg3) +// #define assertEqualINF(arg) assertOp("assertEqualINF", "expected", INFINITY, compareEqual, "==", "actual", arg) +// #define assertEqualNAN(arg) assertOp("assertEqualNAN", "expected", true, compareEqual, "==", "actual", isnan(arg)) + + +#include "Arduino.h" +#include "Kelvin2RGB.h" + + +unittest_setup() +{ +} + +unittest_teardown() +{ +} + +/* +unittest(test_new_operator) +{ + assertEqualINF(exp(800)); + assertEqualINF(0.0/0.0); + assertEqualINF(42); + + assertEqualNAN(INFINITY - INFINITY); + assertEqualNAN(0.0/0.0); + assertEqualNAN(42); +} +*/ + +unittest(test_constructor) +{ + fprintf(stderr, "VERSION: %s\n", KELVIN2RGB_LIB_VERSION); + + Kelvin2RGB tempColor; + + assertEqualFloat(0, tempColor.temperature(), 0.0001); + assertEqualFloat(0, tempColor.brightness(), 0.0001); + assertEqualFloat(0, tempColor.red(), 0.0001); + assertEqualFloat(0, tempColor.green(), 0.0001); + assertEqualFloat(0, tempColor.blue(), 0.0001); + assertEqualFloat(0, tempColor.RGB(), 0.0001); +} + + +unittest(test_Tanner_Helland) +{ + Kelvin2RGB tempColor; + + fprintf(stderr, "DLS_warmWhite\n"); + tempColor.convert_TH(DLS_warmWhite, 100); + + assertEqualFloat(3000, tempColor.temperature(), 0.0001); + assertEqualFloat(100, tempColor.brightness(), 0.0001); + assertEqualFloat(1, tempColor.red(), 0.0001); + assertEqualFloat(0.694903, tempColor.green(), 0.0001); + assertEqualFloat(0.431048, tempColor.blue(), 0.0001); + assertEqual(16757102, tempColor.RGB()); +} + +unittest(test_Neil_Bartlett) +{ + Kelvin2RGB tempColor; + + fprintf(stderr, "DLS_warmWhite\n"); + tempColor.convert_NB(DLS_warmWhite, 100); + + assertEqualFloat(3000, tempColor.temperature(), 0.0001); + assertEqualFloat(100, tempColor.brightness(), 0.0001); + assertEqualFloat(1, tempColor.red(), 0.0001); + assertEqualFloat(0.707636, tempColor.green(), 0.0001); + assertEqualFloat(0.424804, tempColor.blue(), 0.0001); + assertEqual(16757868, tempColor.RGB()); +} + +unittest_main() + +// --------