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
use crate::{simd::f32x4, Paint, Point, Scalar, Transform, Units};
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "serde")]
use serde::{de::DeserializeSeed, Deserializer};
use std::{
    collections::HashMap,
    fmt,
    ops::{Add, Mul},
    str::FromStr,
};

lazy_static::lazy_static! {
    pub static ref SVG_COLORS: HashMap<String, RGBA> = {
        let empty = HashMap::new(); // do not use parse to avoid recursive lock
        include_str!("./svg-colors.txt")
            .lines()
            .map(|line| {
                let mut iter = line.split(' ');
                let name = iter.next()?;
                let color = RGBA::from_str_named(iter.next()?, &empty).ok()?;
                Some((name.to_owned(), color))
            })
            .collect::<Option<HashMap<String, RGBA>>>()
            .expect("failed to parse embedded svg colors")
    };
}

/// Common interface to all color representations
pub trait Color: Copy {
    /// Blend other color on top of this color
    fn blend_over(self, other: Self) -> Self;

    /// Override alpha component of the color
    fn with_alpha(self, alpha: Scalar) -> Self;

    /// Convert color to sRGBA list
    fn to_rgba(self) -> [u8; 4];

    /// Convert color to sRGB list (alpha is discarded)
    fn to_rgb(self) -> [u8; 3] {
        let [r, g, b, _] = self.to_rgba();
        [r, g, b]
    }

    /// Calculate LUMA of the color.
    fn luma(self) -> f32 {
        let [r, g, b] = self.to_rgb();
        0.2126 * (r as f32 / 255.0) + 0.7152 * (g as f32 / 255.0) + 0.0722 * (b as f32 / 255.0)
    }

    /// Pick color that produces the best contrast with self
    fn best_contrast(self, c0: Self, c1: Self) -> Self {
        let luma = self.luma();
        if (luma - c0.luma()).abs() < (luma - c1.luma()).abs() {
            c1
        } else {
            c0
        }
    }

    /// Linear interpolation between self and other colors.
    fn lerp(self, other: Self, t: f32) -> Self;
}

/// sRGBA color packed as [u8; 4]
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Pod, Zeroable)]
pub struct RGBA([u8; 4]);

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

    /// Red channel value
    pub const fn red(self) -> u8 {
        self.0[0]
    }

    /// Green channel value
    pub const fn green(self) -> u8 {
        self.0[1]
    }

    /// Blue channel value
    pub const fn blue(self) -> u8 {
        self.0[2]
    }

    /// Alpha channel value
    pub const fn alpha(self) -> u8 {
        self.0[3]
    }

    /// Parse color or resolve by name
    pub fn from_str_named(color: &str, colors: &HashMap<String, RGBA>) -> Result<Self, ColorError> {
        // parse alpha suffix
        let (color, alpha) = match color.rfind('/') {
            None => (color, None),
            Some(alpha_offset) => {
                let alpha: f32 = color[alpha_offset + 1..]
                    .parse()
                    .map_err(|_| ColorError::InvalidAlpha)?;
                (&color[..alpha_offset], Some(alpha))
            }
        };
        // #RRGGBB(AA)
        let rgba = if color.starts_with('#') && (color.len() == 7 || color.len() == 9) {
            let bytes: &[u8] = color[1..].as_ref();
            let digit = |byte| match byte {
                b'A'..=b'F' => Ok(byte - b'A' + 10),
                b'a'..=b'f' => Ok(byte - b'a' + 10),
                b'0'..=b'9' => Ok(byte - b'0'),
                _ => Err(ColorError::HexExpected),
            };
            let mut hex = bytes
                .chunks(2)
                .map(|pair| Ok(digit(pair[0])? << 4 | digit(pair[1])?));
            RGBA::new(
                hex.next().unwrap_or(Ok(0))?,
                hex.next().unwrap_or(Ok(0))?,
                hex.next().unwrap_or(Ok(0))?,
                hex.next().unwrap_or(Ok(255))?,
            )
        } else {
            colors
                .get(color)
                .copied()
                .ok_or_else(|| ColorError::UnkownColor(color.to_owned()))?
        };
        // apply alpha
        match alpha {
            None => Ok(rgba),
            Some(alpha) => Ok(RGBA::new(
                rgba.red(),
                rgba.green(),
                rgba.blue(),
                (rgba.alpha() as f32 * alpha) as u8,
            )),
        }
    }
}

impl Color for RGBA {
    fn to_rgba(self) -> [u8; 4] {
        self.0
    }

    fn blend_over(self, other: Self) -> Self {
        LinColor::from(self)
            .blend_over(LinColor::from(other))
            .into()
    }

    fn with_alpha(self, alpha: Scalar) -> Self {
        LinColor::from(self).with_alpha(alpha).into()
    }

    fn lerp(self, other: Self, t: f32) -> Self {
        LinColor::from(self).lerp(LinColor::from(other), t).into()
    }
}

impl From<LinColor> for RGBA {
    #[inline(always)]
    fn from(lin: LinColor) -> Self {
        let [r, g, b, _]: [f32; 4] = crate::simd::l2s(lin.unmultiply()).into();
        RGBA::new(
            (r * 255.0 + 0.5) as u8,
            (g * 255.0 + 0.5) as u8,
            (b * 255.0 + 0.5) as u8,
            (lin.alpha() * 255.0 + 0.5) as u8,
        )
    }
}

impl From<[u8; 4]> for RGBA {
    #[inline]
    fn from(rgba: [u8; 4]) -> Self {
        RGBA(rgba)
    }
}

impl From<[u8; 3]> for RGBA {
    #[inline]
    fn from([r, g, b]: [u8; 3]) -> Self {
        RGBA::new(r, g, b, 255)
    }
}

impl fmt::Debug for RGBA {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let [bg_r, bg_g, bg_b] = self.to_rgb();
        let [fg_r, fg_g, fg_b] = self
            .best_contrast(RGBA::new(255, 255, 255, 255), RGBA::new(0, 0, 0, 255))
            .to_rgb();
        write!(
            fmt,
            "\x1b[38;2;{};{};{};48;2;{};{};{}m",
            fg_r, fg_g, fg_b, bg_r, bg_g, bg_b
        )?;
        write!(fmt, "{}", self)?;
        write!(fmt, "\x1b[m")
    }
}

impl fmt::Display for RGBA {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let [r, g, b, a] = self.to_rgba();
        write!(f, "#{:02x}{:02x}{:02x}", r, g, b)?;
        if a != 255 {
            write!(f, "{:02x}", a)?;
        }
        Ok(())
    }
}

impl FromStr for RGBA {
    type Err = ColorError;

    fn from_str(color: &str) -> Result<Self, Self::Err> {
        RGBA::from_str_named(color, &SVG_COLORS)
    }
}

#[cfg(feature = "serde")]
pub struct RGBADeserializer<'a> {
    pub colors: &'a HashMap<String, RGBA>,
}

#[cfg(feature = "serde")]
impl<'de, 'a> DeserializeSeed<'de> for RGBADeserializer<'a> {
    type Value = RGBA;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::Deserialize;
        let color = std::borrow::Cow::<'de, str>::deserialize(deserializer)?;
        RGBA::from_str_named(color.as_ref(), self.colors).map_err(serde::de::Error::custom)
    }
}

/// Alpha premultiplied RGBA color in the linear color space (no gamma correction)
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Default, Pod, Zeroable)]
pub struct LinColor(crate::simd::f32x4);

impl LinColor {
    #[inline(always)]
    pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
        LinColor(f32x4::new(r, g, b, a))
    }

    #[inline(always)]
    pub fn red(self) -> f32 {
        self.0.x0()
    }

    #[inline(always)]
    pub fn green(self) -> f32 {
        self.0.x1()
    }

    #[inline(always)]
    pub fn blue(self) -> f32 {
        self.0.x2()
    }

    #[inline(always)]
    pub fn alpha(self) -> f32 {
        self.0.x3()
    }

    #[inline(always)]
    pub fn distance(self, other: Self) -> f32 {
        let diff = self.unmultiply() - other.unmultiply();
        diff.dot(diff).sqrt()
    }

    /// Linear color is by default pre-multiplied by alpha, this function removes
    /// pre-multiplication.
    #[inline(always)]
    pub fn unmultiply(self) -> f32x4 {
        let alpha = self.alpha();
        if alpha <= 1e-6 {
            // avoid division by zero, check firefox scene.
            f32x4::zero()
        } else {
            self.0 / f32x4::splat(alpha)
        }
    }

    /// Convert into alpha-premultiplied SRGB from Linear RGB
    ///
    /// Used by gradients, do not make public
    #[inline(always)]
    pub(crate) fn into_srgb(self) -> Self {
        Self(crate::simd::l2s(self.unmultiply()) * f32x4::splat(self.alpha()))
    }

    /// Convert into alpha-premultiplied Linear RGB from SRGB
    ///
    /// Used by gradient, do not make public
    #[inline(always)]
    pub(crate) fn into_linear(self) -> Self {
        Self(crate::simd::s2l(self.unmultiply()) * f32x4::splat(self.alpha()))
    }
}

impl Color for LinColor {
    #[inline(always)]
    fn to_rgba(self) -> [u8; 4] {
        RGBA::from(self).to_rgba()
    }

    #[inline(always)]
    fn blend_over(self, other: Self) -> Self {
        other + self * (1.0 - other.alpha())
    }

    #[inline(always)]
    fn with_alpha(self, alpha: Scalar) -> Self {
        self * (alpha as f32)
    }

    #[inline(always)]
    fn lerp(self, other: Self, t: f32) -> Self {
        other * t + self * (1.0 - t)
    }
}

impl Paint for LinColor {
    fn at(&self, _: Point) -> LinColor {
        *self
    }

    fn units(&self) -> Option<Units> {
        None
    }

    fn transform(&self) -> Transform {
        Transform::identity()
    }

    #[cfg(feature = "serde")]
    fn to_json(&self) -> Result<serde_json::Value, crate::SvgParserError> {
        Ok(serde_json::Value::String(self.to_string()))
    }
}

impl Add<Self> for LinColor {
    type Output = Self;

    #[inline(always)]
    fn add(self, other: Self) -> Self::Output {
        Self(self.0 + other.0)
    }
}

impl Mul<f32> for LinColor {
    type Output = Self;

    #[inline(always)]
    fn mul(self, scale: f32) -> Self::Output {
        Self(self.0 * scale)
    }
}

impl From<RGBA> for LinColor {
    fn from(color: RGBA) -> Self {
        // !!! overshots at 1.0
        // let [r, g, b, a] = color.to_rgba();
        // let rgba = f32x4::new(r as f32, g as f32, b as f32, 255.0) * 0.00392156862745098;
        // LinColor(s2l(rgba) * f32x4::splat(a as f32 / 255.0))
        let a = color.alpha() as f32 / 255.0;
        let r = srgb_to_linear(color.red() as f32 / 255.0) * a;
        let g = srgb_to_linear(color.green() as f32 / 255.0) * a;
        let b = srgb_to_linear(color.blue() as f32 / 255.0) * a;
        LinColor::new(r, g, b, a)
    }
}

impl From<LinColor> for [f32; 4] {
    fn from(color: LinColor) -> Self {
        color.0.into()
    }
}

impl FromStr for LinColor {
    type Err = ColorError;

    fn from_str(color: &str) -> Result<Self, Self::Err> {
        Ok(RGBA::from_str(color)?.into())
    }
}

impl fmt::Display for LinColor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        RGBA::from(*self).fmt(f)
    }
}

impl Color for Scalar {
    fn to_rgba(self) -> [u8; 4] {
        let color = (linear_to_srgb(1.0 - (self as f32)) * 255.0 + 0.5) as u8;
        [color, color, color, 255]
    }

    fn blend_over(self, other: Self) -> Self {
        other + self * (1.0 - other)
    }

    fn with_alpha(self, alpha: Scalar) -> Self {
        self * alpha
    }

    fn lerp(self, other: Self, t: f32) -> Self {
        let t = t as Scalar;
        self * (1.0 - t) + other * t
    }
}

/// Convert Linear RGB color component into a SRGB color component.
///
/// It was hard to optimize this function, even current version
/// is slow because of the conditional jump. Lookup table is not working
/// here as well it should be at least 4K in size an not cache friendly.
///
/// Precise implementation
/// ```no_run
/// pub fn linear_to_srgb(value: f32) -> f32 {
///     if value <= 0.0031308 {
///         value * 12.92
///     } else {
///         1.055 * value.powf(1.0 / 2.4) - 0.055
///     }
/// }
/// ```
#[inline]
pub fn linear_to_srgb(x0: f32) -> f32 {
    if x0 <= 0.0031308 {
        x0 * 12.92
    } else {
        // This function is generated by least square fitting of
        // `f(x) = 1.055 * x.powf(1.0 / 2.4) - 0.055` on value [0.0031308..1.0]
        // see `scripts/srgb.py` for details.
        let x1 = x0.sqrt();
        let x2 = x1.sqrt();
        let x3 = x2.sqrt();
        -0.01848558 * x0 + 0.6445592 * x1 + 0.70994765 * x2 - 0.33605254 * x3
    }
}

#[inline]
pub fn srgb_to_linear(value: f32) -> f32 {
    if value <= 0.04045 {
        value / 12.92
    } else {
        ((value + 0.055) / 1.055).powf(2.4)
    }
}

#[derive(Debug, Clone)]
pub enum ColorError {
    HexExpected,
    InvalidAlpha,
    UnkownColor(String),
}

impl fmt::Display for ColorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Color format (#RRGGBB(AA)?|<name>)(/<float alpha>)? :")?;
        match self {
            ColorError::HexExpected => write!(f, "Hex value expected"),
            ColorError::InvalidAlpha => write!(f, "Alpha must be float"),
            ColorError::UnkownColor(name) => write!(f, "Unkown named color: {}", name),
        }
    }
}

impl std::error::Error for ColorError {}

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

    #[test]
    fn test_color_rgba() {
        let c = RGBA::new(1, 2, 3, 4);
        assert_eq!([1, 2, 3, 4], c.to_rgba());
        assert_eq!(1, c.red());
        assert_eq!(2, c.green());
        assert_eq!(3, c.blue());
        assert_eq!(4, c.alpha());
    }

    #[test]
    fn test_color_parse() -> Result<(), ColorError> {
        assert_eq!(RGBA::new(1, 2, 3, 4), "#01020304".parse::<RGBA>()?);
        assert_eq!(RGBA::new(170, 187, 204, 255), "#aabbcc".parse::<RGBA>()?);
        assert_eq!(RGBA::new(0, 0, 0, 255), "#000000".parse::<RGBA>()?);
        assert_eq!(RGBA::new(1, 2, 3, 63), "#010203/.25".parse::<RGBA>()?);
        assert_eq!(RGBA::new(0xff, 0x7f, 0x50, 0xff), "coral".parse::<RGBA>()?);
        assert_eq!(SVG_COLORS.len(), 147);
        Ok(())
    }

    #[test]
    fn test_conversion() -> Result<(), ColorError> {
        let c: RGBA = "#ff804010".parse()?;
        let l: LinColor = c.into();
        let r: RGBA = l.into();
        assert_eq!(c, r);
        Ok(())
    }

    #[test]
    fn test_lin_and_srgb() {
        for i in 0..255 {
            let v = i as f32 / 255.0;
            assert_approx_eq!(v, linear_to_srgb(srgb_to_linear(v)), 1e-4);
            assert_approx_eq!(v, srgb_to_linear(linear_to_srgb(v)), 1e-4);
        }
    }

    #[test]
    fn test_display_parse() -> Result<(), ColorError> {
        let c: RGBA = "#01020304".parse()?;
        assert_eq!(c, RGBA::new(1, 2, 3, 4));
        assert_eq!(c.to_string(), "#01020304");

        let c: RGBA = "#010203".parse()?;
        assert_eq!(c, RGBA::new(1, 2, 3, 255));
        assert_eq!(c.to_string(), "#010203");

        Ok(())
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde() -> Result<(), Box<dyn std::error::Error>> {
        use serde_json::de::StrRead;

        let mut colors = HashMap::new();
        colors.insert("aqua".to_owned(), "#008080".parse()?);

        let mut deserializer = serde_json::Deserializer::new(StrRead::new("\"aqua/.5\""));
        let color = RGBADeserializer { colors: &colors }.deserialize(&mut deserializer)?;
        assert_eq!(color, "#0080807f".parse()?);

        Ok(())
    }
}