From 2180a4126ee03e57e4a35ce08c9d9884000d58b2 Mon Sep 17 00:00:00 2001 From: <> Date: Sat, 18 Mar 2017 00:49:05 -0700 Subject: [PATCH] Fixed off-by one bug on hsl to rgb conversion --- includes/colors.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/includes/colors.c b/includes/colors.c index 9a8f57b..69c14cd 100644 --- a/includes/colors.c +++ b/includes/colors.c @@ -33,11 +33,11 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) { (0 > src -> l && src -> l > 1)) { return; } - float hue = src -> h; float r, g, b; + float hue = src -> h; float chroma = (1 - fabs(2 * (src -> l) - 1)) * (src -> s); 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) { r = chroma; g = x; @@ -67,9 +67,9 @@ void cvt_rgb(struct Rgb *dest, const struct Hls const *src) { g = 0; b = 0; } - dest -> r = (r + m) * 255; - dest -> g = (g + m) * 255; - dest -> b = (b + m) * 255; + dest -> r = ceil((r + m) * 255); + dest -> g = ceil((g + m) * 255); + dest -> b = ceil((b + m) * 255); } void cvt_hls(struct Hls *dest, const struct Rgb const *src) { @@ -242,4 +242,21 @@ int main() { testconversion(128, 0, 128); testconversion(0, 128, 128); testconversion(0, 0, 128); -} \ No newline at end of file + 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); +} +