ferravex_graphics/
look.rs1use crate::Color;
3#[repr(u32)]
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub enum Look {
7 #[default]
8 Authored = 0,
9 Warm = 1,
10 Cool = 2,
11 Ink = 3,
12}
13impl Look {
14 pub fn from_index(value: u32) -> Option<Self> {
15 match value {
16 0 => Some(Self::Authored),
17 1 => Some(Self::Warm),
18 2 => Some(Self::Cool),
19 3 => Some(Self::Ink),
20 _ => None,
21 }
22 }
23 pub fn from_name(name: &str) -> Option<Self> {
24 match name {
25 "authored" => Some(Self::Authored),
26 "warm" => Some(Self::Warm),
27 "cool" => Some(Self::Cool),
28 "ink" => Some(Self::Ink),
29 _ => None,
30 }
31 }
32 pub fn name(self) -> &'static str {
33 match self {
34 Self::Authored => "Authored",
35 Self::Warm => "Warm",
36 Self::Cool => "Cool",
37 Self::Ink => "Ink",
38 }
39 }
40 pub fn shade(self, color: Color) -> Color {
41 let Color { r, g, b, a } = color;
42 match self {
43 Self::Authored => color,
44 Self::Warm => Color::new((r * 1.06 + g * 0.02).min(1.), g * 0.98, b * 0.88, a),
45 Self::Cool => Color::new(
46 r * 0.87,
47 (g * 1.02).min(1.),
48 (b * 1.08 + r * 0.015).min(1.),
49 a,
50 ),
51 Self::Ink => {
52 let y = r * 0.2126 + g * 0.7152 + b * 0.0722;
53 Color::new(y * 0.82, y * 0.94, y, a)
54 }
55 }
56 }
57}
58#[cfg(test)]
59mod tests {
60 use super::*;
61 #[test]
62 fn presets_preserve_alpha_and_authored_identity() {
63 for color in [
64 Color::new(0.9, 0.4, 0.2, 0.3),
65 Color::new(0., 0., 0., 0.),
66 Color::new(1., 1., 1., 1.),
67 ] {
68 assert_eq!(Look::Authored.shade(color), color);
69 for i in 0..4 {
70 let look = Look::from_index(i).unwrap();
71 let c = look.shade(color);
72 assert_eq!(c.a, color.a);
73 assert!(c.to_array().iter().all(|v| (0. ..=1.).contains(v)));
74 assert_eq!(Look::from_name(&look.name().to_lowercase()), Some(look));
75 }
76 }
77 assert_eq!(Look::from_index(4), None);
78 }
79}