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
use prelude::*;
use core::{Uniform, AsUniform};

/// A color value consisting of four floating point values for the color channels red, green, blue
/// and alpha.
///
/// Various drawing methods accept color instances to be used as multiplicators in the drawing
/// process, e.g. [`Sprite::draw()`](struct.Sprite.html#method.draw) allows multiplying the sprite-
/// texture's color channels by given color.
#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))]
#[derive(Copy, Clone, Default, PartialEq)]
pub struct Color(pub f32, pub f32, pub f32, pub f32);

impl Color {

    pub const TRANSPARENT: Color = Color(0.0, 0.0, 0.0, 0.0);
    pub const WHITE: Color = Color(1.0, 1.0, 1.0, 1.0);
    pub const BLACK: Color = Color(0.0, 0.0, 0.0, 1.0);
    pub const RED: Color = Color(1.0, 0.0, 0.0, 1.0);
    pub const GREEN: Color = Color(0.0, 1.0, 0.0, 1.0);
    pub const BLUE: Color = Color(0.0, 0.0, 1.0, 1.0);
    pub const YELLOW: Color = Color(1.0, 1.0, 0.0, 1.0);
    pub const CYAN: Color = Color(0.0, 1.0, 1.0, 1.0);
    pub const MAGENTA: Color = Color(1.0, 0.0, 1.0, 1.0);

    /// Creates a new instance with color channels set to one and the alpha channel set to given value.
    pub fn alpha(alpha: f32) -> Color {
        Color(1.0, 1.0, 1.0, alpha)
    }

    /// Creates a new instance with color channels set to zero and the alpha channel set to given value.
    pub fn alpha_mask(alpha: f32) -> Color {
        Color(0.0, 0.0, 0.0, alpha)
    }

    /// Creates a new instance with all channels set to given value.
    pub fn alpha_pm(alpha: f32) -> Color {
        Color(alpha, alpha, alpha, alpha)
    }

    /// Creates a new instance with color channels set to given value and the alpha channel set to one.
    pub fn lightness(value: f32) -> Color {
        Color(value, value, value, 1.0)
    }

    /// Creates a new instance from given HSL (range 0.0 - 1.0)
    pub fn from_hsl(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Color {
        if saturation == 0.0 {
            Color(lightness, lightness, lightness, alpha)
        } else {
            let q = if lightness < 0.5 {
                lightness * (1.0 + saturation)
            } else {
                lightness + saturation - lightness * saturation
            };
            let p = 2.0 * lightness - q;
            Color(
                Self::hue_to_rgb(p, q, hue + 1.0 / 3.0),
                Self::hue_to_rgb(p, q, hue),
                Self::hue_to_rgb(p, q, hue - 1.0 / 3.0),
                alpha
            )
        }
    }

    /// Creates a new instance from given color-temperature (~1000 to ~40000).
    ///
    /// Based on http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
    pub fn from_temperature(temperature: f32, alpha: f32) -> Color {

        let value = (temperature / 100.0).floor();
        let red;
        let green;
        let blue;

        if value <= 66.0 {
            red = 255;
            green = (99.4708025861 * value.ln() - 161.1195681661) as i32;
        } else {
            red = (329.698727466 * (value - 60.0).powf(-0.1332047592)) as i32;
            green = (288.1221695283 * (value - 60.0).powf(-0.0755148492)) as i32;
        }

        if value >= 66.0 {
            blue = 255;
        } else if value <= 19.0 {
            blue = 0;
        } else {
            blue = (138.5177312231 * (value - 10.0).ln() - 305.0447927307) as i32;
        }

        Color(
            cmp::max(0, cmp::min(255, red)) as f32 / 255.0,
            cmp::max(0, cmp::min(255, green)) as f32 / 255.0,
            cmp::max(0, cmp::min(255, blue)) as f32 / 255.0,
            alpha
        )
    }

    /// Returns value of the instance's red channel.
    pub fn r(self: &Self) -> f32 {
        self.0
    }

    /// Returns value of the instance's green channel.
    pub fn g(self: &Self) -> f32 {
        self.1
    }

    /// Returns value of the instance's blue channel.
    pub fn b(self: &Self) -> f32 {
        self.2
    }

    /// Returns value of the instance's alpha channel.
    pub fn a(self: &Self) -> f32 {
        self.3
    }

    /// Sets the instance's channels from another color object.
    pub fn set(self: &mut Self, value: Color) {
        self.0 = value.0;
        self.1 = value.1;
        self.2 = value.2;
        self.3 = value.3;
    }

    /// Sets a value for the instance's red channel
    pub fn set_r(self: &mut Self, value: f32) -> &mut Color {
        self.0 = value;
        self
    }

    /// Sets a value for the instance's green channel.
    pub fn set_g(self: &mut Self, value: f32) -> &mut Color {
        self.1 = value;
        self
    }

    /// Sets a value for the instance's blue channel.
    pub fn set_b(self: &mut Self, value: f32) -> &mut Color {
        self.2 = value;
        self
    }

    /// Sets a value for the instance's alpha channel.
    pub fn set_a(self: &mut Self, value: f32) -> &mut Color {
        self.3 = value;
        self
    }

    /// Multiplies the instance's color channels by given scaling factor. Does not modify alpha.
    pub fn scale(self: &mut Self, scaling: f32) -> &mut Color {
        self.0 *= scaling;
        self.1 *= scaling;
        self.2 *= scaling;
        self
    }

    // Returns new instance with alpha applied to all color channels.
    pub fn to_pm(self: &Self) -> Color {
        Color(self.0 * self.3, self.1 * self.3, self.2 * self.3, self.3)
    }

    /// Hue to rgb helper function uses by hsl.
    fn hue_to_rgb(p: f32, q: f32, mut hue: f32) -> f32 {
        if hue < 0.0 {
            hue += 1.0;
        }
        if hue > 1.0 {
            hue -= 1.0;
        }
        if hue < 1.0 / 6.0 {
            return p + (q - p) * 6.0 * hue;
        }
        if hue < 1.0 / 2.0 {
            return q;
        }
        if hue < 2.0 / 3.0 {
            return p + (q - p) * (2.0/3.0 - hue) * 6.0;
        }
        return p;
    }
}

impl<T> From<(T, T, T, T)> for Color where f32: From<T> {
    fn from(source: (T, T, T, T)) -> Self {
        Color(source.0.into(), source.1.into(), source.2.into(), source.3.into())
    }
}

impl<T> From<[ T; 4 ]> for Color where T: Copy, f32: From<T> {
    fn from(source: [ T; 4 ]) -> Self {
        Color(source[0].into(), source[1].into(), source[2].into(), source[3].into())
    }
}

impl From<Color> for [ f32; 4 ] {
    fn from(source: Color) -> Self {
        [ source.0, source.1, source.2, source.3 ]
    }
}

impl<'a> From<&'a Color> for [ f32; 4 ] {
    fn from(source: &'a Color) -> Self {
        [ source.0, source.1, source.2, source.3 ]
    }
}

impl From<Color> for (f32, f32, f32, f32) {
    fn from(source: Color) -> Self {
        (source.0, source.1, source.2, source.3)
    }
}

impl<'a> From<&'a Color> for (f32, f32, f32, f32) {
    fn from(source: &'a Color) -> Self {
        (source.0, source.1, source.2, source.3)
    }
}

impl AsUniform for Color {
    fn as_uniform(self: &Self) -> Uniform {
        Uniform::Vec4([ self.0, self.1, self.2, self.3 ])
    }
}

impl fmt::Debug for Color {
    fn fmt(self: &Self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Color({}, {}, {}, {})", self.0, self.1, self.2, self.3)
    }
}