2003-07-15 04:15:43 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
2000-01-03 06:30:20 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2000-01-03 09:58:43 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2003-07-15 04:15:43 +08:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-01-03 09:58:43 +08:00
|
|
|
* Library General Public License for more details.
|
2000-01-03 06:30:20 +08:00
|
|
|
*
|
2000-01-03 09:58:43 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
2000-01-03 06:30:20 +08:00
|
|
|
*/
|
|
|
|
|
2001-01-24 02:49:44 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-03-30 21:08:54 +08:00
|
|
|
#include <babl/babl.h>
|
2004-07-27 02:14:48 +08:00
|
|
|
#include <glib-object.h>
|
2000-01-03 06:30:20 +08:00
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-01-24 02:49:44 +08:00
|
|
|
#include "gimpcolortypes.h"
|
|
|
|
|
2000-02-08 04:35:13 +08:00
|
|
|
#include "gimpcolorspace.h"
|
2001-01-24 02:49:44 +08:00
|
|
|
#include "gimprgb.h"
|
|
|
|
#include "gimphsv.h"
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2000-01-03 06:30:20 +08:00
|
|
|
|
2010-06-30 01:46:37 +08:00
|
|
|
/**
|
2010-06-30 02:08:39 +08:00
|
|
|
* SECTION: gimpcolorspace
|
2010-06-30 01:46:37 +08:00
|
|
|
* @title: GimpColorSpace
|
|
|
|
* @short_description: Utility functions which convert colors between
|
|
|
|
* different color models.
|
|
|
|
*
|
|
|
|
* When programming pixel data manipulation functions you will often
|
|
|
|
* use algorithms operating on a color model different from the one
|
|
|
|
* GIMP uses. This file provides utility functions to convert colors
|
|
|
|
* between different color spaces.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2001-01-15 20:20:38 +08:00
|
|
|
#define GIMP_HSL_UNDEFINED -1.0
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
/* GimpRGB functions */
|
|
|
|
|
2003-10-14 04:56:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_rgb_to_hsv:
|
|
|
|
* @rgb: A color value in the RGB colorspace
|
2019-08-01 21:39:13 +08:00
|
|
|
* @hsv: (out caller-allocates): The value converted to the HSV colorspace
|
2003-10-17 06:03:20 +08:00
|
|
|
*
|
|
|
|
* Does a conversion from RGB to HSV (Hue, Saturation,
|
|
|
|
* Value) colorspace.
|
2003-10-14 04:56:33 +08:00
|
|
|
**/
|
2001-01-02 02:35:09 +08:00
|
|
|
void
|
2001-01-15 20:20:38 +08:00
|
|
|
gimp_rgb_to_hsv (const GimpRGB *rgb,
|
2006-04-12 18:53:28 +08:00
|
|
|
GimpHSV *hsv)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
gdouble max, min, delta;
|
|
|
|
|
|
|
|
g_return_if_fail (rgb != NULL);
|
2001-01-15 04:25:46 +08:00
|
|
|
g_return_if_fail (hsv != NULL);
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
max = gimp_rgb_max (rgb);
|
|
|
|
min = gimp_rgb_min (rgb);
|
|
|
|
|
2001-01-15 04:25:46 +08:00
|
|
|
hsv->v = max;
|
2001-01-17 03:42:37 +08:00
|
|
|
delta = max - min;
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2001-01-17 03:42:37 +08:00
|
|
|
if (delta > 0.0001)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
2001-01-15 04:25:46 +08:00
|
|
|
hsv->s = delta / max;
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
if (rgb->r == max)
|
|
|
|
{
|
2001-01-15 04:25:46 +08:00
|
|
|
hsv->h = (rgb->g - rgb->b) / delta;
|
2006-04-12 18:53:28 +08:00
|
|
|
if (hsv->h < 0.0)
|
|
|
|
hsv->h += 6.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
else if (rgb->g == max)
|
|
|
|
{
|
2001-01-15 04:25:46 +08:00
|
|
|
hsv->h = 2.0 + (rgb->b - rgb->r) / delta;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
2011-10-01 15:43:49 +08:00
|
|
|
else
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
2001-01-15 04:25:46 +08:00
|
|
|
hsv->h = 4.0 + (rgb->r - rgb->g) / delta;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
2001-01-15 04:25:46 +08:00
|
|
|
hsv->h /= 6.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hsv->s = 0.0;
|
2001-01-15 20:20:38 +08:00
|
|
|
hsv->h = 0.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
2001-01-15 04:25:46 +08:00
|
|
|
|
|
|
|
hsv->a = rgb->a;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
2003-10-14 04:56:33 +08:00
|
|
|
/**
|
|
|
|
* gimp_hsv_to_rgb:
|
|
|
|
* @hsv: A color value in the HSV colorspace
|
2019-08-01 21:39:13 +08:00
|
|
|
* @rgb: (out caller-allocates): The returned RGB value.
|
2003-10-17 06:03:20 +08:00
|
|
|
*
|
2003-10-14 04:56:33 +08:00
|
|
|
* Converts a color value from HSV to RGB colorspace
|
|
|
|
**/
|
2001-01-02 02:35:09 +08:00
|
|
|
void
|
2001-01-15 20:20:38 +08:00
|
|
|
gimp_hsv_to_rgb (const GimpHSV *hsv,
|
2006-04-12 18:53:28 +08:00
|
|
|
GimpRGB *rgb)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gdouble f, w, q, t;
|
|
|
|
|
2001-01-15 20:20:38 +08:00
|
|
|
gdouble hue;
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
g_return_if_fail (rgb != NULL);
|
2001-01-15 04:25:46 +08:00
|
|
|
g_return_if_fail (hsv != NULL);
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2001-01-15 20:20:38 +08:00
|
|
|
if (hsv->s == 0.0)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->r = hsv->v;
|
|
|
|
rgb->g = hsv->v;
|
|
|
|
rgb->b = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-01-15 20:20:38 +08:00
|
|
|
hue = hsv->h;
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2001-01-15 20:20:38 +08:00
|
|
|
if (hue == 1.0)
|
|
|
|
hue = 0.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2001-01-15 20:20:38 +08:00
|
|
|
hue *= 6.0;
|
|
|
|
|
|
|
|
i = (gint) hue;
|
|
|
|
f = hue - i;
|
2001-01-15 04:25:46 +08:00
|
|
|
w = hsv->v * (1.0 - hsv->s);
|
|
|
|
q = hsv->v * (1.0 - (hsv->s * f));
|
|
|
|
t = hsv->v * (1.0 - (hsv->s * (1.0 - f)));
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case 0:
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->r = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
rgb->g = t;
|
|
|
|
rgb->b = w;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rgb->r = q;
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->g = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
rgb->b = w;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rgb->r = w;
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->g = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
rgb->b = t;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
rgb->r = w;
|
|
|
|
rgb->g = q;
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->b = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
rgb->r = t;
|
|
|
|
rgb->g = w;
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->b = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
break;
|
|
|
|
case 5:
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->r = hsv->v;
|
2001-01-02 02:35:09 +08:00
|
|
|
rgb->g = w;
|
|
|
|
rgb->b = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-01-15 04:25:46 +08:00
|
|
|
|
|
|
|
rgb->a = hsv->a;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
2003-07-15 04:15:43 +08:00
|
|
|
|
2003-10-14 04:56:33 +08:00
|
|
|
/**
|
|
|
|
* gimp_rgb_to_hsl:
|
|
|
|
* @rgb: A color value in the RGB colorspace
|
2019-08-01 21:39:13 +08:00
|
|
|
* @hsl: (out caller-allocates): The value converted to HSL
|
2003-10-17 06:03:20 +08:00
|
|
|
*
|
2003-11-09 06:06:57 +08:00
|
|
|
* Convert an RGB color value to a HSL (Hue, Saturation, Lightness)
|
|
|
|
* color value.
|
2003-10-14 04:56:33 +08:00
|
|
|
**/
|
2001-01-02 02:35:09 +08:00
|
|
|
void
|
2001-01-15 20:20:38 +08:00
|
|
|
gimp_rgb_to_hsl (const GimpRGB *rgb,
|
2003-05-20 18:20:34 +08:00
|
|
|
GimpHSL *hsl)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
gdouble max, min, delta;
|
|
|
|
|
|
|
|
g_return_if_fail (rgb != NULL);
|
2003-05-20 18:20:34 +08:00
|
|
|
g_return_if_fail (hsl != NULL);
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
max = gimp_rgb_max (rgb);
|
|
|
|
min = gimp_rgb_min (rgb);
|
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->l = (max + min) / 2.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
if (max == min)
|
|
|
|
{
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->s = 0.0;
|
|
|
|
hsl->h = GIMP_HSL_UNDEFINED;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-20 18:20:34 +08:00
|
|
|
if (hsl->l <= 0.5)
|
|
|
|
hsl->s = (max - min) / (max + min);
|
2001-01-02 02:35:09 +08:00
|
|
|
else
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->s = (max - min) / (2.0 - max - min);
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
delta = max - min;
|
|
|
|
|
2001-01-15 20:20:38 +08:00
|
|
|
if (delta == 0.0)
|
2006-04-12 18:53:28 +08:00
|
|
|
delta = 1.0;
|
2001-01-15 20:20:38 +08:00
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
if (rgb->r == max)
|
|
|
|
{
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->h = (rgb->g - rgb->b) / delta;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
else if (rgb->g == max)
|
|
|
|
{
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->h = 2.0 + (rgb->b - rgb->r) / delta;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
2011-10-01 15:43:49 +08:00
|
|
|
else
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->h = 4.0 + (rgb->r - rgb->g) / delta;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
hsl->h /= 6.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
if (hsl->h < 0.0)
|
|
|
|
hsl->h += 1.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
2003-05-20 18:20:34 +08:00
|
|
|
|
|
|
|
hsl->a = rgb->a;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
2008-05-06 23:49:41 +08:00
|
|
|
static inline gdouble
|
2003-05-19 22:21:03 +08:00
|
|
|
gimp_hsl_value (gdouble n1,
|
|
|
|
gdouble n2,
|
|
|
|
gdouble hue)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
gdouble val;
|
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
if (hue > 6.0)
|
|
|
|
hue -= 6.0;
|
2001-01-02 02:35:09 +08:00
|
|
|
else if (hue < 0.0)
|
2003-05-20 18:20:34 +08:00
|
|
|
hue += 6.0;
|
2003-05-19 22:21:03 +08:00
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
if (hue < 1.0)
|
|
|
|
val = n1 + (n2 - n1) * hue;
|
|
|
|
else if (hue < 3.0)
|
2001-01-02 02:35:09 +08:00
|
|
|
val = n2;
|
2003-05-20 18:20:34 +08:00
|
|
|
else if (hue < 4.0)
|
|
|
|
val = n1 + (n2 - n1) * (4.0 - hue);
|
2001-01-02 02:35:09 +08:00
|
|
|
else
|
|
|
|
val = n1;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-10-14 04:56:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_hsl_to_rgb:
|
2003-10-17 06:03:20 +08:00
|
|
|
* @hsl: A color value in the HSL colorspace
|
2019-08-01 21:39:13 +08:00
|
|
|
* @rgb: (out caller-allocates): The value converted to a value
|
|
|
|
* in the RGB colorspace
|
2003-10-17 06:03:20 +08:00
|
|
|
*
|
2003-10-14 04:56:33 +08:00
|
|
|
* Convert a HSL color value to an RGB color value.
|
|
|
|
**/
|
2001-01-02 02:35:09 +08:00
|
|
|
void
|
2003-05-20 18:20:34 +08:00
|
|
|
gimp_hsl_to_rgb (const GimpHSL *hsl,
|
2006-04-12 18:53:28 +08:00
|
|
|
GimpRGB *rgb)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
2003-05-20 18:20:34 +08:00
|
|
|
g_return_if_fail (hsl != NULL);
|
2001-01-02 02:35:09 +08:00
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
if (hsl->s == 0)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
2003-05-19 22:21:03 +08:00
|
|
|
/* achromatic case */
|
2003-05-20 18:20:34 +08:00
|
|
|
rgb->r = hsl->l;
|
|
|
|
rgb->g = hsl->l;
|
|
|
|
rgb->b = hsl->l;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-19 22:21:03 +08:00
|
|
|
gdouble m1, m2;
|
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
if (hsl->l <= 0.5)
|
|
|
|
m2 = hsl->l * (1.0 + hsl->s);
|
2003-05-19 22:21:03 +08:00
|
|
|
else
|
2003-05-20 18:20:34 +08:00
|
|
|
m2 = hsl->l + hsl->s - hsl->l * hsl->s;
|
2003-05-19 22:21:03 +08:00
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
m1 = 2.0 * hsl->l - m2;
|
2003-05-19 22:21:03 +08:00
|
|
|
|
2003-05-20 18:20:34 +08:00
|
|
|
rgb->r = gimp_hsl_value (m1, m2, hsl->h * 6.0 + 2.0);
|
|
|
|
rgb->g = gimp_hsl_value (m1, m2, hsl->h * 6.0);
|
|
|
|
rgb->b = gimp_hsl_value (m1, m2, hsl->h * 6.0 - 2.0);
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
2003-05-20 18:20:34 +08:00
|
|
|
|
|
|
|
rgb->a = hsl->a;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|