"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl / cogl-color.c
1 /*
2  * Cogl
3  *
4  * An object oriented GL/GLES Abstraction/Utility Layer
5  *
6  * Copyright (C) 2008,2009 Intel Corporation.
7  *
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.
12  *
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.
17  *
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/>.
20  *
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <string.h>
29
30 #include "cogl-util.h"
31 #include "cogl-color.h"
32 #include "cogl-fixed.h"
33 #include "cogl-color-private.h"
34
35 CoglColor *
36 cogl_color_new (void)
37 {
38   return g_slice_new (CoglColor);
39 }
40
41 CoglColor *
42 cogl_color_copy (const CoglColor *color)
43 {
44   if (G_LIKELY (color))
45     return g_slice_dup (CoglColor, color);
46
47   return NULL;
48 }
49
50 void
51 cogl_color_free (CoglColor *color)
52 {
53   if (G_LIKELY (color))
54     g_slice_free (CoglColor, color);
55 }
56
57 void
58 cogl_color_init_from_4ub (CoglColor *color,
59                           guint8     red,
60                           guint8     green,
61                           guint8     blue,
62                           guint8     alpha)
63 {
64   _COGL_RETURN_IF_FAIL (color != NULL);
65
66   color->red   = red;
67   color->green = green;
68   color->blue  = blue;
69   color->alpha = alpha;
70 }
71
72 /* XXX: deprecated, use cogl_color_init_from_4ub */
73 void
74 cogl_color_set_from_4ub (CoglColor *dest,
75                          guint8     red,
76                          guint8     green,
77                          guint8     blue,
78                          guint8     alpha)
79 {
80   cogl_color_init_from_4ub (dest, red, green, blue, alpha);
81 }
82
83 void
84 cogl_color_init_from_4f (CoglColor *color,
85                          float      red,
86                          float      green,
87                          float      blue,
88                          float      alpha)
89 {
90   _COGL_RETURN_IF_FAIL (color != NULL);
91
92   color->red   =  (red * 255);
93   color->green =  (green * 255);
94   color->blue  =  (blue * 255);
95   color->alpha =  (alpha * 255);
96 }
97
98 /* XXX: deprecated, use cogl_color_init_from_4f */
99 void
100 cogl_color_set_from_4f (CoglColor *color,
101                         float      red,
102                         float      green,
103                         float      blue,
104                         float      alpha)
105 {
106   cogl_color_init_from_4f (color, red, green, blue, alpha);
107 }
108
109 void
110 cogl_color_init_from_4fv (CoglColor *color,
111                           float *color_array)
112 {
113   _COGL_RETURN_IF_FAIL (color != NULL);
114
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);
119 }
120
121 unsigned char
122 cogl_color_get_red_byte (const CoglColor *color)
123 {
124   return color->red;
125 }
126
127 float
128 cogl_color_get_red_float (const CoglColor *color)
129 {
130   return (float) color->red / 255.0;
131 }
132
133 float
134 cogl_color_get_red (const CoglColor *color)
135 {
136   return  ((float) color->red / 255.0);
137 }
138
139 unsigned char
140 cogl_color_get_green_byte (const CoglColor *color)
141 {
142   return color->green;
143 }
144
145 float
146 cogl_color_get_green_float (const CoglColor *color)
147 {
148   return (float) color->green / 255.0;
149 }
150
151 float
152 cogl_color_get_green (const CoglColor *color)
153 {
154   return  ((float) color->green / 255.0);
155 }
156
157 unsigned char
158 cogl_color_get_blue_byte (const CoglColor *color)
159 {
160   return color->blue;
161 }
162
163 float
164 cogl_color_get_blue_float (const CoglColor *color)
165 {
166   return (float) color->blue / 255.0;
167 }
168
169 float
170 cogl_color_get_blue (const CoglColor *color)
171 {
172   return  ((float) color->blue / 255.0);
173 }
174
175 unsigned char
176 cogl_color_get_alpha_byte (const CoglColor *color)
177 {
178   return color->alpha;
179 }
180
181 float
182 cogl_color_get_alpha_float (const CoglColor *color)
183 {
184   return (float) color->alpha / 255.0;
185 }
186
187 float
188 cogl_color_get_alpha (const CoglColor *color)
189 {
190   return  ((float) color->alpha / 255.0);
191 }
192
193 void
194 cogl_color_set_red_byte (CoglColor     *color,
195                          unsigned char  red)
196 {
197   color->red = red;
198 }
199
200 void
201 cogl_color_set_red_float (CoglColor *color,
202                           float      red)
203 {
204   color->red = red * 255.0;
205 }
206
207 void
208 cogl_color_set_red (CoglColor *color,
209                     float      red)
210 {
211   color->red = red * 255.0;
212 }
213
214 void
215 cogl_color_set_green_byte (CoglColor     *color,
216                            unsigned char  green)
217 {
218   color->green = green;
219 }
220
221 void
222 cogl_color_set_green_float (CoglColor *color,
223                             float      green)
224 {
225   color->green = green * 255.0;
226 }
227
228 void
229 cogl_color_set_green (CoglColor *color,
230                       float      green)
231 {
232   color->green = green * 255.0;
233 }
234
235 void
236 cogl_color_set_blue_byte (CoglColor     *color,
237                           unsigned char  blue)
238 {
239   color->blue = blue;
240 }
241
242 void
243 cogl_color_set_blue_float (CoglColor *color,
244                            float      blue)
245 {
246   color->blue = blue * 255.0;
247 }
248
249 void
250 cogl_color_set_blue (CoglColor *color,
251                      float      blue)
252 {
253   color->blue = blue * 255.0;
254 }
255
256 void
257 cogl_color_set_alpha_byte (CoglColor     *color,
258                            unsigned char  alpha)
259 {
260   color->alpha = alpha;
261 }
262
263 void
264 cogl_color_set_alpha_float (CoglColor *color,
265                             float      alpha)
266 {
267   color->alpha = alpha * 255.0;
268 }
269
270 void
271 cogl_color_set_alpha (CoglColor *color,
272                       float      alpha)
273 {
274   color->alpha = alpha * 255.0;
275 }
276
277 void
278 cogl_color_premultiply (CoglColor *color)
279 {
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;
283 }
284
285 void
286 cogl_color_unpremultiply (CoglColor *color)
287 {
288   if (color->alpha != 0)
289     {
290       color->red = (color->red * 255) / color->alpha;
291       color->green = (color->green * 255) / color->alpha;
292       color->blue = (color->blue * 255) / color->alpha;
293     }
294 }
295
296 gboolean
297 cogl_color_equal (gconstpointer v1, gconstpointer v2)
298 {
299   const guint32 *c1 = v1, *c2 = v2;
300
301   _COGL_RETURN_VAL_IF_FAIL (v1 != NULL, FALSE);
302   _COGL_RETURN_VAL_IF_FAIL (v2 != NULL, FALSE);
303
304   /* XXX: We don't compare the padding */
305   return *c1 == *c2 ? TRUE : FALSE;
306 }
307
308 void
309 _cogl_color_get_rgba_4ubv (const CoglColor *color,
310                            guint8 *dest)
311 {
312   memcpy (dest, color, 4);
313 }
314