4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2008,2009 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
30 #include "cogl-util.h"
31 #include "cogl-color.h"
32 #include "cogl-fixed.h"
33 #include "cogl-color-private.h"
38 return g_slice_new (CoglColor);
42 cogl_color_copy (const CoglColor *color)
45 return g_slice_dup (CoglColor, color);
51 cogl_color_free (CoglColor *color)
54 g_slice_free (CoglColor, color);
58 cogl_color_init_from_4ub (CoglColor *color,
64 _COGL_RETURN_IF_FAIL (color != NULL);
72 /* XXX: deprecated, use cogl_color_init_from_4ub */
74 cogl_color_set_from_4ub (CoglColor *dest,
80 cogl_color_init_from_4ub (dest, red, green, blue, alpha);
84 cogl_color_init_from_4f (CoglColor *color,
90 _COGL_RETURN_IF_FAIL (color != NULL);
92 color->red = (red * 255);
93 color->green = (green * 255);
94 color->blue = (blue * 255);
95 color->alpha = (alpha * 255);
98 /* XXX: deprecated, use cogl_color_init_from_4f */
100 cogl_color_set_from_4f (CoglColor *color,
106 cogl_color_init_from_4f (color, red, green, blue, alpha);
110 cogl_color_init_from_4fv (CoglColor *color,
113 _COGL_RETURN_IF_FAIL (color != NULL);
115 color->red = (color_array[0] * 255);
116 color->green = (color_array[1] * 255);
117 color->blue = (color_array[2] * 255);
118 color->alpha = (color_array[3] * 255);
122 cogl_color_get_red_byte (const CoglColor *color)
128 cogl_color_get_red_float (const CoglColor *color)
130 return (float) color->red / 255.0;
134 cogl_color_get_red (const CoglColor *color)
136 return ((float) color->red / 255.0);
140 cogl_color_get_green_byte (const CoglColor *color)
146 cogl_color_get_green_float (const CoglColor *color)
148 return (float) color->green / 255.0;
152 cogl_color_get_green (const CoglColor *color)
154 return ((float) color->green / 255.0);
158 cogl_color_get_blue_byte (const CoglColor *color)
164 cogl_color_get_blue_float (const CoglColor *color)
166 return (float) color->blue / 255.0;
170 cogl_color_get_blue (const CoglColor *color)
172 return ((float) color->blue / 255.0);
176 cogl_color_get_alpha_byte (const CoglColor *color)
182 cogl_color_get_alpha_float (const CoglColor *color)
184 return (float) color->alpha / 255.0;
188 cogl_color_get_alpha (const CoglColor *color)
190 return ((float) color->alpha / 255.0);
194 cogl_color_set_red_byte (CoglColor *color,
201 cogl_color_set_red_float (CoglColor *color,
204 color->red = red * 255.0;
208 cogl_color_set_red (CoglColor *color,
211 color->red = red * 255.0;
215 cogl_color_set_green_byte (CoglColor *color,
218 color->green = green;
222 cogl_color_set_green_float (CoglColor *color,
225 color->green = green * 255.0;
229 cogl_color_set_green (CoglColor *color,
232 color->green = green * 255.0;
236 cogl_color_set_blue_byte (CoglColor *color,
243 cogl_color_set_blue_float (CoglColor *color,
246 color->blue = blue * 255.0;
250 cogl_color_set_blue (CoglColor *color,
253 color->blue = blue * 255.0;
257 cogl_color_set_alpha_byte (CoglColor *color,
260 color->alpha = alpha;
264 cogl_color_set_alpha_float (CoglColor *color,
267 color->alpha = alpha * 255.0;
271 cogl_color_set_alpha (CoglColor *color,
274 color->alpha = alpha * 255.0;
278 cogl_color_premultiply (CoglColor *color)
280 color->red = (color->red * color->alpha + 128) / 255;
281 color->green = (color->green * color->alpha + 128) / 255;
282 color->blue = (color->blue * color->alpha + 128) / 255;
286 cogl_color_unpremultiply (CoglColor *color)
288 if (color->alpha != 0)
290 color->red = (color->red * 255) / color->alpha;
291 color->green = (color->green * 255) / color->alpha;
292 color->blue = (color->blue * 255) / color->alpha;
297 cogl_color_equal (gconstpointer v1, gconstpointer v2)
299 const guint32 *c1 = v1, *c2 = v2;
301 _COGL_RETURN_VAL_IF_FAIL (v1 != NULL, FALSE);
302 _COGL_RETURN_VAL_IF_FAIL (v2 != NULL, FALSE);
304 /* XXX: We don't compare the padding */
305 return *c1 == *c2 ? TRUE : FALSE;
309 _cogl_color_get_rgba_4ubv (const CoglColor *color,
312 memcpy (dest, color, 4);