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
use num::Float;

use std::ops::{Add, Sub, Mul, Div};

use {Alpha, Rgb, Xyz, Yxy};
use {Limited, Mix, Shade, FromColor, Blend, ComponentWise};
use {clamp, flt};
use blend::PreAlpha;

///Linear luminance with an alpha component. See the [`Lumaa` implementation in `Alpha`](struct.Alpha.html#Lumaa).
pub type Lumaa<T = f32> = Alpha<Luma<T>, T>;

///Linear luminance.
///
///Luma is a purely gray scale color space, which is included more for
///completeness than anything else, and represents how bright a color is
///perceived to be. It's basically the `Y` component of [CIE
///XYZ](struct.Xyz.html). The lack of any form of hue representation limits
///the set of operations that can be performed on it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Luma<T: Float = f32> {
    ///The lightness of the color. 0.0 is black and 1.0 is white.
    pub luma: T,
}

impl<T: Float> Luma<T> {
    ///Linear luminance.
    pub fn new(luma: T) -> Luma<T> {
        Luma {
            luma: luma,
        }
    }

    ///Linear luminance from an 8 bit value.
    pub fn new_u8(luma: u8) -> Luma<T> {
        Luma {
            luma: flt::<T,_>(luma) / flt(255.0),
        }
    }
}

///<span id="Lumaa"></span>[`Lumaa`](type.Lumaa.html) implementations.
impl<T: Float> Alpha<Luma<T>, T> {
    ///Linear luminance with transparency.
    pub fn new(luma: T, alpha: T) -> Lumaa<T> {
        Alpha {
            color: Luma::new(luma),
            alpha: alpha,
        }
    }

    ///Linear luminance and transparency from 8 bit values.
    pub fn new_u8(luma: u8, alpha: u8) -> Lumaa<T> {
        Alpha {
            color: Luma::new_u8(luma),
            alpha: flt::<T,_>(alpha) / flt(255.0),
        }
    }
}

impl<T: Float> FromColor<T> for Luma<T> {
    fn from_xyz(xyz: Xyz<T>) -> Self {
        Luma {
            luma: xyz.y,
        }
    }

    fn from_yxy(yxy: Yxy<T>) -> Self {
        Luma {
            luma: yxy.luma,
        }
    }

    fn from_rgb(rgb: Rgb<T>) -> Self {
        Luma {
            luma: rgb.red * flt(0.2126) + rgb.green * flt(0.7152) + rgb.blue * flt(0.0722),
        }
    }

    fn from_luma(luma: Luma<T>) -> Self {
        luma
    }

}

impl<T: Float> Limited for Luma<T> {
    fn is_valid(&self) -> bool {
        self.luma >= T::zero() && self.luma <= T::one()
    }

    fn clamp(&self) -> Luma<T> {
        let mut c = *self;
        c.clamp_self();
        c
    }

    fn clamp_self(&mut self) {
        self.luma = clamp(self.luma, T::zero(), T::one());
    }
}

impl<T: Float> Mix for Luma<T> {
    type Scalar = T;

    fn mix(&self, other: &Luma<T>, factor: T) -> Luma<T> {
        let factor = clamp(factor, T::zero(), T::one());

        Luma {
            luma: self.luma + factor * (other.luma - self.luma),
        }
    }
}

impl<T: Float> Shade for Luma<T> {
    type Scalar = T;

    fn lighten(&self, amount: T) -> Luma<T> {
        Luma {
            luma: (self.luma + amount).max(T::zero()),
        }
    }
}

impl<T: Float> Blend for Luma<T> {
    type Color = Luma<T>;

    fn into_premultiplied(self) -> PreAlpha<Luma<T>, T> {
        Lumaa::from(self).into()
    }

    fn from_premultiplied(color: PreAlpha<Luma<T>, T>) -> Self {
        Lumaa::from(color).into()
    }
}

impl<T: Float> ComponentWise for Luma<T> {
    type Scalar = T;

    fn component_wise<F: FnMut(T, T) -> T>(&self, other: &Luma<T>, mut f: F) -> Luma<T> {
        Luma {
            luma: f(self.luma, other.luma),
        }
    }

    fn component_wise_self<F: FnMut(T) -> T>(&self, mut f: F) -> Luma<T> {
        Luma {
            luma: f(self.luma),
        }
    }
}

impl<T: Float> Default for Luma<T> {
    fn default() -> Luma<T> {
        Luma::new(T::zero())
    }
}

impl<T: Float> Add<Luma<T>> for Luma<T> {
    type Output = Luma<T>;

    fn add(self, other: Luma<T>) -> Luma<T> {
        Luma {
            luma: self.luma + other.luma,
        }
    }
}

impl<T: Float> Add<T> for Luma<T> {
    type Output = Luma<T>;

    fn add(self, c: T) -> Luma<T> {
        Luma {
            luma: self.luma + c,
        }
    }
}

impl<T: Float> Sub<Luma<T>> for Luma<T> {
    type Output = Luma<T>;

    fn sub(self, other: Luma<T>) -> Luma<T> {
        Luma {
            luma: self.luma - other.luma,
        }
    }
}

impl<T: Float> Sub<T> for Luma<T> {
    type Output = Luma<T>;

    fn sub(self, c: T) -> Luma<T> {
        Luma {
            luma: self.luma - c,
        }
    }
}

impl<T: Float> Mul<Luma<T>> for Luma<T> {
    type Output = Luma<T>;

    fn mul(self, other: Luma<T>) -> Luma<T> {
        Luma {
            luma: self.luma * other.luma,
        }
    }
}

impl<T: Float> Mul<T> for Luma<T> {
    type Output = Luma<T>;

    fn mul(self, c: T) -> Luma<T> {
        Luma {
            luma: self.luma * c,
        }
    }
}

impl<T: Float> Div<Luma<T>> for Luma<T> {
    type Output = Luma<T>;

    fn div(self, other: Luma<T>) -> Luma<T> {
        Luma {
            luma: self.luma / other.luma,
        }
    }
}

impl<T: Float> Div<T> for Luma<T> {
    type Output = Luma<T>;

    fn div(self, c: T) -> Luma<T> {
        Luma {
            luma: self.luma / c,
        }
    }
}

#[cfg(test)]
mod test {
    use Luma;

    #[test]
    fn ranges() {
        assert_ranges!{
            Luma;
            limited {
                luma: 0.0 => 1.0
            }
            limited_min {}
            unlimited {}
        }
    }
}