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
use glib::translate::*;
use std::boxed::Box as Box_;
use std::mem;

glib_wrapper! {
    #[derive(Debug, PartialOrd, Ord)] // Hash
    pub struct Color(Boxed<ffi::CoglColor>);

    match fn {
        copy => |ptr| ffi::cogl_color_copy(mut_override(ptr)),
        free => |ptr| ffi::cogl_color_free(ptr),
        get_type => || ffi::cogl_color_get_gtype(),
    }
}

impl Color {
    /// Creates a new (empty) color
    ///
    /// # Returns
    ///
    /// a newly-allocated `Color`. Use `Color::free`
    ///  to free the allocated resources
    pub fn new() -> Color {
        unsafe { from_glib_full(ffi::cogl_color_new()) }
    }

    /// Retrieves the alpha channel of `self` as a fixed point
    /// value between 0 and 1.0.
    ///
    /// # Returns
    ///
    /// the alpha channel of the passed color
    pub fn get_alpha(&self) -> f32 {
        unsafe { ffi::cogl_color_get_alpha(self.to_glib_none().0) }
    }

    /// Retrieves the alpha channel of `self` as a byte value
    /// between 0 and 255
    ///
    /// # Returns
    ///
    /// the alpha channel of the passed color
    pub fn get_alpha_byte(&self) -> u8 {
        unsafe { ffi::cogl_color_get_alpha_byte(self.to_glib_none().0) }
    }

    /// Retrieves the alpha channel of `self` as a floating point
    /// value between 0.0 and 1.0
    ///
    /// # Returns
    ///
    /// the alpha channel of the passed color
    pub fn get_alpha_float(&self) -> f32 {
        unsafe { ffi::cogl_color_get_alpha_float(self.to_glib_none().0) }
    }

    /// Retrieves the blue channel of `self` as a fixed point
    /// value between 0 and 1.0.
    ///
    /// # Returns
    ///
    /// the blue channel of the passed color
    pub fn get_blue(&self) -> f32 {
        unsafe { ffi::cogl_color_get_blue(self.to_glib_none().0) }
    }

    /// Retrieves the blue channel of `self` as a byte value
    /// between 0 and 255
    ///
    /// # Returns
    ///
    /// the blue channel of the passed color
    pub fn get_blue_byte(&self) -> u8 {
        unsafe { ffi::cogl_color_get_blue_byte(self.to_glib_none().0) }
    }

    /// Retrieves the blue channel of `self` as a floating point
    /// value between 0.0 and 1.0
    ///
    /// # Returns
    ///
    /// the blue channel of the passed color
    pub fn get_blue_float(&self) -> f32 {
        unsafe { ffi::cogl_color_get_blue_float(self.to_glib_none().0) }
    }

    /// Retrieves the green channel of `self` as a fixed point
    /// value between 0 and 1.0.
    ///
    /// # Returns
    ///
    /// the green channel of the passed color
    pub fn get_green(&self) -> f32 {
        unsafe { ffi::cogl_color_get_green(self.to_glib_none().0) }
    }

    /// Retrieves the green channel of `self` as a byte value
    /// between 0 and 255
    ///
    /// # Returns
    ///
    /// the green channel of the passed color
    pub fn get_green_byte(&self) -> u8 {
        unsafe { ffi::cogl_color_get_green_byte(self.to_glib_none().0) }
    }

    /// Retrieves the green channel of `self` as a floating point
    /// value between 0.0 and 1.0
    ///
    /// # Returns
    ///
    /// the green channel of the passed color
    pub fn get_green_float(&self) -> f32 {
        unsafe { ffi::cogl_color_get_green_float(self.to_glib_none().0) }
    }

    /// Retrieves the red channel of `self` as a fixed point
    /// value between 0 and 1.0.
    ///
    /// # Returns
    ///
    /// the red channel of the passed color
    pub fn get_red(&self) -> f32 {
        unsafe { ffi::cogl_color_get_red(self.to_glib_none().0) }
    }

    /// Retrieves the red channel of `self` as a byte value
    /// between 0 and 255
    ///
    /// # Returns
    ///
    /// the red channel of the passed color
    pub fn get_red_byte(&self) -> u8 {
        unsafe { ffi::cogl_color_get_red_byte(self.to_glib_none().0) }
    }

    /// Retrieves the red channel of `self` as a floating point
    /// value between 0.0 and 1.0
    ///
    /// # Returns
    ///
    /// the red channel of the passed color
    pub fn get_red_float(&self) -> f32 {
        unsafe { ffi::cogl_color_get_red_float(self.to_glib_none().0) }
    }

    /// Sets the values of the passed channels into a `Color`
    /// ## `red`
    /// value of the red channel, between 0 and 1.0
    /// ## `green`
    /// value of the green channel, between 0 and 1.0
    /// ## `blue`
    /// value of the blue channel, between 0 and 1.0
    /// ## `alpha`
    /// value of the alpha channel, between 0 and 1.0
    pub fn init_from_4f(&mut self, red: f32, green: f32, blue: f32, alpha: f32) {
        unsafe {
            ffi::cogl_color_init_from_4f(self.to_glib_none_mut().0, red, green, blue, alpha);
        }
    }

    /// Sets the values of the passed channels into a `Color`
    /// ## `color_array`
    /// a pointer to an array of 4 float color components
    pub fn init_from_4fv(&mut self, color_array: &[f32]) {
        unsafe {
            ffi::cogl_color_init_from_4fv(self.to_glib_none_mut().0, color_array.as_ptr());
        }
    }

    /// Sets the values of the passed channels into a `Color`.
    /// ## `red`
    /// value of the red channel, between 0 and 255
    /// ## `green`
    /// value of the green channel, between 0 and 255
    /// ## `blue`
    /// value of the blue channel, between 0 and 255
    /// ## `alpha`
    /// value of the alpha channel, between 0 and 255
    pub fn init_from_4ub(&mut self, red: u8, green: u8, blue: u8, alpha: u8) {
        unsafe {
            ffi::cogl_color_init_from_4ub(self.to_glib_none_mut().0, red, green, blue, alpha);
        }
    }

    /// Converts a non-premultiplied color to a pre-multiplied color. For
    /// example, semi-transparent red is (1.0, 0, 0, 0.5) when non-premultiplied
    /// and (0.5, 0, 0, 0.5) when premultiplied.
    pub fn premultiply(&mut self) {
        unsafe {
            ffi::cogl_color_premultiply(self.to_glib_none_mut().0);
        }
    }

    /// Sets the alpha channel of `self` to `alpha`.
    /// ## `alpha`
    /// a float value between 0.0f and 1.0f
    pub fn set_alpha(&mut self, alpha: f32) {
        unsafe {
            ffi::cogl_color_set_alpha(self.to_glib_none_mut().0, alpha);
        }
    }

    /// Sets the alpha channel of `self` to `alpha`.
    /// ## `alpha`
    /// a byte value between 0 and 255
    pub fn set_alpha_byte(&mut self, alpha: u8) {
        unsafe {
            ffi::cogl_color_set_alpha_byte(self.to_glib_none_mut().0, alpha);
        }
    }

    /// Sets the alpha channel of `self` to `alpha`.
    /// ## `alpha`
    /// a float value between 0.0f and 1.0f
    pub fn set_alpha_float(&mut self, alpha: f32) {
        unsafe {
            ffi::cogl_color_set_alpha_float(self.to_glib_none_mut().0, alpha);
        }
    }

    /// Sets the blue channel of `self` to `blue`.
    /// ## `blue`
    /// a float value between 0.0f and 1.0f
    pub fn set_blue(&mut self, blue: f32) {
        unsafe {
            ffi::cogl_color_set_blue(self.to_glib_none_mut().0, blue);
        }
    }

    /// Sets the blue channel of `self` to `blue`.
    /// ## `blue`
    /// a byte value between 0 and 255
    pub fn set_blue_byte(&mut self, blue: u8) {
        unsafe {
            ffi::cogl_color_set_blue_byte(self.to_glib_none_mut().0, blue);
        }
    }

    /// Sets the blue channel of `self` to `blue`.
    /// ## `blue`
    /// a float value between 0.0f and 1.0f
    pub fn set_blue_float(&mut self, blue: f32) {
        unsafe {
            ffi::cogl_color_set_blue_float(self.to_glib_none_mut().0, blue);
        }
    }

    /// Sets the green channel of `self` to `green`.
    /// ## `green`
    /// a float value between 0.0f and 1.0f
    pub fn set_green(&mut self, green: f32) {
        unsafe {
            ffi::cogl_color_set_green(self.to_glib_none_mut().0, green);
        }
    }

    /// Sets the green channel of `self` to `green`.
    /// ## `green`
    /// a byte value between 0 and 255
    pub fn set_green_byte(&mut self, green: u8) {
        unsafe {
            ffi::cogl_color_set_green_byte(self.to_glib_none_mut().0, green);
        }
    }

    /// Sets the green channel of `self` to `green`.
    /// ## `green`
    /// a float value between 0.0f and 1.0f
    pub fn set_green_float(&mut self, green: f32) {
        unsafe {
            ffi::cogl_color_set_green_float(self.to_glib_none_mut().0, green);
        }
    }

    /// Sets the red channel of `self` to `red`.
    /// ## `red`
    /// a float value between 0.0f and 1.0f
    pub fn set_red(&mut self, red: f32) {
        unsafe {
            ffi::cogl_color_set_red(self.to_glib_none_mut().0, red);
        }
    }

    /// Sets the red channel of `self` to `red`.
    /// ## `red`
    /// a byte value between 0 and 255
    pub fn set_red_byte(&mut self, red: u8) {
        unsafe {
            ffi::cogl_color_set_red_byte(self.to_glib_none_mut().0, red);
        }
    }

    /// Sets the red channel of `self` to `red`.
    /// ## `red`
    /// a float value between 0.0f and 1.0f
    pub fn set_red_float(&mut self, red: f32) {
        unsafe {
            ffi::cogl_color_set_red_float(self.to_glib_none_mut().0, red);
        }
    }

    /// Converts `self` to the HLS format.
    ///
    /// The `hue` value is in the 0 .. 360 range. The `luminance` and
    /// `saturation` values are in the 0 .. 1 range.
    /// ## `hue`
    /// return location for the hue value or `None`
    /// ## `saturation`
    /// return location for the saturation value or `None`
    /// ## `luminance`
    /// return location for the luminance value or `None`
    pub fn to_hsl(&self) -> (f32, f32, f32) {
        unsafe {
            let mut hue = mem::MaybeUninit::uninit();
            let mut saturation = mem::MaybeUninit::uninit();
            let mut luminance = mem::MaybeUninit::uninit();
            ffi::cogl_color_to_hsl(
                self.to_glib_none().0,
                hue.as_mut_ptr(),
                saturation.as_mut_ptr(),
                luminance.as_mut_ptr(),
            );
            let hue = hue.assume_init();
            let saturation = saturation.assume_init();
            let luminance = luminance.assume_init();
            (hue, saturation, luminance)
        }
    }

    /// Converts a pre-multiplied color to a non-premultiplied color. For
    /// example, semi-transparent red is (0.5, 0, 0, 0.5) when premultiplied
    /// and (1.0, 0, 0, 0.5) when non-premultiplied.
    pub fn unpremultiply(&mut self) {
        unsafe {
            ffi::cogl_color_unpremultiply(self.to_glib_none_mut().0);
        }
    }

    fn equal(v1: &Self, v2: &Self) -> bool {
        let a = Box_::into_raw(Box::new(v1)) as *mut _;
        let b = Box_::into_raw(Box::new(v2)) as *mut _;
        unsafe { ffi::cogl_color_equal(a, b) == crate::TRUE }
    }

    /// Converts a color expressed in HLS (hue, luminance and saturation)
    /// values into a `Color`.
    /// ## `color`
    /// return location for a `Color`
    /// ## `hue`
    /// hue value, in the 0 .. 360 range
    /// ## `saturation`
    /// saturation value, in the 0 .. 1 range
    /// ## `luminance`
    /// luminance value, in the 0 .. 1 range
    pub fn init_from_hsl(hue: f32, saturation: f32, luminance: f32) -> Color {
        unsafe {
            let mut color = Color::uninitialized();
            ffi::cogl_color_init_from_hsl(color.to_glib_none_mut().0, hue, saturation, luminance);
            color
        }
    }
}

#[doc(hidden)]
impl Uninitialized for Color {
    #[inline]
    unsafe fn uninitialized() -> Self {
        mem::zeroed()
    }
}

impl Default for Color {
    fn default() -> Self {
        Self::new()
    }
}

impl PartialEq for Color {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        Color::equal(self, other)
    }
}

impl Eq for Color {}