2018-05-08 19:24:18 +00:00
|
|
|
/* Copyright 2017 Jason Williams
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "color.h"
|
|
|
|
#include "led_tables.h"
|
|
|
|
#include "progmem.h"
|
2023-03-23 05:30:30 +00:00
|
|
|
#include "util.h"
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2024-10-11 23:10:02 +00:00
|
|
|
rgb_t hsv_to_rgb_impl(hsv_t hsv, bool use_cie) {
|
|
|
|
rgb_t rgb;
|
2019-08-30 18:19:03 +00:00
|
|
|
uint8_t region, remainder, p, q, t;
|
|
|
|
uint16_t h, s, v;
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
if (hsv.s == 0) {
|
2019-05-19 16:11:08 +00:00
|
|
|
#ifdef USE_CIE1931_CURVE
|
2020-08-05 05:11:06 +00:00
|
|
|
if (use_cie) {
|
|
|
|
rgb.r = rgb.g = rgb.b = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
|
|
|
|
} else {
|
|
|
|
rgb.r = hsv.v;
|
|
|
|
rgb.g = hsv.v;
|
|
|
|
rgb.b = hsv.v;
|
|
|
|
}
|
2019-05-19 16:11:08 +00:00
|
|
|
#else
|
2019-08-30 18:19:03 +00:00
|
|
|
rgb.r = hsv.v;
|
|
|
|
rgb.g = hsv.v;
|
|
|
|
rgb.b = hsv.v;
|
2019-05-19 16:11:08 +00:00
|
|
|
#endif
|
2019-08-30 18:19:03 +00:00
|
|
|
return rgb;
|
|
|
|
}
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
h = hsv.h;
|
|
|
|
s = hsv.s;
|
2019-09-19 01:32:38 +00:00
|
|
|
#ifdef USE_CIE1931_CURVE
|
2020-08-05 05:11:06 +00:00
|
|
|
if (use_cie) {
|
|
|
|
v = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
|
|
|
|
} else {
|
|
|
|
v = hsv.v;
|
|
|
|
}
|
2019-09-19 01:32:38 +00:00
|
|
|
#else
|
2019-08-30 18:19:03 +00:00
|
|
|
v = hsv.v;
|
2019-09-19 01:32:38 +00:00
|
|
|
#endif
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
region = h * 6 / 255;
|
|
|
|
remainder = (h * 2 - region * 85) * 3;
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
p = (v * (255 - s)) >> 8;
|
|
|
|
q = (v * (255 - ((s * remainder) >> 8))) >> 8;
|
|
|
|
t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
switch (region) {
|
|
|
|
case 6:
|
|
|
|
case 0:
|
|
|
|
rgb.r = v;
|
|
|
|
rgb.g = t;
|
|
|
|
rgb.b = p;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rgb.r = q;
|
|
|
|
rgb.g = v;
|
|
|
|
rgb.b = p;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rgb.r = p;
|
|
|
|
rgb.g = v;
|
|
|
|
rgb.b = t;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
rgb.r = p;
|
|
|
|
rgb.g = q;
|
|
|
|
rgb.b = v;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
rgb.r = t;
|
|
|
|
rgb.g = p;
|
|
|
|
rgb.b = v;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rgb.r = v;
|
|
|
|
rgb.g = p;
|
|
|
|
rgb.b = q;
|
|
|
|
break;
|
|
|
|
}
|
2018-05-08 19:24:18 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
return rgb;
|
2018-05-08 19:24:18 +00:00
|
|
|
}
|
2019-12-31 16:33:49 +00:00
|
|
|
|
2024-10-11 23:10:02 +00:00
|
|
|
rgb_t hsv_to_rgb(hsv_t hsv) {
|
2020-08-05 05:11:06 +00:00
|
|
|
#ifdef USE_CIE1931_CURVE
|
|
|
|
return hsv_to_rgb_impl(hsv, true);
|
|
|
|
#else
|
|
|
|
return hsv_to_rgb_impl(hsv, false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-10-11 23:10:02 +00:00
|
|
|
rgb_t hsv_to_rgb_nocie(hsv_t hsv) {
|
2022-02-12 18:29:31 +00:00
|
|
|
return hsv_to_rgb_impl(hsv, false);
|
|
|
|
}
|