From a8dff0a7de4c20395882bc178b28987c21bdeb06 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 20 Mar 2021 17:35:04 -0500 Subject: [PATCH] Update PwmPin.cpp --- src/extras/PwmPin.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/extras/PwmPin.cpp b/src/extras/PwmPin.cpp index 5b58a9f..957f3c2 100644 --- a/src/extras/PwmPin.cpp +++ b/src/extras/PwmPin.cpp @@ -129,6 +129,8 @@ LedPin::LedPin(uint8_t pin, uint8_t level){ set(level); } +/////////////////// + void LedPin::set(uint8_t level){ if(!enabled) return; @@ -140,6 +142,63 @@ void LedPin::set(uint8_t level){ } +/////////////////// + +void LedPin::HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ){ + + // The algorithm below was provided on the web at https://www.cs.rit.edu/~ncs/color/t_convert.html + // h = [0,360] + // s = [0,1] + // v = [0,1] + + int i; + float f, p, q, t; + + if( s == 0 ){ + *r = *g = *b = v; + return; + } + + h /= 60; + i = floor( h ) ; + f = h - i; + p = v * ( 1 - s ); + q = v * ( 1 - s * f ); + t = v * ( 1 - s * ( 1 - f ) ); + switch( i % 6 ) { + case 0: + *r = v; + *g = t; + *b = p; + break; + case 1: + *r = q; + *g = v; + *b = p; + break; + case 2: + *r = p; + *g = v; + *b = t; + break; + case 3: + *r = p; + *g = q; + *b = v; + break; + case 4: + *r = t; + *g = p; + *b = v; + break; + case 5: + *r = v; + *g = p; + *b = q; + break; + } +} + //////////////////////////// ServoPin::ServoPin(uint8_t channel, uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees){