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
mod conversions;
#[cfg(test)]
mod tests;
/// A struct representing a color in the CIELAB color space.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Cielab(f32, f32, f32); // lightness, a, b
impl Cielab {
/// Creates a Cielab instance from 3 float
/// representing the lightness, a, and b components.
///
/// The lightness component must between 0 and 100 included
/// otherwise it doesn't represent a valid CIELAB color
/// and the return value is None.
///
/// # Examples
///
/// ```
/// use cowor::Cielab;
/// let valid = Cielab::new(10.0, 20.0, 30.0); // Ok.
/// let invalid = Cielab::new(110.0, 20.0, 30.0); // Not ok. The lightness component is greater than 100.
/// assert!(valid.is_some());
/// assert!(invalid.is_none());
/// ```
pub fn new(lightness: f32, a: f32, b: f32) -> Option<Self> {
match (0f32..=100f32).contains(&lightness) {
true => Some(Self(lightness, a, b)),
false => None,
}
}
/// Lightness component of a CIELAB color.
/// Value is between 0 and 100 included.
///
/// # Examples
///
/// ```
/// use cowor::Cielab;
/// let cielab = Cielab::new(10.0, 20.0, 30.0).unwrap();
/// assert_eq!(cielab.lightness(), 10.0);
/// ```
pub fn lightness(&self) -> f32 {
self.0
}
/// A component of a CIELAB color.
///
/// # Examples
///
/// ```
/// use cowor::Cielab;
/// let cielab = Cielab::new(10.0, 20.0, 30.0).unwrap();
/// assert_eq!(cielab.a(), 20.0);
/// ```
pub fn a(&self) -> f32 {
self.1
}
/// B component of a CIELAB color.
///
/// # Examples
///
/// ```
/// use cowor::Cielab;
/// let cielab = Cielab::new(10.0, 20.0, 30.0).unwrap();
/// assert_eq!(cielab.b(), 30.0);
/// ```
pub fn b(&self) -> f32 {
self.2
}
}