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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! Caintains base color structure
//! Specifically [`ColorRgb`] and [`ColorRgba`]

use std::cmp::Ordering;
use std::fmt::{Binary, Display, Formatter, LowerHex, Octal, UpperHex};

#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Serialize};

/// RGB color representation
/// # Example
/// ```
/// use roccat_vulcan_api_rs::{ColorBuffer, ColorRgb};
///
/// let _buffer = ColorBuffer::from_element(ColorRgb::new(255, 255, 255));
/// ```
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Copy, Hash, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct ColorRgb {
    /// Red
    r: u8,
    /// Green
    g: u8,
    /// Blue
    b: u8,
}

impl ColorRgb {
    /// Create a bew color from rgb values
    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }

    /// create [`ColorRgb`] form `[R, G, B]`
    /// # Example
    /// ```
    /// use roccat_vulcan_api_rs::ColorRgb;
    ///
    /// let array = [255, 0, 0];
    /// assert_eq!(ColorRgb::new_from_array(array), ColorRgb::new(255, 0, 0));
    /// ```
    pub const fn new_from_array(array: [u8; 3]) -> Self {
        Self {
            r: array[0],
            g: array[1],
            b: array[2],
        }
    }

    /// Get the red itensity
    pub const fn r(self) -> u8 {
        self.r
    }

    /// Get the green itensity
    pub const fn g(self) -> u8 {
        self.g
    }

    /// Get the blue itensity
    pub const fn b(self) -> u8 {
        self.b
    }

    /// Get the red itensity
    pub fn r_mut(&mut self) -> &mut u8 {
        &mut self.r
    }

    /// Get the green itensity
    pub fn g_mut(&mut self) -> &mut u8 {
        &mut self.g
    }

    /// Get the blue itensity
    pub fn b_mut(&mut self) -> &mut u8 {
        &mut self.b
    }

    /// Retrn the color as a u32 encoded as `0x00_RR_GG_BB`
    /// # Example
    /// ```
    /// use roccat_vulcan_api_rs::ColorRgb;
    ///
    /// let color = ColorRgb::new(0xFF, 0xC5, 0x09);
    /// let number = 0x00_FF_C5_09_u32;
    /// assert_eq!(color.into_u32(), number);
    /// ```
    pub const fn into_u32(self) -> u32 {
        ((self.r as u32) << 16_u32) + ((self.g as u32) << 8_u32) + self.b as u32
    }

    /// Create the color form a u32.
    /// The number is encoded as `0x**_RR_GG_BB`, where `**` are droped bites.
    /// # Example
    /// ```
    /// use roccat_vulcan_api_rs::ColorRgb;
    ///
    /// let number = 0x00_FF_C5_09_u32;
    /// let color = ColorRgb::from_u32(number);
    /// assert_eq!(color, ColorRgb::new(0xFF, 0xC5, 0x09));
    /// ```
    #[allow(clippy::cast_possible_truncation)]
    pub const fn from_u32(c: u32) -> Self {
        Self::new((c >> 16) as u8, (c >> 8) as u8, c as u8)
    }
}

// TODO

// pub enum ValueCreationError {
//     TooSmall,
//     TooBig,
//     Nan,
// }

/// Color saturation, gauranties that the value is between 0 and 1.
///
/// Used by [`ColorRgb::new_hsv`].
///
/// # Examlpe
/// ```
/// use roccat_vulcan_api_rs::Saturation;
///
/// assert_eq!(Saturation::new(0.5_f64).unwrap().value(), 0.5_f64);
/// assert_eq!(Saturation::new(1.5_f64), None);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Saturation(f64);

impl Saturation {
    /// Create a new value if `s` is between 0 and 1 both included. Otherwise return [`None`]
    pub fn new(s: f64) -> Option<Self> {
        if (0_f64..=1_f64).contains(&s) && !s.is_nan() {
            Some(Self(s))
        } else {
            None
        }
    }

    /// Get the value warpped
    pub const fn value(self) -> f64 {
        self.0
    }
}

/// Color value (also called brigthness). Gauranties that the value is between 0 and 1.
///
/// Used by [`ColorRgb::new_hsv`].
///
/// # Examlpe
/// ```
/// use roccat_vulcan_api_rs::Value;
///
/// assert_eq!(Value::new(0.5_f64).unwrap().value(), 0.5_f64);
/// assert_eq!(Value::new(1.5_f64), None);
/// ```
pub type Value = Saturation;

impl Eq for Saturation {}

impl Ord for Saturation {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.partial_cmp(other) {
            Some(ord) => ord,
            None => unreachable!(),
        }
    }
}

impl PartialOrd for Saturation {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl Display for Saturation {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Default for Saturation {
    fn default() -> Self {
        match Self::new(0_f64) {
            Some(s) => s,
            None => unreachable!(),
        }
    }
}

/// Color hue. Gauranties that the value is between 0 and 1.
///
/// Used by [`ColorRgb::new_hsv`].
///
/// # Examlpe
/// ```
/// use roccat_vulcan_api_rs::Hue;
///
/// assert_eq!(Hue::new(0.5_f64).unwrap().value(), 0.5_f64);
/// assert_eq!(Hue::new(1.5_f64), None);
/// assert_eq!(Hue::new_from_degree(180_f64), Hue::new(0.5_f64))
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Hue(f64);

impl Hue {
    /// Create a new value between 0 and 1
    /// # Example
    /// ```
    /// use roccat_vulcan_api_rs::Hue;
    ///
    /// assert_eq!(Hue::new(0.5_f64).unwrap().value(), 0.5_f64);
    /// assert_eq!(Hue::new(1.5_f64), None);
    /// ```
    pub fn new(s: f64) -> Option<Self> {
        if (0_f64..=1_f64).contains(&s) && !s.is_nan() {
            Some(Self(s))
        } else {
            None
        }
    }

    /// Create a hue from an angle in degree
    /// # Examlpe
    /// ```
    /// use roccat_vulcan_api_rs::Hue;
    ///
    /// assert_eq!(Hue::new_from_degree(180_f64), Hue::new(0.5_f64))
    /// ```
    pub fn new_from_degree(f: f64) -> Option<Self> {
        Self::new(f / 360_f64)
    }

    /// Create a new hue from an angle in radiant
    /// # Example
    /// ```
    /// use std::f64::consts::PI;
    ///
    /// use roccat_vulcan_api_rs::Hue;
    ///
    /// assert_eq!(Hue::new_from_radiant(PI), Hue::new(0.5_f64))
    /// ```
    pub fn new_from_radiant(f: f64) -> Option<Self> {
        Self::new(f / (2_f64 * std::f64::consts::PI))
    }

    /// Get the hue in degree
    /// # Example
    /// ```
    /// use roccat_vulcan_api_rs::Hue;
    ///
    /// let hue = Hue::new(0.5_f64).unwrap();
    /// assert_eq!(hue.degree(), 180_f64);
    /// let hue = Hue::new(0_f64).unwrap();
    /// assert_eq!(hue.degree(), 0_f64);
    /// let hue = Hue::new(0.25_f64).unwrap();
    /// assert_eq!(hue.degree(), 90_f64);
    /// ```
    pub fn degree(self) -> f64 {
        self.value() * 360_f64
    }

    /// Get the hue in radiant
    pub fn radiant(self) -> f64 {
        self.value() * 2_f64 * std::f64::consts::PI
    }

    /// Get the value warpped
    pub const fn value(self) -> f64 {
        self.0
    }
}

impl Eq for Hue {}

impl PartialOrd for Hue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl Ord for Hue {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.partial_cmp(other) {
            Some(ord) => ord,
            None => unreachable!(),
        }
    }
}

impl Display for Hue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Default for Hue {
    /// Create a new Hue with value `0_f64`
    fn default() -> Self {
        match Self::new(0_f64) {
            Some(s) => s,
            None => unreachable!(),
        }
    }
}

impl ColorRgb {
    /// Convert a value between 0 and 1 en un [u8] entre 0 et 255
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    fn convert_to_u8(v: f64) -> u8 {
        // the cast is safe
        (v * 255_f64).round().min(255_f64).max(0_f64) as u8
    }

    /// Create a new colow with hue, saturation and value space
    pub fn new_hsv(hue: Hue, saturation: Saturation, value: Value) -> Self {
        let chroma = saturation.value() * value.value();
        let h_prime = hue.value() * 6_f64;
        let x = chroma * (1_f64 - ((h_prime % 2_f64) - 1_f64).abs());

        #[allow(clippy::cast_possible_truncation)]
        let (r1, g1, b1) = match h_prime.ceil() as i32 {
            0_i32 | 1_i32 => (chroma, x, 0_f64),
            2_i32 => (x, chroma, 0_f64),
            3_i32 => (0_f64, chroma, x),
            4_i32 => (0_f64, x, chroma),
            5_i32 => (x, 0_f64, chroma),
            6_i32 => (chroma, 0_f64, x),
            _ => (0_f64, 0_f64, 0_f64),
        };

        let m = value.value() - chroma;
        let red = Self::convert_to_u8(r1 + m);
        let green = Self::convert_to_u8(g1 + m);
        let blue = Self::convert_to_u8(b1 + m);
        Self::new(red, green, blue)
    }

    /// Get the Hue Saturation Value representation of this color.
    pub fn into_hsv(self) -> (Hue, Saturation, Value) {
        let value = self.r.max(self.b).max(self.g);
        let min = self.r.min(self.b).min(self.g);
        let chroma = value - min;

        let hue = if chroma == 0 {
            0_f64
        } else if value == self.r {
            1_f64 / 6_f64 * (self.g as f64 - self.b as f64) / chroma as f64
        } else if value == self.g {
            1_f64 / 6_f64 * (2_f64 + (self.b as f64 - self.r as f64) / chroma as f64)
        } else {
            // value == b
            1_f64 / 6_f64 * (4_f64 + (self.r as f64 - self.g as f64) / chroma as f64)
        };

        let s_v = if value == 0 {
            0_f64
        } else {
            chroma as f64 / value as f64
        };

        (
            Hue::new(hue).unwrap(),
            Saturation::new(s_v).unwrap(),
            Value::new(value as f64 / 255_f64).unwrap(),
        )
    }
}

impl From<[u8; 3]> for ColorRgb {
    fn from(array: [u8; 3]) -> Self {
        ColorRgb::new_from_array(array)
    }
}

impl From<(u8, u8, u8)> for ColorRgb {
    fn from(tuple: (u8, u8, u8)) -> Self {
        let (r, g, b) = tuple;
        ColorRgb::new(r, g, b)
    }
}

impl From<u32> for ColorRgb {
    fn from(n: u32) -> Self {
        ColorRgb::from_u32(n)
    }
}

impl From<ColorRgb> for u32 {
    fn from(c: ColorRgb) -> Self {
        c.into_u32()
    }
}

impl From<ColorRgb> for (u8, u8, u8) {
    fn from(color: ColorRgb) -> Self {
        (color.r(), color.g(), color.b())
    }
}

impl Display for ColorRgb {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(r: {}, g: {}, b: {})", self.r(), self.g(), self.b())
    }
}

impl Binary for ColorRgb {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:b}", self.into_u32())
    }
}

impl UpperHex for ColorRgb {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:X}", self.into_u32())
    }
}

impl LowerHex for ColorRgb {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:x}", self.into_u32())
    }
}

impl Octal for ColorRgb {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:o}", self.into_u32())
    }
}

/// Color with alpha parameter
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct ColorRgba {
    /// base color
    color: ColorRgb,
    /// opacity
    alpha: u8,
}

impl ColorRgba {
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]

    /// Create a new RGBA color with the alpa value given by a [`f32`]
    pub fn new_from_float(r: u8, g: u8, b: u8, f: f32) -> Self {
        Self {
            color: ColorRgb::new(r, g, b),
            alpha: (f.max(0_f32).min(1_f32) * 255_f32).floor() as u8,
        }
    }

    /// Create a new RGBA color
    pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self {
            color: ColorRgb::new(r, g, b),
            alpha: a,
        }
    }

    /// Return the transparency value (0: tranparent, 255: opaque)
    pub const fn alpha(self) -> u8 {
        self.alpha
    }

    /// Return the row collor without the alpha
    pub const fn color(self) -> ColorRgb {
        self.color
    }

    /// applay the luminosity level to the value as it would be mixed with black
    #[allow(clippy::cast_possible_truncation)]
    const fn apply_lumninosity(value: u8, lum: u8) -> u8 {
        ((value as u16 * lum as u16) / 255_u16) as u8
    }

    /// Get the red value taking into account the luminosity
    pub const fn r(self) -> u8 {
        Self::apply_lumninosity(self.color().r(), self.alpha())
    }

    /// Get the green value taking into account the luminosity
    pub const fn g(self) -> u8 {
        Self::apply_lumninosity(self.color().g(), self.alpha())
    }

    /// Get the blue value taking into account the luminosity
    pub const fn b(self) -> u8 {
        Self::apply_lumninosity(self.color().b(), self.alpha())
    }

    /// Convert the color into a color RGB mixing it with black
    pub const fn into_color(self) -> ColorRgb {
        ColorRgb::new(self.r(), self.g(), self.b())
    }

    /// Convert the color to an u32
    pub const fn into_u32(self) -> u32 {
        (self.color().into_u32() << 4_u32) + (self.alpha() as u32)
    }

    /// Convert the color to an u32
    #[allow(clippy::cast_possible_truncation)]
    pub const fn from_u32(n: u32) -> Self {
        Self {
            color: ColorRgb::from_u32(n >> 4),
            alpha: n as u8,
        }
    }

    //TODO color add sub etc...
}

impl Display for ColorRgba {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "(r: {}, g: {}, b: {}, a: {})",
            self.color().r(),
            self.color().g(),
            self.color().b(),
            self.alpha()
        )
    }
}

impl Binary for ColorRgba {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:b}", self.into_u32())
    }
}

impl UpperHex for ColorRgba {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:X}", self.into_u32())
    }
}

impl LowerHex for ColorRgba {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:x}", self.into_u32())
    }
}

impl Octal for ColorRgba {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:o}", self.into_u32())
    }
}

impl From<ColorRgb> for ColorRgba {
    fn from(c: ColorRgb) -> Self {
        Self::new(c.r(), c.g(), c.b(), 255)
    }
}

impl From<ColorRgba> for ColorRgb {
    fn from(c: ColorRgba) -> Self {
        c.into_color()
    }
}

impl From<u32> for ColorRgba {
    fn from(n: u32) -> Self {
        ColorRgba::from_u32(n)
    }
}

impl From<ColorRgba> for u32 {
    fn from(c: ColorRgba) -> Self {
        c.into_u32()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn color() {
        let ca = ColorRgba::new(30, 87, 70, 255);
        let c = ColorRgb::new(30, 87, 70);
        assert_eq!(c, ca.color());
        assert_eq!(c.r(), ca.r());

        let ca = ColorRgba::new(20, 0, 0, 128);
        assert_eq!(ca.r(), 10);
        assert_eq!(ca.b(), 0);
    }

    #[test]
    fn hsv_color() {
        let c = ColorRgb::new_hsv(
            Hue::new(1_f64).unwrap(),
            Saturation::new(1_f64).unwrap(),
            Value::new(1_f64).unwrap(),
        );
        assert_eq!(c, ColorRgb::new(255, 0, 0));

        let c = ColorRgb::new_hsv(
            Hue::new(0.5_f64).unwrap(),
            Saturation::new(1_f64).unwrap(),
            Value::new(1_f64).unwrap(),
        );
        assert_eq!(c, ColorRgb::new(0, 255, 255));
    }
}