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

use numeric::{Clamp, ClampError, ClampFrom, Lerp};

use lum::{Lum, LumAlpha};

/// Red-green-blue color structure.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct Rgb<T> (pub T, pub T, pub T);

impl<T> Rgb<T> {
    #[inline(always)]
    pub fn blue (self) -> T { self.2 }

    #[inline(always)]
    pub fn green (self) -> T { self.1 }

    pub fn luminance (self) -> f64 where f64: ClampFrom<T> {
        use numeric::ClampFrom;
        let r = f64::clamp_from(self.0);
        let g = f64::clamp_from(self.1);
        let b = f64::clamp_from(self.2);
        r * 0.2126 + g * 0.7152 + b * 0.0722
    }

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

    pub fn with_alpha (self, alpha: T) -> Rgba<T> {
        Rgba(self.0, self.1, self.2, alpha)
    }
}

impl<F, T: ClampFrom<F>> ClampFrom<Rgb<F>> for Rgb<T> {
    fn clamp_from (other: Rgb<F>) -> Rgb<T> {
        Rgb(T::clamp_from(other.0), T::clamp_from(other.1), T::clamp_from(other.2))
    }

    fn saturating_clamp_from (other: Rgb<F>) -> Rgb<T> {
        Rgb(T::saturating_clamp_from(other.0),
            T::saturating_clamp_from(other.1),
            T::saturating_clamp_from(other.2))
    }

    fn try_clamp_from (other: Rgb<F>) -> Result<Rgb<T>, ClampError> {
        Ok(Rgb(T::try_clamp_from(other.0)?,
               T::try_clamp_from(other.1)?,
               T::try_clamp_from(other.2)?))
    }
}

impl<T: Clone> From<Lum<T>> for Rgb<T> {
    fn from (other: Lum<T>) -> Rgb<T> { Rgb(other.0.clone(), other.0.clone(), other.0) }
}

impl<S: Lerp<T>, T: Copy> Lerp<T> for Rgb<S> {
    fn lerp (a: Rgb<S>, b: Rgb<S>, t: T) -> Rgb<S> {
        Rgb(S::lerp(a.0, b.0, t), S::lerp(a.1, b.1, t), S::lerp(a.2, b.2, t))
    }
}

/// Red-green-blue-alpha color structure.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
pub struct Rgba<T> (pub T, pub T, pub T, pub T);

impl<T> Rgba<T> {
    #[inline(always)]
    pub fn alpha (self) -> T { self.3 }

    #[inline(always)]
    pub fn blue (self) -> T { self.2 }

    pub fn drop_alpha (self) -> Rgb<T> { Rgb(self.0, self.1, self.2) }

    #[inline(always)]
    pub fn green (self) -> T { self.1 }

    pub fn luminance (self) -> f64 where f64: ClampFrom<T> {
        use numeric::ClampFrom;
        let r = f64::clamp_from(self.0);
        let g = f64::clamp_from(self.1);
        let b = f64::clamp_from(self.2);
        r * 0.2126 + g * 0.7152 + b * 0.0722
    }

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

    pub fn split_alpha (self) -> (Rgb<T>, T) {
        (Rgb(self.0, self.1, self.2), self.3)
    }
}

impl<F, T: ClampFrom<F>> ClampFrom<Rgba<F>> for Rgba<T> {
    fn clamp_from (other: Rgba<F>) -> Rgba<T> {
        Rgba(T::clamp_from(other.0),
             T::clamp_from(other.1),
             T::clamp_from(other.2),
             T::clamp_from(other.3))
    }

    fn saturating_clamp_from (other: Rgba<F>) -> Rgba<T> {
        Rgba(T::saturating_clamp_from(other.0),
             T::saturating_clamp_from(other.1),
             T::saturating_clamp_from(other.2),
             T::saturating_clamp_from(other.3))
    }

    fn try_clamp_from (other: Rgba<F>) -> Result<Rgba<T>, ClampError> {
        Ok(Rgba(T::try_clamp_from(other.0)?,
                T::try_clamp_from(other.1)?,
                T::try_clamp_from(other.2)?,
                T::try_clamp_from(other.3)?))
    }
}

impl<T: Clone + Clamp> From<Lum<T>> for Rgba<T> {
    fn from (other: Lum<T>) -> Rgba<T> {
        Rgba(other.0.clone(), other.0.clone(), other.0, T::clamp_max())
    }
}

impl<T: Clone + Clamp> From<Option<Lum<T>>> for Rgba<T> {
    fn from (other: Option<Lum<T>>) -> Rgba<T> {
        match other {
            None => Rgba(T::clamp_min(), T::clamp_min(), T::clamp_min(), T::clamp_min()),
            Some(other) => Rgba(other.0.clone(), other.0.clone(), other.0, T::clamp_max()),
        }
    }
}

impl<T: Clone> From<LumAlpha<T>> for Rgba<T> {
    fn from (other: LumAlpha<T>) -> Rgba<T> {
        Rgba(other.0.clone(), other.0.clone(), other.0, other.1)
    }
}

impl<T: Clamp> From<Rgb<T>> for Rgba<T> {
    fn from (other: Rgb<T>) -> Rgba<T> { Rgba(other.0, other.1, other.2, T::clamp_max()) }
}

impl<T: Clamp> From<Option<Rgb<T>>> for Rgba<T> {
    fn from (other: Option<Rgb<T>>) -> Rgba<T> {
        match other {
            None => Rgba(T::clamp_min(), T::clamp_min(), T::clamp_min(), T::clamp_min()),
            Some(other) => Rgba(other.0, other.1, other.2, T::clamp_max()),
        }
    }
}

impl<S: Lerp<T>, T: Copy> Lerp<T> for Rgba<S> {
    fn lerp (a: Rgba<S>, b: Rgba<S>, t: T) -> Rgba<S> {
        Rgba(S::lerp(a.0, b.0, t), S::lerp(a.1, b.1, t), S::lerp(a.2, b.2, t), S::lerp(a.3, b.3, t))
    }
}