plottery_lib 0.12.0

Core geometry library of Plottery, a creative coding framework for generative vector graphics and pen plotting.
Documentation
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use serde::{Deserialize, Serialize};

use crate::{Angle, LARGE_EPSILON};

use super::color_names::{ColorName, COLOR_NAMES};

/// A color represented in RGB (Red, Green, Blue) format.
///
/// Each component (r, g, b) is a float in range \[0-1\].
///
/// ### Example
/// ```
/// # use plottery_lib::*;
/// let red = ColorRgb::red();
/// let custom_color = ColorRgb::new(0.5, 0.7, 0.3);
/// let hex_string = custom_color.hex();
/// ```
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct ColorRgb {
    pub r: f32,
    pub g: f32,
    pub b: f32,
}

impl ColorRgb {
    /// Creates a new RGB color with the specified red, green, and blue components.
    ///
    /// Each component should be between 0.0 and 1.0.
    pub fn new(r: f32, g: f32, b: f32) -> Self {
        Self { r, g, b }
    }
    /// Converts this RGB color to HSV format.
    pub fn hsv(&self) -> ColorHsv {
        (*self).into()
    }
    /// Converts this RGB color to the perceptual Oklab format.
    pub fn oklab(&self) -> ColorOklab {
        (*self).into()
    }
    /// Returns the hexadecimal representation of this color.
    /// ```
    /// # use plottery_lib::*;
    /// assert_eq!(ColorRgb::red().hex(), "#ff0000");
    /// assert_eq!(ColorRgb::black().hex(), "#000000");
    /// assert_eq!(ColorRgb::white().hex(), "#ffffff");
    /// assert_eq!(ColorRgb::yellow().hex(), "#ffff00");
    /// assert_eq!(ColorRgb::blue().hex(), "#0000ff");
    /// ```
    pub fn hex(&self) -> String {
        format!(
            "#{:02x}{:02x}{:02x}",
            (self.r.clamp(0.0, 1.0) * 255.0) as u8,
            (self.g.clamp(0.0, 1.0) * 255.0) as u8,
            (self.b.clamp(0.0, 1.0) * 255.0) as u8
        )
    }

    /// Plain channel average of the encoded values. For perceived lightness use [`ColorRgb::lightness`].
    pub fn brightness(&self) -> f32 {
        (self.r.clamp(0.0, 1.0) + self.g.clamp(0.0, 1.0) + self.b.clamp(0.0, 1.0)) / 3.0
    }

    /// Perceived lightness 0..=1, see [`ColorOklab`].
    pub fn lightness(&self) -> f32 {
        ColorOklab::from(*self).l
    }

    /// Physical luminance Y 0..=1 (Rec. 709 weights on linearized sRGB).
    pub fn luminance(&self) -> f32 {
        let (r, g, b) = self.linear();
        0.2126 * r + 0.7152 * g + 0.0722 * b
    }

    /// Clamped channels converted from sRGB to linear light.
    fn linear(&self) -> (f32, f32, f32) {
        (
            srgb_to_linear(self.r.clamp(0.0, 1.0)),
            srgb_to_linear(self.g.clamp(0.0, 1.0)),
            srgb_to_linear(self.b.clamp(0.0, 1.0)),
        )
    }

    /// Linear interpolation towards `other`, `t` from 0 to 1.
    pub fn lerp(&self, other: ColorRgb, t: f32) -> ColorRgb {
        ColorRgb::new(
            self.r + (other.r - self.r) * t,
            self.g + (other.g - self.g) * t,
            self.b + (other.b - self.b) * t,
        )
    }

    /// Returns the Euclidean distance between this color and another color.
    /// This measure is not a perfect representation of human perception of color difference.
    pub fn dist_euclidean(&self, other: ColorRgb) -> f32 {
        let dr = self.r - other.r;
        let dg = self.g - other.g;
        let db = self.b - other.b;
        (dr * dr + dg * dg + db * db).sqrt()
    }

    /// Returns the closest [ColorName] from the list of predefined named colors [COLOR_NAMES].
    pub fn get_name(&self) -> ColorName {
        *COLOR_NAMES
            .iter()
            .min_by_key(|color| self.dist_euclidean(color.color).to_bits())
            .unwrap()
    }
}

impl From<ColorRgb> for ColorHsv {
    fn from(rgb: ColorRgb) -> Self {
        let r = rgb.r.clamp(0.0, 1.0);
        let g = rgb.g.clamp(0.0, 1.0);
        let b = rgb.b.clamp(0.0, 1.0);
        let max = r.max(g).max(b);
        let min = r.min(g).min(b);
        let delta = max - min;
        let h = if delta == 0.0 {
            0.0
        } else if max == r {
            60.0 * (((g - b) / delta) % 6.0)
        } else if max == g {
            60.0 * ((b - r) / delta + 2.0)
        } else {
            60.0 * ((r - g) / delta + 4.0)
        } / 360.0; // Convert degrees to 0.0-1.0 range
        let s = if max == 0.0 { 0.0 } else { delta / max };
        let v = max;
        Self { h, s, v }
    }
}
impl From<&ColorRgb> for ColorHsv {
    fn from(rgb: &ColorRgb) -> Self {
        ColorHsv::from(*rgb)
    }
}

/// A color represented in HSV (Hue, Saturation, Value) format.
///
/// - Hue (h): Color \[0-1\] (`0.0` is red, `0.333` is green, `0.667` is blue)
/// - Saturation (s): Amount of color \[0-1\]
/// - Value (v): Brightness \[0-1\]
///
/// ### Example
/// ```
/// # use plottery_lib::*;
/// let red_hsv = ColorRgb::red().hsv();
/// let red_rgb = red_hsv.rgb();
/// ```
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ColorHsv {
    pub h: f32,
    pub s: f32,
    pub v: f32,
}

impl ColorHsv {
    /// Creates a new HSV color with the specified hue, saturation and value values.
    ///
    /// `saturation` and `value` should be between 0.0 and 1.0.
    pub fn new(h: f32, s: f32, v: f32) -> Self {
        Self { h, s, v }
    }

    /// Converts this HSV color to RGB format.
    pub fn rgb(&self) -> ColorRgb {
        (*self).into()
    }
}

impl From<ColorHsv> for ColorRgb {
    fn from(hsv: ColorHsv) -> Self {
        let h = (hsv.h % 1.0) * 360.0;
        let s = hsv.s.clamp(0.0, 1.0);
        let v = hsv.v.clamp(0.0, 1.0);
        let c = v * s;
        let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
        let m = v - c;
        let (r, g, b) = if h < 60.0 {
            (c, x, 0.0)
        } else if h < 120.0 {
            (x, c, 0.0)
        } else if h < 180.0 {
            (0.0, c, x)
        } else if h < 240.0 {
            (0.0, x, c)
        } else if h < 300.0 {
            (x, 0.0, c)
        } else {
            (c, 0.0, x)
        };
        Self {
            r: r + m,
            g: g + m,
            b: b + m,
        }
    }
}

fn srgb_to_linear(c: f32) -> f32 {
    if c <= 0.04045 {
        c / 12.92
    } else {
        ((c + 0.055) / 1.055).powf(2.4)
    }
}

fn linear_to_srgb(c: f32) -> f32 {
    if c <= 0.0031308 {
        c * 12.92
    } else {
        1.055 * c.powf(1.0 / 2.4) - 0.055
    }
}

/// Perceptual color: `l` lightness 0..=1, `a` green-red, `b` blue-yellow (roughly -0.4..=0.4).
///
/// Distances and interpolation in this space match how different the colors look.
///
/// ### Example
/// ```
/// # use plottery_lib::*;
/// let lab = ColorRgb::red().oklab();
/// let half_way = lab.lerp(ColorRgb::blue().oklab(), 0.5).rgb();
/// ```
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct ColorOklab {
    pub l: f32,
    pub a: f32,
    pub b: f32,
}

impl ColorOklab {
    pub fn new(l: f32, a: f32, b: f32) -> Self {
        Self { l, a, b }
    }

    /// Converts this Oklab color to RGB format.
    pub fn rgb(&self) -> ColorRgb {
        (*self).into()
    }

    /// Colorfulness, 0 for grays.
    pub fn chroma(&self) -> f32 {
        (self.a * self.a + self.b * self.b).sqrt()
    }

    pub fn hue(&self) -> Angle {
        Angle::from_rad(self.b.atan2(self.a))
    }

    /// Perceptual distance (ΔE in Oklab), unlike [`ColorRgb::dist_euclidean`].
    pub fn dist(&self, other: ColorOklab) -> f32 {
        let dl = self.l - other.l;
        let da = self.a - other.a;
        let db = self.b - other.b;
        (dl * dl + da * da + db * db).sqrt()
    }

    /// Perceptually even interpolation, `t` from 0 to 1.
    #[must_use]
    pub fn lerp(&self, other: ColorOklab, t: f32) -> ColorOklab {
        ColorOklab::new(
            self.l + (other.l - self.l) * t,
            self.a + (other.a - self.a) * t,
            self.b + (other.b - self.b) * t,
        )
    }
}

impl From<ColorRgb> for ColorOklab {
    fn from(rgb: ColorRgb) -> Self {
        let (r, g, b) = rgb.linear();
        let l = (0.412_221_46 * r + 0.536_332_55 * g + 0.051_445_995 * b).cbrt();
        let m = (0.211_903_5 * r + 0.680_699_5 * g + 0.107_396_96 * b).cbrt();
        let s = (0.088_302_46 * r + 0.281_718_85 * g + 0.629_978_7 * b).cbrt();
        Self {
            l: 0.210_454_26 * l + 0.793_617_8 * m - 0.004_072_047 * s,
            a: 1.977_998_5 * l - 2.428_592_2 * m + 0.450_593_7 * s,
            b: 0.025_904_037 * l + 0.782_771_77 * m - 0.808_675_77 * s,
        }
    }
}
impl From<&ColorRgb> for ColorOklab {
    fn from(rgb: &ColorRgb) -> Self {
        ColorOklab::from(*rgb)
    }
}

impl From<ColorOklab> for ColorRgb {
    fn from(lab: ColorOklab) -> Self {
        let l = lab.l + 0.396_337_78 * lab.a + 0.215_803_76 * lab.b;
        let m = lab.l - 0.105_561_346 * lab.a - 0.063_854_17 * lab.b;
        let s = lab.l - 0.089_484_18 * lab.a - 1.291_485_5 * lab.b;
        let (l, m, s) = (l * l * l, m * m * m, s * s * s);
        let to_channel = |linear: f32| linear_to_srgb(linear.clamp(0.0, 1.0)).clamp(0.0, 1.0);
        Self {
            r: to_channel(4.076_741_7 * l - 3.307_711_6 * m + 0.230_969_94 * s),
            g: to_channel(-1.268_438 * l + 2.609_757_4 * m - 0.341_319_38 * s),
            b: to_channel(-0.0041960863 * l - 0.703_418_6 * m + 1.707_614_7 * s),
        }
    }
}

impl PartialEq for ColorRgb {
    fn eq(&self, other: &Self) -> bool {
        (self.r - other.r).abs() < LARGE_EPSILON
            && (self.g - other.g).abs() < LARGE_EPSILON
            && (self.b - other.b).abs() < LARGE_EPSILON
    }
}

impl PartialEq for ColorOklab {
    fn eq(&self, other: &Self) -> bool {
        (self.l - other.l).abs() < LARGE_EPSILON
            && (self.a - other.a).abs() < LARGE_EPSILON
            && (self.b - other.b).abs() < LARGE_EPSILON
    }
}

impl PartialEq for ColorHsv {
    fn eq(&self, other: &Self) -> bool {
        (self.h - other.h).abs() < LARGE_EPSILON
            && (self.s - other.s).abs() < LARGE_EPSILON
            && (self.v - other.v).abs() < LARGE_EPSILON
    }
}

impl PartialEq<ColorRgb> for ColorHsv {
    fn eq(&self, other: &ColorRgb) -> bool {
        self.rgb() == *other
    }
}

impl PartialEq<ColorHsv> for ColorRgb {
    fn eq(&self, other: &ColorHsv) -> bool {
        *self == other.rgb()
    }
}

impl ColorRgb {
    pub fn black() -> Self {
        Self {
            r: 0.0,
            g: 0.0,
            b: 0.0,
        }
    }
    pub fn white() -> Self {
        Self {
            r: 1.0,
            g: 1.0,
            b: 1.0,
        }
    }
    pub fn gray_scale(brightness: f32) -> Self {
        Self {
            r: brightness,
            g: brightness,
            b: brightness,
        }
    }
    pub fn red() -> Self {
        Self {
            r: 1.0,
            g: 0.0,
            b: 0.0,
        }
    }
    pub fn green() -> Self {
        Self {
            r: 0.0,
            g: 1.0,
            b: 0.0,
        }
    }
    pub fn blue() -> Self {
        Self {
            r: 0.0,
            g: 0.0,
            b: 1.0,
        }
    }
    /// (1, 1, 0)
    pub fn yellow() -> Self {
        Self {
            r: 1.0,
            g: 1.0,
            b: 0.0,
        }
    }
    /// (0, 1, 1)
    pub fn cyan() -> Self {
        Self {
            r: 0.0,
            g: 1.0,
            b: 1.0,
        }
    }
    /// (1, 0, 1)
    pub fn magenta() -> Self {
        Self {
            r: 1.0,
            g: 0.0,
            b: 1.0,
        }
    }
    pub fn gray() -> Self {
        Self {
            r: 0.5,
            g: 0.5,
            b: 0.5,
        }
    }
    pub fn light_gray() -> Self {
        Self {
            r: 0.75,
            g: 0.75,
            b: 0.75,
        }
    }
    pub fn dark_gray() -> Self {
        Self {
            r: 0.25,
            g: 0.25,
            b: 0.25,
        }
    }
}