diff --git a/qthing/color.cpp b/qthing/color.cpp index e8d9147240c98b86fdea75daf0c536b2ea4ef204..8a9ccd95387a82994255c6b78e164188452c4408 100644 --- a/qthing/color.cpp +++ b/qthing/color.cpp @@ -27,4 +27,45 @@ namespace qthing { return RGB(h * v, 0, (1 - h) * v); } } + + RGB hsv(float hue, float saturation, float value) { + if(value <= 0) + return RGB(); + + if(saturation <= 0) + return RGB(value, value, value); + + if(saturation > 1) + saturation = 1; + + if(value > 1) + value = 1; + + hue = fmod(hue, 1); + if(hue < 0) + ++hue; + + float hh = hue * 6; + int i = (int)hh; + float ff = hh - i; + float p = value * (1.0 - saturation); + float q = value * (1.0 - (saturation * ff)); + float t = value * (1.0 - (saturation * (1.0 - ff))); + + switch(i) { + case(0): + return RGB(value, t, p); + case(1): + return RGB(q, value, p); + case(2): + return RGB(p, value, t); + case(3): + return RGB(p, q, value); + case(4): + return RGB(t, p, value); + case(5): + return RGB(value, p, q); + } + return RGB(); + } } diff --git a/qthing/qthing.h b/qthing/qthing.h index 28c50b74245ba68af6c4e15dc4d956cd5b18ea08..fa045c5fbe96ff0293ad3c2ed1f8e184da6046e1 100644 --- a/qthing/qthing.h +++ b/qthing/qthing.h @@ -228,6 +228,7 @@ namespace qthing { }; RGB hue(const float hue, const float value = 1); + RGB hsv(float hue, float staturation = 1, float value = 1); typedef std::function<led_color_t(uint8_t, uint16_t, led_color_t)> led_color_handler_t;