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
use crate::{Paint, Point, Scalar, Transform, Units};
use std::{
    fmt,
    ops::{Add, Mul},
    str::FromStr,
};

pub trait Color {
    fn blend_over(&self, other: &Self) -> Self;

    fn with_alpha(&self, alpha: Scalar) -> Self;

    fn to_rgba(&self) -> [u8; 4];

    fn to_rgb(&self) -> [u8; 3]
    where
        Self: Sized,
    {
        let [r, g, b, _] = self.to_rgba();
        [r, g, b]
    }
}

/// ABGR color packed as u32 value (most of the platforms are little-endian)
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ColorU8(u32);

impl ColorU8 {
    pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self(((a as u32) << 24) | ((b as u32) << 16) | ((g as u32) << 8) | (r as u32))
    }

    pub const fn alpha(self) -> u8 {
        ((self.0 >> 24) & 0xff) as u8
    }

    pub const fn blue(self) -> u8 {
        ((self.0 >> 16) & 0xff) as u8
    }

    pub const fn green(self) -> u8 {
        ((self.0 >> 8) & 0xff) as u8
    }

    pub const fn red(self) -> u8 {
        (self.0 & 0xff) as u8
    }

    /*
    fn blend_over(self, other: Self) -> Self {
        // Sa - source alpha
        // Sc - source color
        // Da - destination alpha
        // Dc - destination color
        // Output color would be (prime means premultiplied):
        //   Oc' = Sc * Sa + Dc * Da * (1 - Sa)
        //   Oa = Sa + Da * (1 - Sa)
        //   Oc = Oc' / Oa
        //
        //   Oc' = lerp(Dc * Da, Sc, Sa)
        //   Oa = Sa + Da - Sa * Da
        let da = u32::from(self.alpha());
        let sa = u32::from(other.alpha());
        let _oa = sa + da - mul_u8(sa, da);

        let dc = self.0;
        let sc = other.0;
        let _oc = lerp_u8x4(mul_u8x4(dc, da), sc, sa);
        todo!()
    }
    */
}

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

    fn blend_over(&self, _other: &Self) -> Self {
        todo!()
    }

    fn with_alpha(&self, _alpha: Scalar) -> Self {
        todo!()
    }
}

impl From<LinColor> for ColorU8 {
    fn from(lin: LinColor) -> Self {
        let LinColor([r, g, b, a]) = lin;
        if a < std::f32::EPSILON {
            return ColorU8::default();
        }
        let r = (linear_to_srgb(r / a) * 255.0 + 0.5) as u8;
        let g = (linear_to_srgb(g / a) * 255.0 + 0.5) as u8;
        let b = (linear_to_srgb(b / a) * 255.0 + 0.5) as u8;
        let a = (a * 255.0 + 0.5) as u8;
        ColorU8::new(r, g, b, a)
    }
}

impl fmt::Debug for ColorU8 {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("ColorU8")
            .field("r", &self.red())
            .field("g", &self.green())
            .field("b", &self.blue())
            .field("a", &self.alpha())
            .finish()
    }
}

impl FromStr for ColorU8 {
    type Err = ColorError;

    fn from_str(color: &str) -> Result<Self, Self::Err> {
        if color.starts_with('#') && (color.len() == 7 || color.len() == 9) {
            // #RRGGBB(AA)
            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])?));
            Ok(ColorU8::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 {
            Err(ColorError::HexExpected)
        }
    }
}

/// Alpha premultiplied RGBA color in the liniar color space (no gamma correction)
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct LinColor([f32; 4]);

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

    pub const fn red(&self) -> f32 {
        self.0[0]
    }

    pub const fn green(&self) -> f32 {
        self.0[1]
    }

    pub const fn blue(&self) -> f32 {
        self.0[2]
    }

    pub const fn alpha(&self) -> f32 {
        self.0[3]
    }

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

    /// Temporary convert to srgb color space
    ///
    /// Used by gradients, do not make public
    pub(crate) fn into_srgb(self) -> Self {
        let Self([r, g, b, a]) = self;
        Self::new(
            linear_to_srgb(r * a),
            linear_to_srgb(g * a),
            linear_to_srgb(b * a),
            a,
        )
    }

    /// Convert back from temporary srgb color space
    ///
    /// Used by gradient, do not make public
    pub(crate) fn into_linear(self) -> Self {
        let Self([r, g, b, a]) = self;
        Self::new(
            srgb_to_linear(r / a),
            srgb_to_linear(g / a),
            srgb_to_linear(b / a),
            a,
        )
    }
}

impl Color for LinColor {
    fn to_rgba(&self) -> [u8; 4] {
        ColorU8::from(*self).to_rgba()
    }

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

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

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

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

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

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

    #[inline]
    fn add(self, other: Self) -> Self::Output {
        let Self([r0, g0, b0, a0]) = self;
        let Self([r1, g1, b1, a1]) = other;
        Self([r0 + r1, g0 + g1, b0 + b1, a0 + a1])
    }
}

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

    #[inline]
    fn mul(self, scale: f32) -> Self::Output {
        let Self([r, g, b, a]) = self;
        Self([scale * r, scale * g, scale * b, scale * a])
    }
}

impl From<ColorU8> for LinColor {
    fn from(color: ColorU8) -> Self {
        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 FromStr for LinColor {
    type Err = ColorError;

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

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
    }
}

/// 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.
#[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 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 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,
}

impl fmt::Display for ColorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ColorError::HexExpected => {
                write!(f, "Color expected to be #RRGGBB(AA) in hexidemical format")
            }
        }
    }
}

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

/*
const MASK_LOW: u32 = 0x00FF00FF;

/// Calculate `[_, a1, _, a3] * b / 255`, where `a{0-3}` and `b` are `u8`
///
/// Those optimisation comes from for these two formulas:
///   1. `a + ar + ar^2 ... = a / (1 - r)` for all r in [0..1)
///   2. `t / 255 = (t / 256) / (1 - r)` where if `r = 1 / 256`
///
/// Applying this 1 and 2, takin only first two arguments from 1.
///   `(v >> 8) + (v >> 16)` => `((v >> 8) + v) >> 8` where `v = a * b`
///
/// Basically we get `v / 255 = ((v >> 8) + v) >> 8`
///
/// This function also doing this operation at on two u8 at once by means of masking
/// and then recomposing everyting in one value.
///
/// References:
///   - [Image Compasiting Fundamentals](https://www.cs.princeton.edu/courses/archive/fall00/cs426/papers/smith95a.pdf)
///   - [Double blend trick](http://stereopsis.com/doubleblend.html)
fn mul_u8x2(a: u32, b: u32) -> u32 {
    let m0 = (a & MASK_LOW) * b + 0x00800080;
    ((((m0 >> 8) & MASK_LOW) + m0) >> 8) & MASK_LOW
}

fn mul_u8(a: u32, b: u32) -> u32 {
    let t = (a * b) + 0x80;
    ((t >> 8) + t) >> 8
}

pub fn mul_u8x4(a: u32, b: u32) -> u32 {
    let low = mul_u8x2(a, b);
    let high = mul_u8x2(a >> 8, b) << 8;
    low + high
}

fn lerp_u8x2(a: u32, b: u32, t: u32) -> u32 {
    let a_low = a & MASK_LOW;
    let b_low = b & MASK_LOW;
    let delta_low = b_low.wrapping_sub(a_low).wrapping_mul(t) >> 8;
    (a_low + delta_low) & MASK_LOW
}

pub fn lerp_u8x4(a: u32, b: u32, t: u32) -> u32 {
    let low = lerp_u8x2(a, b, t);
    let high = lerp_u8x2(a >> 8, b >> 8, t) << 8;
    low | high
}
*/

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

    #[test]
    fn test_color_u8() {
        let c = ColorU8::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_u8_parse() -> Result<(), ColorError> {
        assert_eq!(ColorU8::new(1, 2, 3, 4), "#01020304".parse::<ColorU8>()?);
        assert_eq!(
            ColorU8::new(170, 187, 204, 255),
            "#aabbcc".parse::<ColorU8>()?
        );
        assert_eq!(ColorU8::new(0, 0, 0, 255), "#000000".parse::<ColorU8>()?);
        Ok(())
    }

    /*
    #[test]
    fn test_mul_u8x4() {
        assert_eq!(mul_u8x4(0xff804020, 0x20), 0x20100804);
    }
    */
}