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
use super::{deg, percent, Angle, Color, Ratio, HSL, HSLA};
use std::fmt;

/// Constructs a RGB Color from numerical values, similar to the
/// [`rgb` function](css-rgb) in CSS.
///
/// # Example
/// ```
/// use farver::{Color, rgb};
///
/// let salmon = rgb(250, 128, 114);
///
/// assert_eq!(salmon.to_css(), "rgb(250, 128, 114)");
/// ```
///
/// [css-rgb]: https://www.w3.org/TR/css-color-3/#rgb-color
pub fn rgb(r: u8, g: u8, b: u8) -> RGB {
    RGB {
        r: Ratio::from_u8(r),
        g: Ratio::from_u8(g),
        b: Ratio::from_u8(b),
    }
}

/// Constructs a RGB Color from numerical values, similar to the
/// [`rgba` function](css-rgba) in CSS.
///
/// The alpha value is expressed as a float. Values outside of the
/// 0.0-1.0 range will cause a panic.
///
/// # Example
/// ```
/// use farver::{Color, rgba};
///
/// let salmon = rgba(250, 128, 114, 0.50);
///
/// assert_eq!(salmon.to_css(), "rgba(250, 128, 114, 0.50)");
/// ```
///
/// [css-rgba]: https://www.w3.org/TR/css-color-3/#rgba-color
pub fn rgba(r: u8, g: u8, b: u8, a: f32) -> RGBA {
    RGBA {
        r: Ratio::from_u8(r),
        g: Ratio::from_u8(g),
        b: Ratio::from_u8(b),
        a: Ratio::from_f32(a),
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// A struct to represent how much red, green, and blue should be added to create a color.
///
/// Valid values for r, g, and b must be a u8 between `0-255`, represented as a `Ratio`.
///
/// For more, see the [CSS Color Spec](https://www.w3.org/TR/2018/REC-css-color-3-20180619/#rgb-color).
pub struct RGB {
    // red
    pub r: Ratio,

    // green
    pub g: Ratio,

    // blue
    pub b: Ratio,
}

impl fmt::Display for RGB {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "rgb({}, {}, {})",
            self.r.as_u8(),
            self.g.as_u8(),
            self.b.as_u8()
        )
    }
}

impl Color for RGB {
    type Alpha = RGBA;

    fn to_css(self) -> String {
        self.to_string()
    }

    fn to_hex(self) -> String {
        format!(
            "#{:02x}{:02x}{:02x}",
            self.r.as_u8(),
            self.g.as_u8(),
            self.b.as_u8()
        )
    }

    fn to_rgb(self) -> RGB {
        self
    }

    fn to_rgba(self) -> RGBA {
        let RGB { r, g, b } = self;

        RGBA {
            r,
            g,
            b,
            a: percent(100),
        }
    }

    /// The algorithm for converting from rgb to hsl format, which determines
    /// the equivalent luminosity, saturation, and hue.
    fn to_hsl(self) -> HSL {
        self.to_rgba().to_hsl()
    }

    fn to_hsla(self) -> HSLA {
        self.to_rgba().to_hsla()
    }

    fn saturate(self, amount: Ratio) -> Self {
        self.to_rgba().saturate(amount).to_rgb()
    }

    fn desaturate(self, amount: Ratio) -> Self {
        self.to_rgba().desaturate(amount).to_rgb()
    }

    fn lighten(self, amount: Ratio) -> Self {
        self.to_rgba().lighten(amount).to_rgb()
    }

    fn darken(self, amount: Ratio) -> Self {
        self.to_rgba().darken(amount).to_rgb()
    }

    fn fadein(self, amount: Ratio) -> RGBA {
        self.to_rgba().fadein(amount)
    }

    fn fadeout(self, amount: Ratio) -> RGBA {
        self.to_rgba().fadeout(amount)
    }

    fn fade(self, amount: Ratio) -> RGBA {
        self.to_rgba().fade(amount)
    }

    fn spin(self, amount: Angle) -> Self {
        self.to_rgba().spin(amount).to_rgb()
    }

    fn mix<T: Color>(self, other: T, weight: Ratio) -> RGBA {
        self.to_rgba().mix(other, weight)
    }

    fn tint(self, weight: Ratio) -> Self {
        self.to_rgba().tint(weight).to_rgb()
    }

    fn shade(self, weight: Ratio) -> Self {
        self.to_rgba().shade(weight).to_rgb()
    }

    fn greyscale(self) -> Self {
        self.to_rgba().greyscale().to_rgb()
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
/// A struct to represent how much red, green, and blue should be added to create a color.
/// Also handles alpha specifications.
///
/// Valid values for r, g, and b must be a u8 between `0-255`, represented as a `Ratio`.
/// Alpha (a) values must fall between `0-255`.
///
/// For more, see the [CSS Color Spec](https://www.w3.org/TR/2018/REC-css-color-3-20180619/#rgba-color).
pub struct RGBA {
    // red
    pub r: Ratio,

    // green
    pub g: Ratio,

    // blue
    pub b: Ratio,

    // alpha
    pub a: Ratio,
}

impl fmt::Display for RGBA {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "rgba({}, {}, {}, {:.02})",
            self.r.as_u8(),
            self.g.as_u8(),
            self.b.as_u8(),
            self.a.as_f32()
        )
    }
}

impl Color for RGBA {
    type Alpha = Self;

    fn to_css(self) -> String {
        self.to_string()
    }

    fn to_hex(self) -> String {
        format!(
            "#{:02x}{:02x}{:02x}{:02x}",
            self.r.as_u8(),
            self.g.as_u8(),
            self.b.as_u8(),
            self.a.as_u8()
        )
    }

    fn to_rgb(self) -> RGB {
        let RGBA { r, g, b, .. } = self;
        RGB { r, g, b }
    }

    fn to_rgba(self) -> RGBA {
        self
    }

    fn to_hsl(self) -> HSL {
        self.to_hsla().to_hsl()
    }

    fn to_hsla(self) -> HSLA {
        let RGBA { r, g, b, a } = self;

        // If r, g, and b are the same, the color is a shade of grey (between
        // black and white), with no hue or saturation. In that situation, there
        // is no saturation or hue, and we can use any value to determine luminosity.
        if r == g && g == b {
            return HSLA {
                h: deg(0),
                s: percent(0),
                l: r,
                a,
            };
        }

        // Otherwise, to determine luminosity, we conver the RGB values into a
        // percentage value, find the max and the min of those values, sum them
        // together, and divide by 2.
        let r = self.r.as_f32();
        let g = self.g.as_f32();
        let b = self.b.as_f32();

        let max = if r > g && r > b {
            r
        } else if g > b {
            g
        } else {
            b
        };

        let min = if r < g && r < b {
            r
        } else if g < b {
            g
        } else {
            b
        };

        let luminosity = (max + min) / 2.0;

        // To find the saturation, we look at the max and min values.
        // If the max and the min are the same, there is no saturation to the color.
        // Otherwise, we calculate the saturation based on if the luminosity is
        // greater than or less than 0.5.
        let saturation = if max == min {
            0.0
        } else if luminosity < 0.5 {
            (max - min) / (max + min)
        } else {
            (max - min) / (2.0 - (max + min))
        };

        // To calculate the hue, we look at which value (r, g, or b) is the max.
        // Based on that, we subtract the difference between the other two values,
        // adding 120 or 240 deg to account for the degrees on the color wheel, and
        // then dividing that by the difference between the max and the min values.
        // Finally, we multiply the hue value by 60 to convert it to degrees on
        // the color wheel, accounting for negative hues as well.
        let hue = if max == r {
            60.0 * (g - b) / (max - min)
        } else if max == g {
            120.0 + 60.0 * (b - r) / (max - min)
        } else {
            240.0 + 60.0 * (r - g) / (max - min)
        };

        HSLA {
            h: deg(hue.round() as i32),
            s: Ratio::from_f32(saturation),
            l: Ratio::from_f32(luminosity),
            a,
        }
    }

    fn saturate(self, amount: Ratio) -> Self {
        self.to_hsla().saturate(amount).to_rgba()
    }

    fn desaturate(self, amount: Ratio) -> Self {
        self.to_hsla().desaturate(amount).to_rgba()
    }

    fn lighten(self, amount: Ratio) -> Self {
        self.to_hsla().lighten(amount).to_rgba()
    }

    fn darken(self, amount: Ratio) -> Self {
        self.to_hsla().darken(amount).to_rgba()
    }

    fn fadein(self, amount: Ratio) -> Self {
        self.fade(self.a + amount)
    }

    fn fadeout(self, amount: Ratio) -> Self {
        self.fade(self.a - amount)
    }

    fn fade(self, amount: Ratio) -> Self {
        let RGBA { r, g, b, .. } = self;
        RGBA { r, g, b, a: amount }
    }

    fn spin(self, amount: Angle) -> Self {
        self.to_hsla().spin(amount).to_rgba()
    }

    // This algorithm takes into account both the user-provided weight (w) and
    // the difference between the alpha values of the two colors (a) to determine
    // the weighted average of the two colors.
    // Taken from Sass's implementation (http://sass-lang.com/documentation/Sass/Script/Functions.html#mix-instance_method)
    fn mix<T: Color>(self, other: T, weight: Ratio) -> Self {
        let RGBA {
            r: r_lhs,
            g: g_lhs,
            b: b_lhs,
            a: a_lhs,
        } = self;

        let RGBA {
            r: r_rhs,
            g: g_rhs,
            b: b_rhs,
            a: a_rhs,
        } = other.to_rgba();

        // Convert weight into a decimal, and then scale it so that it falls between a range of [-1, 1].
        let w = (weight.as_f32() * 2.0) - 1.0;

        // Find the difference between the left and right side's alphas (somewhere between [-1, 1]).
        let a = a_lhs.as_f32() - a_rhs.as_f32();

        // Find the combined rgb_weight, taking into account the user's passed-in weight and alpha (range of [-1, 1]).
        let rgb_weight = if w * a == -1.0 {
            w
        } else {
            (w + a) / (1.0 + w * a)
        };

        // Find the combined rgb weight, scaling it to fall in a range bewtween [0, 1].
        let rgb_weight = (rgb_weight + 1.0) / 2.0;

        // Convert left and right side's weights into Ratios.
        let rgb_weight_lhs = Ratio::from_f32(rgb_weight);
        let rgb_weight_rhs = Ratio::from_f32(1.0) - rgb_weight_lhs;

        let alpha_weight_lhs = weight;
        let alpha_weight_rhs = Ratio::from_f32(1.0) - alpha_weight_lhs;

        RGBA {
            r: (r_lhs * rgb_weight_lhs) + (r_rhs * rgb_weight_rhs),
            g: (g_lhs * rgb_weight_lhs) + (g_rhs * rgb_weight_rhs),
            b: (b_lhs * rgb_weight_lhs) + (b_rhs * rgb_weight_rhs),
            a: (a_lhs * alpha_weight_lhs) + (a_rhs * alpha_weight_rhs),
        }
    }

    fn tint(self, weight: Ratio) -> Self {
        self.mix(rgb(255, 255, 255), weight)
    }

    fn shade(self, weight: Ratio) -> Self {
        self.mix(rgb(0, 0, 0), weight)
    }

    fn greyscale(self) -> Self {
        self.to_hsla().greyscale().to_rgba()
    }
}