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
// Copyright (c) 2017, Marty Mills <daggerbot@gmail.com>
// This software is available under the terms of the zlib license.
// See COPYING.md for more information.

use dnum::Lerp;

use clamp::Clamp;

/// Gets the luminance of a non-luminance color type.
pub trait Luminance {
    fn luminance (self) -> f64;
}

/// Luminance color type.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde_", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Lum<T> {
    pub l: T,
}

impl<T> Lum<T> {
    pub fn new (l: T) -> Lum<T> {
        Lum { l }
    }

    pub fn with_alpha (self, a: T) -> Luma<T> {
        Luma { l: self.l, a }
    }
}

impl<T> Clamp for Lum<T>
    where T: Clamp
{
    fn clamp_min () -> Lum<T> { Lum { l: T::clamp_min() } }
    fn clamp_max () -> Lum<T> { Lum { l: T::clamp_max() } }
}

impl<S, T> Lerp<T> for Lum<S>
    where S: Lerp<T>
{
    fn lerp (a: Lum<S>, b: Lum<S>, t: T) -> Lum<S> {
        Lum { l: S::lerp(a.l, b.l, t) }
    }
}

/// Luminance-alpha color type.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde_", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Luma<T> {
    pub l: T,
    pub a: T,
}

impl<T> Luma<T> {
    pub fn drop_alpha (self) -> Lum<T> {
        Lum { l: self.l }
    }

    pub fn new (l: T, a: T) -> Luma<T> {
        Luma { l, a }
    }

    pub fn split_alpha (self) -> (Lum<T>, T) {
        (Lum { l: self.l }, self.a)
    }
}

impl<T> Clamp for Luma<T>
    where T: Clamp
{
    fn clamp_min () -> Luma<T> { Luma { l: T::clamp_min(), a: T::clamp_min() } }
    fn clamp_max () -> Luma<T> { Luma { l: T::clamp_max(), a: T::clamp_max() } }
}

impl<S, T> Lerp<T> for Luma<S>
    where S: Lerp<T>, T: Copy
{
    fn lerp (a: Luma<S>, b: Luma<S>, t: T) -> Luma<S> {
        Luma {
            l: S::lerp(a.l, b.l, t),
            a: S::lerp(a.a, b.a, t),
        }
    }
}