i3status_rs/themes/
color.rs

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
use crate::errors::*;
use serde::de::{self, Deserializer, Visitor};
use serde::{Deserialize, Serialize, Serializer};
use smart_default::SmartDefault;
use std::fmt;
use std::ops::Add;
use std::str::FromStr;

/// An RGBA color (red, green, blue, alpha).
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Rgba {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl Rgba {
    /// Create a new RGBA color.
    ///
    /// `r`: red component (0 to 255).
    ///
    /// `g`: green component (0 to 255).
    ///
    /// `b`: blue component (0 to 255).
    ///
    /// `a`: alpha component (0 to 255).
    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }

    /// Create a new RGBA color from the `hex` value.
    ///
    /// ```let cyan = Rgba::from_hex(0xffffff);```
    pub fn from_hex(hex: u32) -> Self {
        let [r, g, b, a] = hex.to_be_bytes();
        Self { r, g, b, a }
    }
}

impl Add for Rgba {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Rgba::new(
            self.r.saturating_add(rhs.r),
            self.g.saturating_add(rhs.g),
            self.b.saturating_add(rhs.b),
            self.a.saturating_add(rhs.a),
        )
    }
}

/// An HSVA color (hue, saturation, value, alpha).
#[derive(Copy, Clone, Debug, Default)]
pub struct Hsva {
    pub h: f64,
    pub s: f64,
    pub v: f64,
    pub a: u8,
}

impl Hsva {
    /// Create a new HSVA color.
    ///
    /// `h`: hue component (0 to 360)
    ///
    /// `s`: saturation component (0 to 1)
    ///
    /// `v`: value component (0 to 1)
    ///
    /// `a`: alpha component (0 to 255).
    pub fn new(h: f64, s: f64, v: f64, a: u8) -> Self {
        Self { h, s, v, a }
    }
}

impl PartialEq for Hsva {
    fn eq(&self, other: &Self) -> bool {
        approx(self.h, other.h)
            && approx(self.s, other.s)
            && approx(self.v, other.v)
            && self.a == other.a
    }
}

impl From<Rgba> for Hsva {
    fn from(rgba: Rgba) -> Self {
        let r = rgba.r as f64 / 255.0;
        let g = rgba.g as f64 / 255.0;
        let b = rgba.b as f64 / 255.0;

        let min = r.min(g.min(b));
        let max = r.max(g.max(b));
        let delta = max - min;

        let v = max;
        let s = match max > 1e-3 {
            true => delta / max,
            false => 0.0,
        };
        let h = match delta == 0.0 {
            true => 0.0,
            false => {
                if r == max {
                    (g - b) / delta
                } else if g == max {
                    2.0 + (b - r) / delta
                } else {
                    4.0 + (r - g) / delta
                }
            }
        };
        let h2 = ((h * 60.0) + 360.0) % 360.0;

        Self::new(h2, s, v, rgba.a)
    }
}

impl From<Hsva> for Rgba {
    fn from(hsva: Hsva) -> Self {
        let range = (hsva.h / 60.0) as u8;
        let c = hsva.v * hsva.s;
        let x = c * (1.0 - (((hsva.h / 60.0) % 2.0) - 1.0).abs());
        let m = hsva.v - c;

        let cm_scaled = ((c + m) * 255.0) as u8;
        let xm_scaled = ((x + m) * 255.0) as u8;
        let m_scaled = (m * 255.0) as u8;

        match range {
            0 => Self::new(cm_scaled, xm_scaled, m_scaled, hsva.a),
            1 => Self::new(xm_scaled, cm_scaled, m_scaled, hsva.a),
            2 => Self::new(m_scaled, cm_scaled, xm_scaled, hsva.a),
            3 => Self::new(m_scaled, xm_scaled, cm_scaled, hsva.a),
            4 => Self::new(xm_scaled, m_scaled, cm_scaled, hsva.a),
            _ => Self::new(cm_scaled, m_scaled, xm_scaled, hsva.a),
        }
    }
}

impl Add for Hsva {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Hsva::new(
            (self.h + rhs.h) % 360.,
            (self.s + rhs.s).clamp(0., 1.),
            (self.v + rhs.v).clamp(0., 1.),
            self.a.saturating_add(rhs.a),
        )
    }
}

pub fn approx(a: f64, b: f64) -> bool {
    if a == b {
        return true;
    }
    let eps = 1e-2;
    let abs_a = a.abs();
    let abs_b = b.abs();
    let diff = (abs_a - abs_b).abs();
    if a == 0.0 || b == 0.0 || abs_a + abs_b < f64::EPSILON {
        diff < eps * f64::EPSILON
    } else {
        diff / (abs_a + abs_b).min(f64::MAX) < eps
    }
}

#[derive(Debug, Clone, Copy, PartialEq, SmartDefault)]
pub enum Color {
    #[default]
    None,
    Auto,
    Rgba(Rgba),
    Hsva(Hsva),
}

impl Color {
    pub fn skip_ser(&self) -> bool {
        matches!(self, Self::None | Self::Auto)
    }
}

impl Add for Color {
    type Output = Color;
    fn add(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            // Do nothing
            (x, Self::None | Self::Auto) | (Self::None | Self::Auto, x) => x,
            // Hsva + Hsva => Hsva
            (Color::Hsva(hsva1), Color::Hsva(hsva2)) => Color::Hsva(hsva1 + hsva2),
            // Rgba + Rgba => Rgba
            (Color::Rgba(rgba1), Color::Rgba(rgba2)) => Color::Rgba(rgba1 + rgba2),
            // Hsva + Rgba => Hsva
            // Rgba + Hsva => Hsva
            (Color::Hsva(hsva), Color::Rgba(rgba)) | (Color::Rgba(rgba), Color::Hsva(hsva)) => {
                Color::Hsva(hsva + rgba.into())
            }
        }
    }
}

impl FromStr for Color {
    type Err = Error;

    fn from_str(color: &str) -> Result<Self, Self::Err> {
        Ok(if color == "none" || color.is_empty() {
            Color::None
        } else if color == "auto" {
            Color::Auto
        } else if color.starts_with("hsv:") {
            let err_msg = || format!("'{color}' is not a valid HSVA color");
            let color = color.split_at(4).1;
            let mut components = color.split(':').map(|x| x.parse::<f64>().or_error(err_msg));
            let h = components.next().or_error(err_msg)??;
            let s = components.next().or_error(err_msg)??;
            let v = components.next().or_error(err_msg)??;
            let a = components.next().unwrap_or(Ok(100.))?;
            Color::Hsva(Hsva::new(h, s / 100., v / 100., (a / 100. * 255.) as u8))
        } else {
            let err_msg = || format!("'{color}' is not a valid RGBA color");
            let rgb = color.get(1..7).or_error(err_msg)?;
            let a = color.get(7..9).unwrap_or("FF");
            Color::Rgba(Rgba::from_hex(
                (u32::from_str_radix(rgb, 16).or_error(err_msg)? << 8)
                    + u32::from_str_radix(a, 16).or_error(err_msg)?,
            ))
        })
    }
}

impl Serialize for Color {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let format_rgba =
            |rgba: Rgba| format!("#{:02X}{:02X}{:02X}{:02X}", rgba.r, rgba.g, rgba.b, rgba.a);
        match *self {
            Self::None | Self::Auto => serializer.serialize_none(),
            Self::Rgba(rgba) => serializer.serialize_str(&format_rgba(rgba)),
            Self::Hsva(hsva) => serializer.serialize_str(&format_rgba(hsva.into())),
        }
    }
}

impl<'de> Deserialize<'de> for Color {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ColorVisitor;

        impl Visitor<'_> for ColorVisitor {
            type Value = Color;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("color")
            }

            fn visit_str<E>(self, s: &str) -> Result<Color, E>
            where
                E: de::Error,
            {
                s.parse().serde_error()
            }
        }

        deserializer.deserialize_any(ColorVisitor)
    }
}