4 * An OpenGL based 'interactive canvas' library.
6 * Authored By Matthew Allum <mallum@openedhand.com>
8 * Copyright (C) 2006 OpenedHand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
27 * SECTION:clutter-color
28 * @short_description: Color management and manipulation.
30 * #ClutterColor is a simple type for representing colors.
37 #include "clutter-color.h"
41 * @src1: a #ClutterColor
42 * @src2: a #ClutterColor
43 * @dest: return location for the result
45 * Adds @src2 to @src1 and saves the resulting color
48 * The alpha channel of @dest is as the maximum value
49 * between the alpha channels of @src1 and @src2.
52 clutter_color_add (const ClutterColor *src1,
53 const ClutterColor *src2,
56 g_return_if_fail (src1 != NULL);
57 g_return_if_fail (src2 != NULL);
58 g_return_if_fail (dest != NULL);
60 dest->red = CLAMP (src1->red + src2->red, 0, 255);
61 dest->green = CLAMP (src1->green + src2->green, 0, 255);
62 dest->blue = CLAMP (src1->blue + src2->blue, 0, 255);
64 dest->alpha = MAX (src1->alpha, src2->alpha);
68 * clutter_color_subtract:
69 * @src1: a #ClutterColor
70 * @src2: a #ClutterColor
71 * @dest: return location for the result
73 * Subtracts @src2 from @src1 and saves the resulting
76 * The alpha channel of @dest is set as the minimum value
77 * between the alpha channels of @src1 and @src2.
80 clutter_color_subtract (const ClutterColor *src1,
81 const ClutterColor *src2,
84 g_return_if_fail (src1 != NULL);
85 g_return_if_fail (src2 != NULL);
86 g_return_if_fail (dest != NULL);
88 dest->red = CLAMP (src2->red - src1->red, 0, 255);
89 dest->green = CLAMP (src2->green - src1->green, 0, 255);
90 dest->blue = CLAMP (src2->blue - src1->blue, 0, 255);
92 dest->alpha = MIN (src1->alpha, src2->alpha);
96 * clutter_color_lighten:
97 * @src: a #ClutterColor
98 * @dest: return location for the lighter color
100 * Lightens @src by a fixed amount, and saves the changed
104 clutter_color_lighten (const ClutterColor *src,
107 clutter_color_shade (src, dest, 1.3);
111 * clutter_color_darken:
112 * @src: a #ClutterColor
113 * @dest: return location for the darker color
115 * Darkens @src by a fixed amount, and saves the changed color
119 clutter_color_darken (const ClutterColor *src,
122 clutter_color_shade (src, dest, 0.7);
126 * clutter_color_to_hls:
127 * @src: a #ClutterColor
128 * @hue: return location for the hue value or %NULL
129 * @luminance: return location for the luminance value or %NULL
130 * @saturation: return location for the saturation value or %NULL
132 * Converts @src to the HLS format.
135 clutter_color_to_hls (const ClutterColor *src,
140 gdouble red, green, blue;
141 gdouble min, max, delta;
144 g_return_if_fail (src != NULL);
146 red = src->red / 255.0;
147 green = src->green / 255.0;
148 blue = src->blue / 255.0;
182 s = (max - min) / (max + min);
184 s = (max - min) / (2 - max - min);
188 h = (green - blue) / delta;
189 else if (green == max)
190 h = 2 + (blue - red) / delta;
191 else if (blue == max)
192 h = 4 + (red - green) / delta;
200 *hue = (guint8) (h * 255);
203 *luminance = (guint8) (l * 255);
206 *saturation = (guint8) (s * 255);
210 * clutter_color_from_hls:
211 * @dest: return location for a #ClutterColor
212 * @hue: hue value (0 .. 255)
213 * @luminance: luminance value (0 .. 255)
214 * @saturation: saturation value (0 .. 255)
216 * Converts a color expressed in HLS (hue, luminance and saturation)
217 * values into a #ClutterColor.
220 clutter_color_from_hls (ClutterColor *dest,
228 g_return_if_fail (dest != NULL);
230 l = (gdouble) luminance / 255.0;
231 s = (gdouble) saturation / 255.0;
242 dest->red = (guint8) l * 255;
243 dest->green = (guint8) l * 255;
244 dest->blue = (guint8) l * 255;
248 h = ((gdouble) hue / 255.0) + 120;
255 dest->red = (guint8) (m1 + (m2 - m1) * h / 60) * 255;
257 dest->red = (guint8) m2 * 255;
259 dest->red = (guint8) (m1 + (m2 - m1) * (240 - h) / 60) * 255;
261 dest->red = (guint8) m1 * 255;
263 h = (gdouble) hue / 255.0;
270 dest->green = (guint8) (m1 + (m2 - m1) * h / 60) * 255;
272 dest->green = (guint8) m2 * 255;
274 dest->green = (guint8) (m1 + (m2 - m1) * (240 - h) / 60) * 255;
276 dest->green = (guint8) m1 * 255;
278 h = ((gdouble) hue / 255.0) - 120;
285 dest->blue = (guint8) (m1 + (m2 - m1) * h / 60) * 255;
287 dest->blue = (guint8) m2 * 255;
289 dest->blue = (guint8) (m1 + (m2 - m1) * (240 - h) / 60) * 255;
291 dest->blue = (guint8) m1 * 255;
296 * clutter_color_shade:
297 * @src: a #ClutterColor
298 * @dest: return location for the shaded color
299 * @shade: the shade factor to apply
301 * Shades @src by the factor of @shade and saves the modified
305 clutter_color_shade (const ClutterColor *src,
312 g_return_if_fail (src != NULL);
313 g_return_if_fail (dest != NULL);
315 clutter_color_to_hls (src, &h, &l, &s);
317 h1 = (gdouble) h / 255.0;
318 l1 = (gdouble) l / 255.0;
319 s1 = (gdouble) s / 255.0;
333 h = (guint8) h1 * 255;
334 l = (guint8) l1 * 255;
335 s = (guint8) s1 * 255;
337 clutter_color_from_hls (dest, h, l, s);
341 * clutter_color_to_pixel:
342 * @src: a #ClutterColor
344 * Converts @src into a packed 32 bit integer, containing
345 * all the four 8 bit channels used by #ClutterColor.
347 * Return value: a packed color
350 clutter_color_to_pixel (const ClutterColor *src)
352 g_return_val_if_fail (src != NULL, 0);
354 return (src->alpha | src->blue << 8 | src->green << 16 | src->red << 24);
358 * clutter_color_from_pixel:
359 * @dest: return location for a #ClutterColor
360 * @pixel: a 32 bit packed integer containing a color
362 * Converts @pixel from the packed representation of a four 8 bit channel
363 * color to a #ClutterColor.
366 clutter_color_from_pixel (ClutterColor *dest,
369 g_return_if_fail (dest != NULL);
371 dest->red = pixel >> 24;
372 dest->green = (pixel >> 16) & 0xff;
373 dest->blue = (pixel >> 8) & 0xff;
374 dest->alpha = pixel % 0xff;
377 static ClutterColor *
378 clutter_color_copy (ClutterColor *color)
380 ClutterColor *result = g_new0 (ClutterColor, 1);
388 clutter_color_get_type (void)
390 static GType our_type = 0;
393 our_type = g_boxed_type_register_static ("ClutterColor",
394 (GBoxedCopyFunc) clutter_color_copy,
395 (GBoxedFreeFunc) g_free);