From 7f2adac0b474c22f16bbfaa9218ebc29f6aa248a Mon Sep 17 00:00:00 2001 From: Jan Beinke <git@janbeinke.com> Date: Thu, 11 Jun 2020 04:31:04 +0200 Subject: [PATCH] Add hsv function --- qthing/color.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ qthing/qthing.h | 1 + 2 files changed, 42 insertions(+) diff --git a/qthing/color.cpp b/qthing/color.cpp index e8d9147..8a9ccd9 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 28c50b7..fa045c5 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; -- GitLab