perf: use integers to convert hsb to rgb

Some values with new formatting are off by 1
This commit is contained in:
coltontcrowe 2021-08-21 04:05:11 +00:00
parent 6870fdc604
commit 541350ed0b

View file

@ -27,6 +27,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#define HUE_MAX 360 #define HUE_MAX 360
#define SAT_MAX 100 #define SAT_MAX 100
#define BRT_MAX 100 #define BRT_MAX 100
#define RGB_MAX 255
#define NUM_SEG 6 // Number of segments on color wheel
#define DEG_SEG (HUE_MAX / NUM_SEG) // degrees per color wheel segment
enum rgb_underglow_effect { enum rgb_underglow_effect {
UNDERGLOW_EFFECT_SOLID, UNDERGLOW_EFFECT_SOLID,
@ -55,51 +58,47 @@ static const struct device *ext_power;
#endif #endif
static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) { static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) {
double r, g, b; uint32_t i = hsb.h / DEG_SEG;
uint32_t f = hsb.h % DEG_SEG;
uint32_t v = hsb.b * RGB_MAX / BRT_MAX;
uint32_t p = v * (SAT_MAX - hsb.s) / SAT_MAX;
uint32_t q = v * (SAT_MAX * DEG_SEG - f * hsb.s) / SAT_MAX / DEG_SEG;
uint32_t t = v * (SAT_MAX * DEG_SEG - (DEG_SEG - f) * hsb.s) / SAT_MAX / DEG_SEG;
struct led_rgb rgb;
uint8_t i = hsb.h / 60; switch (i) {
double v = hsb.b / ((float)BRT_MAX);
double s = hsb.s / ((float)SAT_MAX);
double f = hsb.h / ((float)HUE_MAX) * 6 - i;
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: case 0:
r = v; rgb.r = (uint8_t)v;
g = t; rgb.g = (uint8_t)t;
b = p; rgb.b = (uint8_t)p;
break; break;
case 1: case 1:
r = q; rgb.r = (uint8_t)q;
g = v; rgb.g = (uint8_t)v;
b = p; rgb.b = (uint8_t)p;
break; break;
case 2: case 2:
r = p; rgb.r = (uint8_t)p;
g = v; rgb.g = (uint8_t)v;
b = t; rgb.b = (uint8_t)t;
break; break;
case 3: case 3:
r = p; rgb.r = (uint8_t)p;
g = q; rgb.g = (uint8_t)q;
b = v; rgb.b = (uint8_t)v;
break; break;
case 4: case 4:
r = t; rgb.r = (uint8_t)t;
g = p; rgb.g = (uint8_t)p;
b = v; rgb.b = (uint8_t)v;
break; break;
case 5: case 5:
r = v; rgb.r = (uint8_t)v;
g = p; rgb.g = (uint8_t)p;
b = q; rgb.b = (uint8_t)q;
break; break;
} }
struct led_rgb rgb = {r : r * 255, g : g * 255, b : b * 255};
return rgb; return rgb;
} }