1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Authored By Matthew Allum <mallum@openedhand.com>
*
* Copyright (C) 2006 OpenedHand
*
* 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.
*
* 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
* Lesser 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.
*/
/**
* SECTION:clutter-color
* @short_description: Color management and manipulation.
*
* #ClutterColor is a simple type for representing colors.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "clutter-color.h"
/**
* clutter_color_add:
* @src1: a #ClutterColor
* @src2: a #ClutterColor
* @dest: return location for the result
*
* Adds @src2 to @src1 and saves the resulting color
* inside @dest.
*
* The alpha channel of @dest is as the maximum value
* between the alpha channels of @src1 and @src2.
*/
void
clutter_color_add (const ClutterColor *src1,
const ClutterColor *src2,
ClutterColor *dest)
{
g_return_if_fail (src1 != NULL);
g_return_if_fail (src2 != NULL);
g_return_if_fail (dest != NULL);
dest->red = CLAMP (src1->red + src2->red, 0, 255);
dest->green = CLAMP (src1->green + src2->green, 0, 255);
dest->blue = CLAMP (src1->blue + src2->blue, 0, 255);
dest->alpha = MAX (src1->alpha, src2->alpha);
}
/**
* clutter_color_subtract:
* @src1: a #ClutterColor
* @src2: a #ClutterColor
* @dest: return location for the result
*
* Subtracts @src2 from @src1 and saves the resulting
* color inside @dest.
*
* The alpha channel of @dest is set as the minimum value
* between the alpha channels of @src1 and @src2.
*/
void
clutter_color_subtract (const ClutterColor *src1,
const ClutterColor *src2,
ClutterColor *dest)
{
g_return_if_fail (src1 != NULL);
g_return_if_fail (src2 != NULL);
g_return_if_fail (dest != NULL);
dest->red = CLAMP (src2->red - src1->red, 0, 255);
dest->green = CLAMP (src2->green - src1->green, 0, 255);
dest->blue = CLAMP (src2->blue - src1->blue, 0, 255);
dest->alpha = MIN (src1->alpha, src2->alpha);
}
/**
* clutter_color_lighten:
* @src: a #ClutterColor
* @dest: return location for the lighter color
*
* Lightens @src by a fixed amount, and saves the changed
* color in @dest.
*/
void
clutter_color_lighten (const ClutterColor *src,
ClutterColor *dest)
{
clutter_color_shade (src, dest, 1.3);
}
/**
* clutter_color_darken:
* @src: a #ClutterColor
* @dest: return location for the darker color
*
* Darkens @src by a fixed amount, and saves the changed color
* in @dest.
*/
void
clutter_color_darken (const ClutterColor *src,
ClutterColor *dest)
{
clutter_color_shade (src, dest, 0.7);
}
/**
* clutter_color_to_hls:
* @src: a #ClutterColor
* @hue: return location for the hue value or %NULL
* @luminance: return location for the luminance value or %NULL
* @saturation: return location for the saturation value or %NULL
*
* Converts @src to the HLS format.
*/
void
clutter_color_to_hls (const ClutterColor *src,
guint8 *hue,
guint8 *luminance,
guint8 *saturation)
{
gdouble red, green, blue;
gdouble min, max, delta;
gdouble h, l, s;
g_return_if_fail (src != NULL);
red = src->red / 255.0;
green = src->green / 255.0;
blue = src->blue / 255.0;
if (red > green)
{
if (red > blue)
max = red;
else
max = blue;
if (green < blue)
min = green;
else
min = blue;
}
else
{
if (green > blue)
max = green;
else
max = blue;
if (red < blue)
min = red;
else
min = blue;
}
l = (max + min) / 2;
s = 0;
h = 0;
if (max != min)
{
if (l <= 0.5)
s = (max - min) / (max + min);
else
s = (max - min) / (2 - max - min);
delta = max - min;
if (red == max)
h = (green - blue) / delta;
else if (green == max)
h = 2 + (blue - red) / delta;
else if (blue == max)
h = 4 + (red - green) / delta;
h *= 60;
if (h < 0.0)
h += 360;
}
if (hue)
*hue = (guint8) (h * 255);
if (luminance)
*luminance = (guint8) (l * 255);
if (saturation)
*saturation = (guint8) (s * 255);
}
/**
* clutter_color_from_hls:
* @dest: return location for a #ClutterColor
* @hue: hue value (0 .. 255)
* @luminance: luminance value (0 .. 255)
* @saturation: saturation value (0 .. 255)
*
* Converts a color expressed in HLS (hue, luminance and saturation)
* values into a #ClutterColor.
*/
void
clutter_color_from_hls (ClutterColor *dest,
guint8 hue,
guint8 luminance,
guint8 saturation)
{
gdouble h, l, s;
gdouble m1, m2;
g_return_if_fail (dest != NULL);
l = (gdouble) luminance / 255.0;
s = (gdouble) saturation / 255.0;
if (l <= 0.5)
m2 = l * (1 - s);
else
m2 = l + s - l * s;
m1 = 2 * l - m2;
if (s == 0)
{
dest->red = (guint8) l * 255;
dest->green = (guint8) l * 255;
dest->blue = (guint8) l * 255;
}
else
{
h = ((gdouble) hue / 255.0) + 120;
while (h > 360)
h -= 360;
while (h < 0)
h += 360;
if (h < 60)
dest->red = (guint8) (m1 + (m2 - m1) * h / 60) * 255;
else if (h < 180)
dest->red = (guint8) m2 * 255;
else if (h < 240)
dest->red = (guint8) (m1 + (m2 - m1) * (240 - h) / 60) * 255;
else
dest->red = (guint8) m1 * 255;
h = (gdouble) hue / 255.0;
while (h > 360)
h -= 360;
while (h < 0)
h += 360;
if (h < 60)
dest->green = (guint8) (m1 + (m2 - m1) * h / 60) * 255;
else if (h < 180)
dest->green = (guint8) m2 * 255;
else if (h < 240)
dest->green = (guint8) (m1 + (m2 - m1) * (240 - h) / 60) * 255;
else
dest->green = (guint8) m1 * 255;
h = ((gdouble) hue / 255.0) - 120;
while (h > 360)
h -= 360;
while (h < 0)
h += 360;
if (h < 60)
dest->blue = (guint8) (m1 + (m2 - m1) * h / 60) * 255;
else if (h < 180)
dest->blue = (guint8) m2 * 255;
else if (h < 240)
dest->blue = (guint8) (m1 + (m2 - m1) * (240 - h) / 60) * 255;
else
dest->blue = (guint8) m1 * 255;
}
}
/**
* clutter_color_shade:
* @src: a #ClutterColor
* @dest: return location for the shaded color
* @shade: the shade factor to apply
*
* Shades @src by the factor of @shade and saves the modified
* color into @dest.
*/
void
clutter_color_shade (const ClutterColor *src,
ClutterColor *dest,
gdouble shade)
{
guint8 h, l, s;
gdouble h1, l1, s1;
g_return_if_fail (src != NULL);
g_return_if_fail (dest != NULL);
clutter_color_to_hls (src, &h, &l, &s);
h1 = (gdouble) h / 255.0;
l1 = (gdouble) l / 255.0;
s1 = (gdouble) s / 255.0;
l1 *= shade;
if (l1 > 1.0)
l1 = 1.0;
else if (l1 < 0.0)
l1 = 0.0;
s1 *= shade;
if (s1 > 1.0)
s1 = 1.0;
else if (s1 < 0.0)
s1 = 0.0;
h = (guint8) h1 * 255;
l = (guint8) l1 * 255;
s = (guint8) s1 * 255;
clutter_color_from_hls (dest, h, l, s);
}
/**
* clutter_color_to_pixel:
* @src: a #ClutterColor
*
* Converts @src into a packed 32 bit integer, containing
* all the four 8 bit channels used by #ClutterColor.
*
* Return value: a packed color
*/
guint32
clutter_color_to_pixel (const ClutterColor *src)
{
g_return_val_if_fail (src != NULL, 0);
return (src->alpha | src->blue << 8 | src->green << 16 | src->red << 24);
}
/**
* clutter_color_from_pixel:
* @dest: return location for a #ClutterColor
* @pixel: a 32 bit packed integer containing a color
*
* Converts @pixel from the packed representation of a four 8 bit channel
* color to a #ClutterColor.
*/
void
clutter_color_from_pixel (ClutterColor *dest,
guint32 pixel)
{
g_return_if_fail (dest != NULL);
dest->red = pixel >> 24;
dest->green = (pixel >> 16) & 0xff;
dest->blue = (pixel >> 8) & 0xff;
dest->alpha = pixel % 0xff;
}
static ClutterColor *
clutter_color_copy (ClutterColor *color)
{
ClutterColor *result = g_new0 (ClutterColor, 1);
*result = *color;
return result;
}
GType
clutter_color_get_type (void)
{
static GType our_type = 0;
if (!our_type)
our_type = g_boxed_type_register_static ("ClutterColor",
(GBoxedCopyFunc) clutter_color_copy,
(GBoxedFreeFunc) g_free);
return our_type;
}
|