Skip to content

Commit

Permalink
greatly improve rgbw desaturated color rendering
Browse files Browse the repository at this point in the history
jackw01 committed Jan 24, 2021
1 parent f35f583 commit bc3e76f
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions ledcontrol/driver/led_render.h
Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ uint32_t render_hsv2rgb_rainbow_float(color_hsv_float hsv,
w = 255;
} else {
uint8_t desat = 255 - sat;
desat = scale_8(scale_8(desat, desat), desat);
desat = scale_8(desat, desat);
r = scale_8(r, sat);
g = scale_8(g, sat);
b = scale_8(b, sat);
@@ -232,30 +232,42 @@ uint32_t render_rgb_float(color_rgb_float rgb,
float g = clamp(rgb.g, 0, 1);
float b = clamp(rgb.b, 0, 1);
float w = 0;

// If saturation is not 1, desaturate the color
// Moves r/g/b values closer to their average
// Not sure if this is the technically correct way but it seems to work?
if (saturation < 1) {
float v = (r + g + b) / 3.0;
if (saturation == 0) {
r = v;
b = v;
g = v;
} else {
r = (r - v) * saturation + v;
g = (g - v) * saturation + v;
b = (b - v) * saturation + v;
}
}
uint8_t sat = saturation * 255.0;

if (has_white) {
float min = r < g ? (r < b ? r : b) : (g < b ? g : b);
min *= min;
float max = r > g ? (r > b ? r : b) : (g > b ? g : b);
float min;
if (sat == 0) {
r = 0;
g = 0;
b = 0;
min = max;
} else {
r = (r - max) * saturation + max;
g = (g - max) * saturation + max;
b = (b - max) * saturation + max;
min = r < g ? (r < b ? r : b) : (g < b ? g : b);
r -= min;
g -= min;
b -= min;
w = min;
}
w = min * min;
} else {
// If saturation is not 1, desaturate the color
// Moves r/g/b values closer to their average
// Not sure if this is the technically correct way but it seems to work?
if (sat != 255) {
float v = (r + g + b) / 3.0;
if (sat == 0) {
r = v;
b = v;
g = v;
} else {
r = (r - v) * saturation + v;
g = (g - v) * saturation + v;
b = (b - v) * saturation + v;
}
}
}

/*

0 comments on commit bc3e76f

Please sign in to comment.