From 02a6912ad42eeab316eb4982a215e8272998de6a Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Wed, 9 May 2001 01:57:02 +0000 Subject: [PATCH] add then :) SVN revision: 4746 --- src/color_helpers.c | 189 ++++++++++++++++++++++++++++++++++++++++++++ src/color_helpers.h | 9 +++ 2 files changed, 198 insertions(+) create mode 100644 src/color_helpers.c create mode 100644 src/color_helpers.h diff --git a/src/color_helpers.c b/src/color_helpers.c new file mode 100644 index 0000000..0f0dc51 --- /dev/null +++ b/src/color_helpers.c @@ -0,0 +1,189 @@ +#include "color_helpers.h" +/* + * Color space conversion helper routines + * Convert between rgb and hsv adn between rgb and hls + */ + +void +__imlib_rgb_to_hsv(int r, int g, int b, float *hue, float *saturation, float *value) +{ + int f; + float i,j,k,max,min,d; + + i = ((float)r)/255.0; + j = ((float)g)/255.0; + k = ((float)b)/255.0; + + f = 0; + max = min = i; + if (j>max) { max = j; f = 1; } else min = j; + if (k>max) { max = k; f = 2; } else if (kmax) { max = j; f = 1; } else min = j; + if (k>max) { max = k; f = 2; } else if (k360) h -= 360; else if (h<0) h += 360; + if (h<60) + *r = (int)(255.0 * (m1 + m21 * h/60.0)); + else if (h<180) + *r = (int)(255.0 * m2); + else if (h<240) + *r = (int)(255.0 * (m1 + m21 * (240.0 - h)/60.0)); + else + *r = (int)(255.0 * m1); + h = hue; + if (h>360) h -= 360; else if (h<0) h += 360; + if (h<60) + *g = (int)(255.0 * (m1 + m21 * h/60.0)); + else if (h<180) + *g = (int)(255.0 * m2); + else if (h<240) + *g = (int)(255.0 * (m1 + m21 * (240.0 - h)/60.0)); + else + *g = (int)(255.0 * m1); + h = hue - 120; + if (h>360) h -= 360; else if (h<0) h += 360; + if (h<60) + *b = (int)(255.0 * (m1 + m21 * h/60.0)); + else if (h<180) + *b = (int)(255.0 * m2); + else if (h<240) + *b = (int)(255.0 * (m1 + m21 * (240.0 - h)/60.0)); + else + *b = (int)(255.0 * m1); + } +} diff --git a/src/color_helpers.h b/src/color_helpers.h new file mode 100644 index 0000000..0ed1999 --- /dev/null +++ b/src/color_helpers.h @@ -0,0 +1,9 @@ +#ifndef __COLOR_HELPERS +#define __COLOR_HELPERS 1 + +void __imlib_rgb_to_hsv(int r, int g, int b, float *hue, float *saturation, float *value); +void __imlib_hsv_to_rgb(float hue, float saturation, float value, int *r, int *g, int *b); +void __imlib_rgb_to_hls(int r, int g, int b, float *hue, float *lightness, float *saturation); +void __imlib_hls_to_rgb(float hue, float lightness, float saturation, int *r, int *g, int *b); + +#endif