logitech_led_sdk/color_percent.rs
1/// A percent-based RGB Color.
2///
3/// Values are from 0-100 NOT 0-255.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct ColorPercent {
6 /// Red
7 ///
8 /// Valued from 0-100
9 pub r: u8,
10
11 /// Green
12 ///
13 /// Valued from 0-100
14 pub g: u8,
15
16 /// Blue
17 ///
18 /// Valued from 0-100
19 pub b: u8,
20}
21
22impl ColorPercent {
23 /// Creates a new color from raw percentage values, NOT RGB values.
24 ///
25 /// # Returns
26 /// This returns a new ColorPercent if the values are in range.
27 /// This returns `None` if any of the values are out of range.
28 pub fn new_percent(r: u8, g: u8, b: u8) -> Option<Self> {
29 if r > 100 || g > 100 || b > 100 {
30 return None;
31 }
32
33 Some(Self { r, g, b })
34 }
35
36 /// Creates a new color from RGB values, NOT percentage values.
37 pub fn new_rgb(r: u8, g: u8, b: u8) -> Self {
38 Self {
39 r: ((u16::from(r) * 100) / 255) as u8,
40 g: ((u16::from(g) * 100) / 255) as u8,
41 b: ((u16::from(b) * 100) / 255) as u8,
42 }
43 }
44}