2003-07-15 04:15:43 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
2001-01-02 02:35:09 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 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
|
2001-01-02 02:35:09 +08:00
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-01-24 02:49:44 +08:00
|
|
|
#include "gimpcolortypes.h"
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2001-01-24 02:49:44 +08:00
|
|
|
#include "gimprgb.h"
|
2001-01-02 02:35:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* RGB functions */
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgb_set (GimpRGB *rgb,
|
|
|
|
gdouble r,
|
|
|
|
gdouble g,
|
|
|
|
gdouble b)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
|
|
|
rgb->r = r;
|
|
|
|
rgb->g = g;
|
|
|
|
rgb->b = b;
|
|
|
|
}
|
|
|
|
|
2001-01-15 04:25:46 +08:00
|
|
|
void
|
|
|
|
gimp_rgb_set_alpha (GimpRGB *rgb,
|
|
|
|
gdouble a)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
|
|
|
rgb->a = a;
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:36:59 +08:00
|
|
|
void
|
|
|
|
gimp_rgb_set_uchar (GimpRGB *rgb,
|
|
|
|
guchar r,
|
|
|
|
guchar g,
|
|
|
|
guchar b)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
|
|
|
rgb->r = (gdouble) r / 255.0;
|
|
|
|
rgb->g = (gdouble) g / 255.0;
|
|
|
|
rgb->b = (gdouble) b / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgb_get_uchar (const GimpRGB *rgb,
|
|
|
|
guchar *r,
|
|
|
|
guchar *g,
|
|
|
|
guchar *b)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
2003-06-14 00:44:21 +08:00
|
|
|
if (r) *r = ROUND (CLAMP (rgb->r, 0.0, 1.0) * 255.0);
|
|
|
|
if (g) *g = ROUND (CLAMP (rgb->g, 0.0, 1.0) * 255.0);
|
|
|
|
if (b) *b = ROUND (CLAMP (rgb->b, 0.0, 1.0) * 255.0);
|
2001-01-12 07:36:59 +08:00
|
|
|
}
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
void
|
|
|
|
gimp_rgb_add (GimpRGB *rgb1,
|
|
|
|
const GimpRGB *rgb2)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgb1 != NULL);
|
|
|
|
g_return_if_fail (rgb2 != NULL);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
rgb1->r += rgb2->r;
|
|
|
|
rgb1->g += rgb2->g;
|
|
|
|
rgb1->b += rgb2->b;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-09 09:39:37 +08:00
|
|
|
gimp_rgb_subtract (GimpRGB *rgb1,
|
|
|
|
const GimpRGB *rgb2)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (rgb1 != NULL);
|
|
|
|
g_return_if_fail (rgb2 != NULL);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
rgb1->r -= rgb2->r;
|
|
|
|
rgb1->g -= rgb2->g;
|
|
|
|
rgb1->b -= rgb2->b;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-09 09:39:37 +08:00
|
|
|
gimp_rgb_multiply (GimpRGB *rgb,
|
|
|
|
gdouble factor)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
|
|
|
rgb->r *= factor;
|
|
|
|
rgb->g *= factor;
|
|
|
|
rgb->b *= factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
gdouble
|
2001-01-09 09:39:37 +08:00
|
|
|
gimp_rgb_distance (const GimpRGB *rgb1,
|
|
|
|
const GimpRGB *rgb2)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (rgb1 != NULL, 0.0);
|
|
|
|
g_return_val_if_fail (rgb2 != NULL, 0.0);
|
|
|
|
|
|
|
|
return (fabs (rgb1->r - rgb2->r) +
|
|
|
|
fabs (rgb1->g - rgb2->g) +
|
|
|
|
fabs (rgb1->b - rgb2->b));
|
|
|
|
}
|
|
|
|
|
|
|
|
gdouble
|
|
|
|
gimp_rgb_max (const GimpRGB *rgb)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (rgb != NULL, 0.0);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
if (rgb->r > rgb->g)
|
|
|
|
return (rgb->r > rgb->b) ? rgb->r : rgb->b;
|
|
|
|
return (rgb->g > rgb->b) ? rgb->g : rgb->b;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gdouble
|
|
|
|
gimp_rgb_min (const GimpRGB *rgb)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (rgb != NULL, 0.0);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
if (rgb->r < rgb->g)
|
|
|
|
return (rgb->r < rgb->b) ? rgb->r : rgb->b;
|
|
|
|
return (rgb->g < rgb->b) ? rgb->g : rgb->b;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgb_clamp (GimpRGB *rgb)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
|
|
|
rgb->r = CLAMP (rgb->r, 0.0, 1.0);
|
|
|
|
rgb->g = CLAMP (rgb->g, 0.0, 1.0);
|
|
|
|
rgb->b = CLAMP (rgb->b, 0.0, 1.0);
|
2001-01-15 04:25:46 +08:00
|
|
|
rgb->a = CLAMP (rgb->a, 0.0, 1.0);
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgb_gamma (GimpRGB *rgb,
|
|
|
|
gdouble gamma)
|
|
|
|
{
|
|
|
|
gdouble ig;
|
|
|
|
|
|
|
|
g_return_if_fail (rgb != NULL);
|
|
|
|
|
|
|
|
if (gamma != 0.0)
|
|
|
|
ig = 1.0 / gamma;
|
|
|
|
else
|
|
|
|
ig = 0.0;
|
|
|
|
|
|
|
|
rgb->r = pow (rgb->r, ig);
|
|
|
|
rgb->g = pow (rgb->g, ig);
|
|
|
|
rgb->b = pow (rgb->b, ig);
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:36:59 +08:00
|
|
|
gdouble
|
|
|
|
gimp_rgb_intensity (const GimpRGB *rgb)
|
|
|
|
{
|
2003-02-28 04:12:52 +08:00
|
|
|
gdouble intensity;
|
|
|
|
|
2001-01-12 07:36:59 +08:00
|
|
|
g_return_val_if_fail (rgb != NULL, 0.0);
|
|
|
|
|
2003-02-28 04:12:52 +08:00
|
|
|
intensity = INTENSITY (rgb->r, rgb->g, rgb->b);
|
|
|
|
|
|
|
|
return CLAMP (intensity, 0.0, 1.0);
|
2001-01-12 07:36:59 +08:00
|
|
|
}
|
2001-01-02 02:35:09 +08:00
|
|
|
|
2001-01-15 08:06:43 +08:00
|
|
|
guchar
|
|
|
|
gimp_rgb_intensity_uchar (const GimpRGB *rgb)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (rgb != NULL, 0);
|
|
|
|
|
2003-06-14 00:44:21 +08:00
|
|
|
return ROUND (gimp_rgb_intensity (rgb) * 255.0);
|
2001-01-15 08:06:43 +08:00
|
|
|
}
|
|
|
|
|
2001-01-15 04:25:46 +08:00
|
|
|
void
|
|
|
|
gimp_rgb_composite (GimpRGB *color1,
|
|
|
|
const GimpRGB *color2,
|
|
|
|
GimpRGBCompositeMode mode)
|
|
|
|
{
|
|
|
|
gdouble factor;
|
|
|
|
|
|
|
|
g_return_if_fail (color1 != NULL);
|
|
|
|
g_return_if_fail (color2 != NULL);
|
2003-07-15 04:15:43 +08:00
|
|
|
|
2001-01-15 04:25:46 +08:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case GIMP_RGB_COMPOSITE_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGB_COMPOSITE_NORMAL:
|
|
|
|
/* put color2 on top of color1 */
|
2001-01-24 20:21:50 +08:00
|
|
|
if (color2->a == 1.0)
|
|
|
|
{
|
|
|
|
*color1 = *color2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
factor = color1->a * (1.0 - color2->a);
|
|
|
|
color1->r = color1->r * factor + color2->r * color2->a;
|
|
|
|
color1->g = color1->g * factor + color2->g * color2->a;
|
|
|
|
color1->b = color1->b * factor + color2->b * color2->a;
|
|
|
|
color1->a = factor + color2->a;
|
|
|
|
}
|
2001-01-15 04:25:46 +08:00
|
|
|
break;
|
2003-07-15 04:15:43 +08:00
|
|
|
|
2001-01-15 04:25:46 +08:00
|
|
|
case GIMP_RGB_COMPOSITE_BEHIND:
|
|
|
|
/* put color2 below color1 */
|
2001-01-24 20:21:50 +08:00
|
|
|
if (color1->a < 1.0)
|
|
|
|
{
|
|
|
|
factor = color2->a * (1.0 - color1->a);
|
|
|
|
color1->r = color2->r * factor + color1->r * color1->a;
|
|
|
|
color1->g = color2->g * factor + color1->g * color1->a;
|
|
|
|
color1->b = color2->b * factor + color1->b * color1->a;
|
|
|
|
color1->a = factor + color1->a;
|
|
|
|
}
|
2001-01-15 04:25:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
/* RGBA functions */
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgba_set (GimpRGB *rgba,
|
|
|
|
gdouble r,
|
|
|
|
gdouble g,
|
|
|
|
gdouble b,
|
|
|
|
gdouble a)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgba != NULL);
|
|
|
|
|
|
|
|
rgba->r = r;
|
|
|
|
rgba->g = g;
|
|
|
|
rgba->b = b;
|
|
|
|
rgba->a = a;
|
|
|
|
}
|
|
|
|
|
2001-01-12 07:36:59 +08:00
|
|
|
void
|
|
|
|
gimp_rgba_set_uchar (GimpRGB *rgba,
|
|
|
|
guchar r,
|
|
|
|
guchar g,
|
|
|
|
guchar b,
|
|
|
|
guchar a)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgba != NULL);
|
|
|
|
|
|
|
|
rgba->r = (gdouble) r / 255.0;
|
|
|
|
rgba->g = (gdouble) g / 255.0;
|
|
|
|
rgba->b = (gdouble) b / 255.0;
|
|
|
|
rgba->a = (gdouble) a / 255.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgba_get_uchar (const GimpRGB *rgba,
|
|
|
|
guchar *r,
|
|
|
|
guchar *g,
|
|
|
|
guchar *b,
|
|
|
|
guchar *a)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgba != NULL);
|
|
|
|
|
2003-06-14 00:44:21 +08:00
|
|
|
if (r) *r = ROUND (CLAMP (rgba->r, 0.0, 1.0) * 255.0);
|
|
|
|
if (g) *g = ROUND (CLAMP (rgba->g, 0.0, 1.0) * 255.0);
|
|
|
|
if (b) *b = ROUND (CLAMP (rgba->b, 0.0, 1.0) * 255.0);
|
|
|
|
if (a) *a = ROUND (CLAMP (rgba->a, 0.0, 1.0) * 255.0);
|
2001-01-12 07:36:59 +08:00
|
|
|
}
|
|
|
|
|
2001-01-02 02:35:09 +08:00
|
|
|
void
|
|
|
|
gimp_rgba_add (GimpRGB *rgba1,
|
|
|
|
const GimpRGB *rgba2)
|
|
|
|
{
|
|
|
|
g_return_if_fail (rgba1 != NULL);
|
|
|
|
g_return_if_fail (rgba2 != NULL);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
rgba1->r += rgba2->r;
|
|
|
|
rgba1->g += rgba2->g;
|
|
|
|
rgba1->b += rgba2->b;
|
|
|
|
rgba1->a += rgba2->a;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-09 09:39:37 +08:00
|
|
|
gimp_rgba_subtract (GimpRGB *rgba1,
|
|
|
|
const GimpRGB *rgba2)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (rgba1 != NULL);
|
|
|
|
g_return_if_fail (rgba2 != NULL);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
rgba1->r -= rgba2->r;
|
|
|
|
rgba1->g -= rgba2->g;
|
|
|
|
rgba1->b -= rgba2->b;
|
|
|
|
rgba1->a -= rgba2->a;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-09 09:39:37 +08:00
|
|
|
gimp_rgba_multiply (GimpRGB *rgba,
|
|
|
|
gdouble factor)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (rgba != NULL);
|
|
|
|
|
2002-06-23 21:08:21 +08:00
|
|
|
rgba->r *= factor;
|
|
|
|
rgba->g *= factor;
|
|
|
|
rgba->b *= factor;
|
|
|
|
rgba->a *= factor;
|
2001-01-02 02:35:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gdouble
|
2003-07-15 04:15:43 +08:00
|
|
|
gimp_rgba_distance (const GimpRGB *rgba1,
|
2001-01-09 09:39:37 +08:00
|
|
|
const GimpRGB *rgba2)
|
2001-01-02 02:35:09 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (rgba1 != NULL, 0.0);
|
|
|
|
g_return_val_if_fail (rgba2 != NULL, 0.0);
|
|
|
|
|
|
|
|
return (fabs (rgba1->r - rgba2->r) + fabs (rgba1->g - rgba2->g) +
|
|
|
|
fabs (rgba1->b - rgba2->b) + fabs (rgba1->a - rgba2->a));
|
|
|
|
}
|