Fixed off-by one bug on hsl to rgb conversion

This commit is contained in:
2017-03-18 00:49:05 -07:00
parent d48b9e9b98
commit 2180a4126e

View File

@ -33,11 +33,11 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) {
(0 > src -> l && src -> l > 1)) { (0 > src -> l && src -> l > 1)) {
return; return;
} }
float hue = src -> h;
float r, g, b; float r, g, b;
float hue = src -> h;
float chroma = (1 - fabs(2 * (src -> l) - 1)) * (src -> s); float chroma = (1 - fabs(2 * (src -> l) - 1)) * (src -> s);
float x = chroma * (1 - fabs((int)(hue/60) % 2 - 1)); float x = chroma * (1 - fabs((int)(hue/60) % 2 - 1));
float m = (src -> l) - chroma/2; float m = (src -> l) - (chroma/2);
if (0 <= hue && hue < 60) { if (0 <= hue && hue < 60) {
r = chroma; r = chroma;
g = x; g = x;
@ -67,9 +67,9 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) {
g = 0; g = 0;
b = 0; b = 0;
} }
dest -> r = (r + m) * 255; dest -> r = ceil((r + m) * 255);
dest -> g = (g + m) * 255; dest -> g = ceil((g + m) * 255);
dest -> b = (b + m) * 255; dest -> b = ceil((b + m) * 255);
} }
void cvt_hls(struct Hls *dest, const struct Rgb const *src) { void cvt_hls(struct Hls *dest, const struct Rgb const *src) {
@ -242,4 +242,21 @@ int main() {
testconversion(128, 0, 128); testconversion(128, 0, 128);
testconversion(0, 128, 128); testconversion(0, 128, 128);
testconversion(0, 0, 128); testconversion(0, 0, 128);
testconvhls(0, 0, 0);
testconvhls(0, 0, 1);
testconvhls(0, 1, 0.5);
testconvhls(120, 1, 0.5);
testconvhls(240, 1, 0.5);
testconvhls(60, 1, 0.5);
testconvhls(180, 1, 0.5);
testconvhls(300, 1, 0.5);
testconvhls(0, 0, 0.75);
testconvhls(0, 0, 0.5);
testconvhls(0, 1, 0.25);
testconvhls(60, 1, 0.25);
testconvhls(120, 1, 0.25);
testconvhls(300, 1, 0.25);
testconvhls(180, 1, 0.25);
testconvhls(240, 1, 0.25);
} }