typst_library/visualize/
color.rs

1use std::fmt::{self, Debug, Formatter};
2use std::hash::{Hash, Hasher};
3use std::str::FromStr;
4use std::sync::LazyLock;
5
6use ecow::{EcoString, EcoVec, eco_format};
7use palette::encoding::{self, Linear};
8use palette::{
9    Alpha, Darken, Desaturate, FromColor, Lighten, OklabHue, RgbHue, Saturate, ShiftHue,
10};
11use qcms::Profile;
12use typst_syntax::{Span, Spanned};
13
14use crate::diag::{At, SourceResult, StrResult, bail};
15use crate::foundations::{
16    Args, Array, IntoValue, Module, Repr, Scope, Str, Value, array, cast, func, repr,
17    scope, ty,
18};
19use crate::layout::{Angle, Ratio};
20
21// Type aliases for `palette` internal types in f32.
22pub type Oklab = palette::oklab::Oklaba<f32>;
23pub type Oklch = palette::oklch::Oklcha<f32>;
24pub type LinearRgb = palette::rgb::Rgba<Linear<encoding::Srgb>, f32>;
25pub type Rgb = palette::rgb::Rgba<encoding::Srgb, f32>;
26pub type Hsl = palette::hsl::Hsla<encoding::Srgb, f32>;
27pub type Hsv = palette::hsv::Hsva<encoding::Srgb, f32>;
28pub type Luma = palette::luma::Lumaa<encoding::Srgb, f32>;
29
30/// The ICC profile used to convert from CMYK to RGB.
31///
32/// This is a minimal CMYK profile that only contains the necessary information
33/// to convert from CMYK to RGB. It is based on the CGATS TR 001-1995
34/// specification. See
35/// <https://github.com/saucecontrol/Compact-ICC-Profiles#cmyk>.
36static CMYK_TO_XYZ: LazyLock<Box<Profile>> = LazyLock::new(|| {
37    Profile::new_from_slice(typst_assets::icc::CMYK_TO_XYZ, false).unwrap()
38});
39
40/// The target sRGB profile.
41static SRGB_PROFILE: LazyLock<Box<Profile>> = LazyLock::new(|| {
42    let mut out = Profile::new_sRGB();
43    out.precache_output_transform();
44    out
45});
46
47static TO_SRGB: LazyLock<qcms::Transform> = LazyLock::new(|| {
48    qcms::Transform::new_to(
49        &CMYK_TO_XYZ,
50        &SRGB_PROFILE,
51        qcms::DataType::CMYK,
52        qcms::DataType::RGB8,
53        // Our input profile only supports perceptual intent.
54        qcms::Intent::Perceptual,
55    )
56    .unwrap()
57});
58
59/// A color in a specific color space.
60///
61/// Typst supports:
62/// - sRGB through the [`rgb` function]($color.rgb)
63/// - Device CMYK through the [`cmyk` function]($color.cmyk)
64/// - D65 Gray through the [`luma` function]($color.luma)
65/// - Oklab through the [`oklab` function]($color.oklab)
66/// - Oklch through the [`oklch` function]($color.oklch)
67/// - Linear RGB through the [`color.linear-rgb` function]($color.linear-rgb)
68/// - HSL through the [`color.hsl` function]($color.hsl)
69/// - HSV through the [`color.hsv` function]($color.hsv)
70///
71///
72/// # Example
73///
74/// ```example
75/// #rect(fill: aqua)
76/// ```
77///
78/// # Predefined colors
79/// Typst defines the following built-in colors:
80///
81/// | Color     | Definition         |
82/// |-----------|:-------------------|
83/// | `black`   | `{luma(0)}`        |
84/// | `gray`    | `{luma(170)}`      |
85/// | `silver`  | `{luma(221)}`      |
86/// | `white`   | `{luma(255)}`      |
87/// | `navy`    | `{rgb("#001f3f")}` |
88/// | `blue`    | `{rgb("#0074d9")}` |
89/// | `aqua`    | `{rgb("#7fdbff")}` |
90/// | `teal`    | `{rgb("#39cccc")}` |
91/// | `eastern` | `{rgb("#239dad")}` |
92/// | `purple`  | `{rgb("#b10dc9")}` |
93/// | `fuchsia` | `{rgb("#f012be")}` |
94/// | `maroon`  | `{rgb("#85144b")}` |
95/// | `red`     | `{rgb("#ff4136")}` |
96/// | `orange`  | `{rgb("#ff851b")}` |
97/// | `yellow`  | `{rgb("#ffdc00")}` |
98/// | `olive`   | `{rgb("#3d9970")}` |
99/// | `green`   | `{rgb("#2ecc40")}` |
100/// | `lime`    | `{rgb("#01ff70")}` |
101///
102/// The predefined colors and the most important color constructors are
103/// available globally and also in the color type's scope, so you can write
104/// either `color.red` or just `red`.
105///
106/// ```preview
107/// #let colors = (
108///   "black", "gray", "silver", "white",
109///   "navy", "blue", "aqua", "teal",
110///   "eastern", "purple", "fuchsia",
111///   "maroon", "red", "orange", "yellow",
112///   "olive", "green", "lime",
113/// )
114///
115/// #set text(font: "PT Sans")
116/// #set page(width: auto)
117/// #grid(
118///   columns: 9,
119///   gutter: 10pt,
120///   ..colors.map(name => {
121///       let col = eval(name)
122///       let luminance = luma(col).components().first()
123///       set text(fill: white) if luminance < 50%
124///       set square(stroke: black) if col == white
125///       set align(center + horizon)
126///       square(size: 50pt,  fill: col, name)
127///   })
128/// )
129/// ```
130///
131/// # Predefined color maps
132/// Typst also includes a number of preset color maps that can be used for
133/// [gradients]($gradient/#stops). These are simply arrays of colors defined in
134/// the module `color.map`.
135///
136/// ```example
137/// #circle(fill: gradient.linear(..color.map.crest))
138/// ```
139///
140/// | Map        | Details                                                     |
141/// |------------|:------------------------------------------------------------|
142/// | `turbo`    | A perceptually uniform rainbow-like color map. Read [this blog post](https://ai.googleblog.com/2019/08/turbo-improved-rainbow-colormap-for.html) for more details. |
143/// | `cividis`  | A blue to gray to yellow color map. See [this blog post](https://bids.github.io/colormap/) for more details. |
144/// | `rainbow`  | Cycles through the full color spectrum. This color map is best used by setting the interpolation color space to [HSL]($color.hsl). The rainbow gradient is **not suitable** for data visualization because it is not perceptually uniform, so the differences between values become unclear to your readers. It should only be used for decorative purposes. |
145/// | `spectral` | Red to yellow to blue color map.                            |
146/// | `viridis`  | A purple to teal to yellow color map.                       |
147/// | `inferno`  | A black to red to yellow color map.                         |
148/// | `magma`    | A black to purple to yellow color map.                      |
149/// | `plasma`   | A purple to pink to yellow color map.                       |
150/// | `rocket`   | A black to red to white color map.                          |
151/// | `mako`     | A black to teal to white color map.                         |
152/// | `vlag`     | A light blue to white to red color map.                     |
153/// | `icefire`  | A light teal to black to orange color map.                  |
154/// | `flare`    | A orange to purple color map that is perceptually uniform.  |
155/// | `crest`    | A light green to blue color map.                            |
156///
157/// Some popular presets are not included because they are not available under a
158/// free licence. Others, like
159/// [Jet](https://jakevdp.github.io/blog/2014/10/16/how-bad-is-your-colormap/),
160/// are not included because they are not color blind friendly. Feel free to use
161/// or create a package with other presets that are useful to you!
162///
163/// ```preview
164/// #set page(width: auto, height: auto)
165/// #set text(font: "PT Sans", size: 8pt)
166///
167/// #let maps = (
168///   "turbo", "cividis", "rainbow", "spectral",
169///   "viridis", "inferno", "magma", "plasma",
170///   "rocket", "mako", "vlag", "icefire",
171///   "flare", "crest",
172/// )
173///
174/// #stack(dir: ltr, spacing: 3pt, ..maps.map((name) => {
175///   let map = eval("color.map." + name)
176///   stack(
177///     dir: ttb,
178///     block(
179///       width: 15pt,
180///       height: 100pt,
181///       fill: gradient.linear(..map, angle: 90deg),
182///     ),
183///     block(
184///       width: 15pt,
185///       height: 32pt,
186///       move(dy: 8pt, rotate(90deg, name)),
187///     ),
188///   )
189/// }))
190/// ```
191#[ty(scope, cast)]
192#[derive(Copy, Clone)]
193pub enum Color {
194    /// A 32-bit luma color.
195    Luma(Luma),
196    /// A 32-bit L\*a\*b\* color in the Oklab color space.
197    Oklab(Oklab),
198    /// A 32-bit LCh color in the Oklab color space.
199    Oklch(Oklch),
200    /// A 32-bit RGB color.
201    Rgb(Rgb),
202    /// A 32-bit linear RGB color.
203    LinearRgb(LinearRgb),
204    /// A 32-bit CMYK color.
205    Cmyk(Cmyk),
206    /// A 32-bit HSL color.
207    Hsl(Hsl),
208    /// A 32-bit HSV color.
209    Hsv(Hsv),
210}
211
212#[scope]
213impl Color {
214    /// The module of preset color maps.
215    pub const MAP: fn() -> Module = || typst_utils::singleton!(Module, map()).clone();
216
217    pub const BLACK: Self = Self::Luma(Luma::new(0.0, 1.0));
218    pub const GRAY: Self = Self::Luma(Luma::new(0.6666666, 1.0));
219    pub const WHITE: Self = Self::Luma(Luma::new(1.0, 1.0));
220    pub const SILVER: Self = Self::Luma(Luma::new(0.8666667, 1.0));
221    pub const NAVY: Self = Self::Rgb(Rgb::new(0.0, 0.121569, 0.247059, 1.0));
222    pub const BLUE: Self = Self::Rgb(Rgb::new(0.0, 0.454902, 0.85098, 1.0));
223    pub const AQUA: Self = Self::Rgb(Rgb::new(0.4980392, 0.858823, 1.0, 1.0));
224    pub const TEAL: Self = Self::Rgb(Rgb::new(0.223529, 0.8, 0.8, 1.0));
225    pub const EASTERN: Self = Self::Rgb(Rgb::new(0.13725, 0.615686, 0.678431, 1.0));
226    pub const PURPLE: Self = Self::Rgb(Rgb::new(0.694118, 0.050980, 0.788235, 1.0));
227    pub const FUCHSIA: Self = Self::Rgb(Rgb::new(0.941177, 0.070588, 0.745098, 1.0));
228    pub const MAROON: Self = Self::Rgb(Rgb::new(0.521569, 0.078431, 0.294118, 1.0));
229    pub const RED: Self = Self::Rgb(Rgb::new(1.0, 0.254902, 0.211765, 1.0));
230    pub const ORANGE: Self = Self::Rgb(Rgb::new(1.0, 0.521569, 0.105882, 1.0));
231    pub const YELLOW: Self = Self::Rgb(Rgb::new(1.0, 0.8627451, 0.0, 1.0));
232    pub const OLIVE: Self = Self::Rgb(Rgb::new(0.239216, 0.6, 0.4392157, 1.0));
233    pub const GREEN: Self = Self::Rgb(Rgb::new(0.1803922, 0.8, 0.2509804, 1.0));
234    pub const LIME: Self = Self::Rgb(Rgb::new(0.0039216, 1.0, 0.4392157, 1.0));
235
236    /// Create a grayscale color.
237    ///
238    /// A grayscale color is represented internally by a single `lightness`
239    /// component.
240    ///
241    /// These components are also available using the
242    /// [`components`]($color.components) method.
243    ///
244    /// ```example
245    /// #for x in range(250, step: 50) {
246    ///   box(square(fill: luma(x)))
247    /// }
248    /// ```
249    #[func]
250    pub fn luma(
251        args: &mut Args,
252        /// The lightness component.
253        #[external]
254        lightness: Component,
255        /// The alpha component.
256        #[external]
257        alpha: RatioComponent,
258        /// Alternatively: The color to convert to grayscale.
259        ///
260        /// If this is given, the `lightness` should not be given.
261        #[external]
262        color: Color,
263    ) -> SourceResult<Color> {
264        Ok(if let Some(color) = args.find::<Color>()? {
265            Color::Luma(color.to_luma())
266        } else {
267            let Component(gray) =
268                args.expect("gray component").unwrap_or(Component(Ratio::one()));
269            let RatioComponent(alpha) =
270                args.eat()?.unwrap_or(RatioComponent(Ratio::one()));
271            Self::Luma(Luma::new(gray.get() as f32, alpha.get() as f32))
272        })
273    }
274
275    /// Create an [Oklab](https://bottosson.github.io/posts/oklab/) color.
276    ///
277    /// This color space is well suited for the following use cases:
278    /// - Color manipulation such as saturating while keeping perceived hue
279    /// - Creating grayscale images with uniform perceived lightness
280    /// - Creating smooth and uniform color transition and gradients
281    ///
282    /// A linear Oklab color is represented internally by an array of four
283    /// components:
284    /// - lightness ([`ratio`])
285    /// - a ([`float`] or [`ratio`].
286    ///   Ratios are relative to `{0.4}`; meaning `{50%}` is equal to `{0.2}`)
287    /// - b ([`float`] or [`ratio`].
288    ///   Ratios are relative to `{0.4}`; meaning `{50%}` is equal to `{0.2}`)
289    /// - alpha ([`ratio`])
290    ///
291    /// These components are also available using the
292    /// [`components`]($color.components) method.
293    ///
294    /// ```example
295    /// #square(
296    ///   fill: oklab(27%, 20%, -3%, 50%)
297    /// )
298    /// ```
299    #[func]
300    pub fn oklab(
301        args: &mut Args,
302        /// The lightness component.
303        #[external]
304        lightness: RatioComponent,
305        /// The a ("green/red") component.
306        #[external]
307        a: ChromaComponent,
308        /// The b ("blue/yellow") component.
309        #[external]
310        b: ChromaComponent,
311        /// The alpha component.
312        #[external]
313        alpha: RatioComponent,
314        /// Alternatively: The color to convert to Oklab.
315        ///
316        /// If this is given, the individual components should not be given.
317        #[external]
318        color: Color,
319    ) -> SourceResult<Color> {
320        Ok(if let Some(color) = args.find::<Color>()? {
321            Color::Oklab(color.to_oklab())
322        } else {
323            let RatioComponent(l) = args.expect("lightness component")?;
324            let ChromaComponent(a) = args.expect("A component")?;
325            let ChromaComponent(b) = args.expect("B component")?;
326            let RatioComponent(alpha) =
327                args.eat()?.unwrap_or(RatioComponent(Ratio::one()));
328            Self::Oklab(Oklab::new(l.get() as f32, a, b, alpha.get() as f32))
329        })
330    }
331
332    /// Create an [Oklch](https://bottosson.github.io/posts/oklab/) color.
333    ///
334    /// This color space is well suited for the following use cases:
335    /// - Color manipulation involving lightness, chroma, and hue
336    /// - Creating grayscale images with uniform perceived lightness
337    /// - Creating smooth and uniform color transition and gradients
338    ///
339    /// A linear Oklch color is represented internally by an array of four
340    /// components:
341    /// - lightness ([`ratio`])
342    /// - chroma ([`float`] or [`ratio`].
343    ///   Ratios are relative to `{0.4}`; meaning `{50%}` is equal to `{0.2}`)
344    /// - hue ([`angle`])
345    /// - alpha ([`ratio`])
346    ///
347    /// These components are also available using the
348    /// [`components`]($color.components) method.
349    ///
350    /// ```example
351    /// #square(
352    ///   fill: oklch(40%, 0.2, 160deg, 50%)
353    /// )
354    /// ```
355    #[func]
356    pub fn oklch(
357        args: &mut Args,
358        /// The lightness component.
359        #[external]
360        lightness: RatioComponent,
361        /// The chroma component.
362        #[external]
363        chroma: ChromaComponent,
364        /// The hue component.
365        #[external]
366        hue: Angle,
367        /// The alpha component.
368        #[external]
369        alpha: RatioComponent,
370        /// Alternatively: The color to convert to Oklch.
371        ///
372        /// If this is given, the individual components should not be given.
373        #[external]
374        color: Color,
375    ) -> SourceResult<Color> {
376        Ok(if let Some(color) = args.find::<Color>()? {
377            Color::Oklch(color.to_oklch())
378        } else {
379            let RatioComponent(l) = args.expect("lightness component")?;
380            let ChromaComponent(c) = args.expect("chroma component")?;
381            let h: Angle = args.expect("hue component")?;
382            let RatioComponent(alpha) =
383                args.eat()?.unwrap_or(RatioComponent(Ratio::one()));
384            Self::Oklch(Oklch::new(
385                l.get() as f32,
386                c,
387                OklabHue::from_degrees(h.to_deg() as f32),
388                alpha.get() as f32,
389            ))
390        })
391    }
392
393    /// Create an RGB(A) color with linear luma.
394    ///
395    /// This color space is similar to sRGB, but with the distinction that the
396    /// color component are not gamma corrected. This makes it easier to perform
397    /// color operations such as blending and interpolation. Although, you
398    /// should prefer to use the [`oklab` function]($color.oklab) for these.
399    ///
400    /// A linear RGB(A) color is represented internally by an array of four
401    /// components:
402    /// - red ([`ratio`])
403    /// - green ([`ratio`])
404    /// - blue ([`ratio`])
405    /// - alpha ([`ratio`])
406    ///
407    /// These components are also available using the
408    /// [`components`]($color.components) method.
409    ///
410    /// ```example
411    /// #square(fill: color.linear-rgb(
412    ///   30%, 50%, 10%,
413    /// ))
414    /// ```
415    #[func(title = "Linear RGB")]
416    pub fn linear_rgb(
417        args: &mut Args,
418        /// The red component.
419        #[external]
420        red: Component,
421        /// The green component.
422        #[external]
423        green: Component,
424        /// The blue component.
425        #[external]
426        blue: Component,
427        /// The alpha component.
428        #[external]
429        alpha: Component,
430        /// Alternatively: The color to convert to linear RGB(A).
431        ///
432        /// If this is given, the individual components should not be given.
433        #[external]
434        color: Color,
435    ) -> SourceResult<Color> {
436        Ok(if let Some(color) = args.find::<Color>()? {
437            Color::LinearRgb(color.to_linear_rgb())
438        } else {
439            let Component(r) = args.expect("red component")?;
440            let Component(g) = args.expect("green component")?;
441            let Component(b) = args.expect("blue component")?;
442            let Component(a) = args.eat()?.unwrap_or(Component(Ratio::one()));
443            Self::LinearRgb(LinearRgb::new(
444                r.get() as f32,
445                g.get() as f32,
446                b.get() as f32,
447                a.get() as f32,
448            ))
449        })
450    }
451
452    /// Create an RGB(A) color.
453    ///
454    /// The color is specified in the sRGB color space.
455    ///
456    /// An RGB(A) color is represented internally by an array of four components:
457    /// - red ([`ratio`])
458    /// - green ([`ratio`])
459    /// - blue ([`ratio`])
460    /// - alpha ([`ratio`])
461    ///
462    /// These components are also available using the [`components`]($color.components)
463    /// method.
464    ///
465    /// ```example
466    /// #square(fill: rgb("#b1f2eb"))
467    /// #square(fill: rgb(87, 127, 230))
468    /// #square(fill: rgb(25%, 13%, 65%))
469    /// ```
470    #[func(title = "RGB")]
471    pub fn rgb(
472        args: &mut Args,
473        /// The red component.
474        #[external]
475        red: Component,
476        /// The green component.
477        #[external]
478        green: Component,
479        /// The blue component.
480        #[external]
481        blue: Component,
482        /// The alpha component.
483        #[external]
484        alpha: Component,
485        /// Alternatively: The color in hexadecimal notation.
486        ///
487        /// Accepts three, four, six or eight hexadecimal digits and optionally
488        /// a leading hash.
489        ///
490        /// If this is given, the individual components should not be given.
491        ///
492        /// ```example
493        /// #text(16pt, rgb("#239dad"))[
494        ///   *Typst*
495        /// ]
496        /// ```
497        #[external]
498        hex: Str,
499        /// Alternatively: The color to convert to RGB(a).
500        ///
501        /// If this is given, the individual components should not be given.
502        #[external]
503        color: Color,
504    ) -> SourceResult<Color> {
505        Ok(if let Some(string) = args.find::<Spanned<Str>>()? {
506            Self::from_str(&string.v).at(string.span)?
507        } else if let Some(color) = args.find::<Color>()? {
508            Color::Rgb(color.to_rgb())
509        } else {
510            let Component(r) = args.expect("red component")?;
511            let Component(g) = args.expect("green component")?;
512            let Component(b) = args.expect("blue component")?;
513            let Component(a) = args.eat()?.unwrap_or(Component(Ratio::one()));
514            Self::Rgb(Rgb::new(
515                r.get() as f32,
516                g.get() as f32,
517                b.get() as f32,
518                a.get() as f32,
519            ))
520        })
521    }
522
523    /// Create a CMYK color.
524    ///
525    /// This is useful if you want to target a specific printer. The conversion
526    /// to RGB for display preview might differ from how your printer reproduces
527    /// the color.
528    ///
529    /// A CMYK color is represented internally by an array of four components:
530    /// - cyan ([`ratio`])
531    /// - magenta ([`ratio`])
532    /// - yellow ([`ratio`])
533    /// - key ([`ratio`])
534    ///
535    /// These components are also available using the
536    /// [`components`]($color.components) method.
537    ///
538    /// Note that CMYK colors are not currently supported when PDF/A output is
539    /// enabled.
540    ///
541    /// ```example
542    /// #square(
543    ///   fill: cmyk(27%, 0%, 3%, 5%)
544    /// )
545    /// ```
546    #[func(title = "CMYK")]
547    pub fn cmyk(
548        args: &mut Args,
549        /// The cyan component.
550        #[external]
551        cyan: RatioComponent,
552        /// The magenta component.
553        #[external]
554        magenta: RatioComponent,
555        /// The yellow component.
556        #[external]
557        yellow: RatioComponent,
558        /// The key component.
559        #[external]
560        key: RatioComponent,
561        /// Alternatively: The color to convert to CMYK.
562        ///
563        /// If this is given, the individual components should not be given.
564        #[external]
565        color: Color,
566    ) -> SourceResult<Color> {
567        Ok(if let Some(color) = args.find::<Color>()? {
568            Color::Cmyk(color.to_cmyk())
569        } else {
570            let RatioComponent(c) = args.expect("cyan component")?;
571            let RatioComponent(m) = args.expect("magenta component")?;
572            let RatioComponent(y) = args.expect("yellow component")?;
573            let RatioComponent(k) = args.expect("key/black component")?;
574            Self::Cmyk(Cmyk::new(
575                c.get() as f32,
576                m.get() as f32,
577                y.get() as f32,
578                k.get() as f32,
579            ))
580        })
581    }
582
583    /// Create an HSL color.
584    ///
585    /// This color space is useful for specifying colors by hue, saturation and
586    /// lightness. It is also useful for color manipulation, such as saturating
587    /// while keeping perceived hue.
588    ///
589    /// An HSL color is represented internally by an array of four components:
590    /// - hue ([`angle`])
591    /// - saturation ([`ratio`])
592    /// - lightness ([`ratio`])
593    /// - alpha ([`ratio`])
594    ///
595    /// These components are also available using the
596    /// [`components`]($color.components) method.
597    ///
598    /// ```example
599    /// #square(
600    ///   fill: color.hsl(30deg, 50%, 60%)
601    /// )
602    /// ```
603    #[func(title = "HSL")]
604    pub fn hsl(
605        args: &mut Args,
606        /// The hue angle.
607        #[external]
608        hue: Angle,
609        /// The saturation component.
610        #[external]
611        saturation: Component,
612        /// The lightness component.
613        #[external]
614        lightness: Component,
615        /// The alpha component.
616        #[external]
617        alpha: Component,
618        /// Alternatively: The color to convert to HSL.
619        ///
620        /// If this is given, the individual components should not be given.
621        #[external]
622        color: Color,
623    ) -> SourceResult<Color> {
624        Ok(if let Some(color) = args.find::<Color>()? {
625            Color::Hsl(color.to_hsl())
626        } else {
627            let h: Angle = args.expect("hue component")?;
628            let Component(s) = args.expect("saturation component")?;
629            let Component(l) = args.expect("lightness component")?;
630            let Component(a) = args.eat()?.unwrap_or(Component(Ratio::one()));
631            Self::Hsl(Hsl::new(
632                RgbHue::from_degrees(h.to_deg() as f32),
633                s.get() as f32,
634                l.get() as f32,
635                a.get() as f32,
636            ))
637        })
638    }
639
640    /// Create an HSV color.
641    ///
642    /// This color space is useful for specifying colors by hue, saturation and
643    /// value. It is also useful for color manipulation, such as saturating
644    /// while keeping perceived hue.
645    ///
646    /// An HSV color is represented internally by an array of four components:
647    /// - hue ([`angle`])
648    /// - saturation ([`ratio`])
649    /// - value ([`ratio`])
650    /// - alpha ([`ratio`])
651    ///
652    /// These components are also available using the
653    /// [`components`]($color.components) method.
654    ///
655    /// ```example
656    /// #square(
657    ///   fill: color.hsv(30deg, 50%, 60%)
658    /// )
659    /// ```
660    #[func(title = "HSV")]
661    pub fn hsv(
662        args: &mut Args,
663        /// The hue angle.
664        #[external]
665        hue: Angle,
666        /// The saturation component.
667        #[external]
668        saturation: Component,
669        /// The value component.
670        #[external]
671        value: Component,
672        /// The alpha component.
673        #[external]
674        alpha: Component,
675        /// Alternatively: The color to convert to HSL.
676        ///
677        /// If this is given, the individual components should not be given.
678        #[external]
679        color: Color,
680    ) -> SourceResult<Color> {
681        Ok(if let Some(color) = args.find::<Color>()? {
682            Color::Hsv(color.to_hsv())
683        } else {
684            let h: Angle = args.expect("hue component")?;
685            let Component(s) = args.expect("saturation component")?;
686            let Component(v) = args.expect("value component")?;
687            let Component(a) = args.eat()?.unwrap_or(Component(Ratio::one()));
688            Self::Hsv(Hsv::new(
689                RgbHue::from_degrees(h.to_deg() as f32),
690                s.get() as f32,
691                v.get() as f32,
692                a.get() as f32,
693            ))
694        })
695    }
696
697    /// Extracts the components of this color.
698    ///
699    /// The size and values of this array depends on the color space. You can
700    /// obtain the color space using [`space`]($color.space). Below is a table
701    /// of the color spaces and their components:
702    ///
703    /// |       Color space       |     C1    |     C2     |     C3    |   C4   |
704    /// |-------------------------|-----------|------------|-----------|--------|
705    /// | [`luma`]($color.luma)   | Lightness |            |           |        |
706    /// | [`oklab`]($color.oklab) | Lightness |    `a`     |    `b`    |  Alpha |
707    /// | [`oklch`]($color.oklch) | Lightness |   Chroma   |    Hue    |  Alpha |
708    /// | [`linear-rgb`]($color.linear-rgb) | Red  |   Green |    Blue |  Alpha |
709    /// | [`rgb`]($color.rgb)     |    Red    |   Green    |    Blue   |  Alpha |
710    /// | [`cmyk`]($color.cmyk)   |    Cyan   |   Magenta  |   Yellow  |  Key   |
711    /// | [`hsl`]($color.hsl)     |     Hue   | Saturation | Lightness |  Alpha |
712    /// | [`hsv`]($color.hsv)     |     Hue   | Saturation |   Value   |  Alpha |
713    ///
714    /// For the meaning and type of each individual value, see the documentation
715    /// of the corresponding color space. The alpha component is optional and
716    /// only included if the `alpha` argument is `true`. The length of the
717    /// returned array depends on the number of components and whether the alpha
718    /// component is included.
719    ///
720    /// ```example
721    /// // note that the alpha component is included by default
722    /// #rgb(40%, 60%, 80%).components()
723    /// ```
724    #[func]
725    pub fn components(
726        self,
727        /// Whether to include the alpha component.
728        #[named]
729        #[default(true)]
730        alpha: bool,
731    ) -> Array {
732        let mut components = match self {
733            Self::Luma(c) => {
734                array![Ratio::new(c.luma.into()), Ratio::new(c.alpha.into())]
735            }
736            Self::Oklab(c) => {
737                array![
738                    Ratio::new(c.l.into()),
739                    f64::from(c.a),
740                    f64::from(c.b),
741                    Ratio::new(c.alpha.into())
742                ]
743            }
744            Self::Oklch(c) => {
745                array![
746                    Ratio::new(c.l.into()),
747                    f64::from(c.chroma),
748                    hue_angle(c.hue.into_degrees()),
749                    Ratio::new(c.alpha.into()),
750                ]
751            }
752            Self::LinearRgb(c) => {
753                array![
754                    Ratio::new(c.red.into()),
755                    Ratio::new(c.green.into()),
756                    Ratio::new(c.blue.into()),
757                    Ratio::new(c.alpha.into()),
758                ]
759            }
760            Self::Rgb(c) => {
761                array![
762                    Ratio::new(c.red.into()),
763                    Ratio::new(c.green.into()),
764                    Ratio::new(c.blue.into()),
765                    Ratio::new(c.alpha.into()),
766                ]
767            }
768            Self::Cmyk(c) => {
769                array![
770                    Ratio::new(c.c.into()),
771                    Ratio::new(c.m.into()),
772                    Ratio::new(c.y.into()),
773                    Ratio::new(c.k.into())
774                ]
775            }
776            Self::Hsl(c) => {
777                array![
778                    hue_angle(c.hue.into_degrees()),
779                    Ratio::new(c.saturation.into()),
780                    Ratio::new(c.lightness.into()),
781                    Ratio::new(c.alpha.into()),
782                ]
783            }
784            Self::Hsv(c) => {
785                array![
786                    hue_angle(c.hue.into_degrees()),
787                    Ratio::new(c.saturation.into()),
788                    Ratio::new(c.value.into()),
789                    Ratio::new(c.alpha.into()),
790                ]
791            }
792        };
793        // Remove the alpha component if the corresponding argument was set.
794        if !alpha && !matches!(self, Self::Cmyk(_)) {
795            let _ = components.pop();
796        }
797        components
798    }
799
800    /// Returns the constructor function for this color's space.
801    ///
802    /// Returns one of:
803    /// - [`luma`]($color.luma)
804    /// - [`oklab`]($color.oklab)
805    /// - [`oklch`]($color.oklch)
806    /// - [`linear-rgb`]($color.linear-rgb)
807    /// - [`rgb`]($color.rgb)
808    /// - [`cmyk`]($color.cmyk)
809    /// - [`hsl`]($color.hsl)
810    /// - [`hsv`]($color.hsv)
811    ///
812    /// ```example
813    /// #let color = cmyk(1%, 2%, 3%, 4%)
814    /// #(color.space() == cmyk)
815    /// ```
816    #[func]
817    pub fn space(self) -> ColorSpace {
818        match self {
819            Self::Luma(_) => ColorSpace::D65Gray,
820            Self::Oklab(_) => ColorSpace::Oklab,
821            Self::Oklch(_) => ColorSpace::Oklch,
822            Self::LinearRgb(_) => ColorSpace::LinearRgb,
823            Self::Rgb(_) => ColorSpace::Srgb,
824            Self::Cmyk(_) => ColorSpace::Cmyk,
825            Self::Hsl(_) => ColorSpace::Hsl,
826            Self::Hsv(_) => ColorSpace::Hsv,
827        }
828    }
829
830    /// Returns the color's RGB(A) hex representation (such as `#ffaa32` or
831    /// `#020304fe`). The alpha component (last two digits in `#020304fe`) is
832    /// omitted if it is equal to `ff` (255 / 100%).
833    #[func]
834    pub fn to_hex(self) -> EcoString {
835        let (r, g, b, a) = self.to_rgb().into_format::<u8, u8>().into_components();
836        if a != 255 {
837            eco_format!("#{:02x}{:02x}{:02x}{:02x}", r, g, b, a)
838        } else {
839            eco_format!("#{:02x}{:02x}{:02x}", r, g, b)
840        }
841    }
842
843    /// Lightens a color by a given factor.
844    #[func]
845    pub fn lighten(
846        self,
847        /// The factor to lighten the color by.
848        factor: Ratio,
849    ) -> Color {
850        let factor = factor.get() as f32;
851        match self {
852            Self::Luma(c) => Self::Luma(c.lighten(factor)),
853            Self::Oklab(c) => Self::Oklab(c.lighten(factor)),
854            Self::Oklch(c) => Self::Oklch(c.lighten(factor)),
855            Self::LinearRgb(c) => Self::LinearRgb(c.lighten(factor)),
856            Self::Rgb(c) => Self::Rgb(c.lighten(factor)),
857            Self::Cmyk(c) => Self::Cmyk(c.lighten(factor)),
858            Self::Hsl(c) => Self::Hsl(c.lighten(factor)),
859            Self::Hsv(c) => Self::Hsv(c.lighten(factor)),
860        }
861    }
862
863    /// Darkens a color by a given factor.
864    #[func]
865    pub fn darken(
866        self,
867        /// The factor to darken the color by.
868        factor: Ratio,
869    ) -> Color {
870        let factor = factor.get() as f32;
871        match self {
872            Self::Luma(c) => Self::Luma(c.darken(factor)),
873            Self::Oklab(c) => Self::Oklab(c.darken(factor)),
874            Self::Oklch(c) => Self::Oklch(c.darken(factor)),
875            Self::LinearRgb(c) => Self::LinearRgb(c.darken(factor)),
876            Self::Rgb(c) => Self::Rgb(c.darken(factor)),
877            Self::Cmyk(c) => Self::Cmyk(c.darken(factor)),
878            Self::Hsl(c) => Self::Hsl(c.darken(factor)),
879            Self::Hsv(c) => Self::Hsv(c.darken(factor)),
880        }
881    }
882
883    /// Increases the saturation of a color by a given factor.
884    #[func]
885    pub fn saturate(
886        self,
887        span: Span,
888        /// The factor to saturate the color by.
889        factor: Ratio,
890    ) -> SourceResult<Color> {
891        let f = factor.get() as f32;
892        Ok(match self {
893            Self::Luma(_) => bail!(
894                span, "cannot saturate grayscale color";
895                hint: "try converting your color to RGB first"
896            ),
897            Self::Hsl(c) => Self::Hsl(c.saturate(f)),
898            Self::Hsv(c) => Self::Hsv(c.saturate(f)),
899            Self::Oklab(_)
900            | Self::Oklch(_)
901            | Self::LinearRgb(_)
902            | Self::Rgb(_)
903            | Self::Cmyk(_) => {
904                Color::Hsv(self.to_hsv().saturate(f)).to_space(self.space())
905            }
906        })
907    }
908
909    /// Decreases the saturation of a color by a given factor.
910    #[func]
911    pub fn desaturate(
912        self,
913        span: Span,
914        /// The factor to desaturate the color by.
915        factor: Ratio,
916    ) -> SourceResult<Color> {
917        let f = factor.get() as f32;
918        Ok(match self {
919            Self::Luma(_) => bail!(
920                span, "cannot desaturate grayscale color";
921                hint: "try converting your color to RGB first"
922            ),
923            Self::Hsl(c) => Self::Hsl(c.desaturate(f)),
924            Self::Hsv(c) => Self::Hsv(c.desaturate(f)),
925            Self::Oklab(_)
926            | Self::Oklch(_)
927            | Self::LinearRgb(_)
928            | Self::Rgb(_)
929            | Self::Cmyk(_) => {
930                Color::Hsv(self.to_hsv().desaturate(f)).to_space(self.space())
931            }
932        })
933    }
934
935    /// Produces the complementary color using a provided color space.
936    /// You can think of it as the opposite side on a color wheel.
937    ///
938    /// ```example
939    /// #square(fill: yellow)
940    /// #square(fill: yellow.negate())
941    /// #square(fill: yellow.negate(space: rgb))
942    /// ```
943    #[func]
944    pub fn negate(
945        self,
946        /// The color space used for the transformation. By default, a perceptual color space is used.
947        #[named]
948        #[default(ColorSpace::Oklab)]
949        space: ColorSpace,
950    ) -> Color {
951        let result = match self.to_space(space) {
952            Self::Luma(c) => Self::Luma(Luma::new(1.0 - c.luma, c.alpha)),
953            Self::Oklab(c) => Self::Oklab(Oklab::new(1.0 - c.l, -c.a, -c.b, c.alpha)),
954            Self::Oklch(c) => Self::Oklch(Oklch::new(
955                1.0 - c.l,
956                c.chroma,
957                OklabHue::from_degrees(c.hue.into_degrees() + 180.0),
958                c.alpha,
959            )),
960            Self::LinearRgb(c) => Self::LinearRgb(LinearRgb::new(
961                1.0 - c.red,
962                1.0 - c.green,
963                1.0 - c.blue,
964                c.alpha,
965            )),
966            Self::Rgb(c) => {
967                Self::Rgb(Rgb::new(1.0 - c.red, 1.0 - c.green, 1.0 - c.blue, c.alpha))
968            }
969            Self::Cmyk(c) => Self::Cmyk(Cmyk::new(1.0 - c.c, 1.0 - c.m, 1.0 - c.y, c.k)),
970            Self::Hsl(c) => Self::Hsl(Hsl::new(
971                RgbHue::from_degrees(c.hue.into_degrees() + 180.0),
972                c.saturation,
973                c.lightness,
974                c.alpha,
975            )),
976            Self::Hsv(c) => Self::Hsv(Hsv::new(
977                RgbHue::from_degrees(c.hue.into_degrees() + 180.0),
978                c.saturation,
979                c.value,
980                c.alpha,
981            )),
982        };
983        result.to_space(self.space())
984    }
985
986    /// Rotates the hue of the color by a given angle.
987    #[func]
988    pub fn rotate(
989        self,
990        span: Span,
991        /// The angle to rotate the hue by.
992        angle: Angle,
993        /// The color space used to rotate. By default, this happens in a perceptual
994        /// color space ([`oklch`]($color.oklch)).
995        #[named]
996        #[default(ColorSpace::Oklch)]
997        space: ColorSpace,
998    ) -> SourceResult<Color> {
999        Ok(match space {
1000            ColorSpace::Oklch => {
1001                let oklch = self.to_oklch();
1002                let rotated = oklch.shift_hue(angle.to_deg() as f32);
1003                Self::Oklch(rotated).to_space(self.space())
1004            }
1005            ColorSpace::Hsl => {
1006                let hsl = self.to_hsl();
1007                let rotated = hsl.shift_hue(angle.to_deg() as f32);
1008                Self::Hsl(rotated).to_space(self.space())
1009            }
1010            ColorSpace::Hsv => {
1011                let hsv = self.to_hsv();
1012                let rotated = hsv.shift_hue(angle.to_deg() as f32);
1013                Self::Hsv(rotated).to_space(self.space())
1014            }
1015            _ => bail!(span, "this colorspace does not support hue rotation"),
1016        })
1017    }
1018
1019    /// Create a color by mixing two or more colors.
1020    ///
1021    /// In color spaces with a hue component (hsl, hsv, oklch), only two colors
1022    /// can be mixed at once. Mixing more than two colors in such a space will
1023    /// result in an error!
1024    ///
1025    /// ```example
1026    /// #set block(height: 20pt, width: 100%)
1027    /// #block(fill: red.mix(blue))
1028    /// #block(fill: red.mix(blue, space: rgb))
1029    /// #block(fill: color.mix(red, blue, white))
1030    /// #block(fill: color.mix((red, 70%), (blue, 30%)))
1031    /// ```
1032    #[func]
1033    pub fn mix(
1034        /// The colors, optionally with weights, specified as a pair (array of
1035        /// length two) of color and weight (float or ratio).
1036        ///
1037        /// The weights do not need to add to `{100%}`, they are relative to the
1038        /// sum of all weights.
1039        #[variadic]
1040        colors: Vec<WeightedColor>,
1041        /// The color space to mix in. By default, this happens in a perceptual
1042        /// color space ([`oklab`]($color.oklab)).
1043        #[named]
1044        #[default(ColorSpace::Oklab)]
1045        space: ColorSpace,
1046    ) -> StrResult<Color> {
1047        Self::mix_iter(colors, space)
1048    }
1049
1050    /// Makes a color more transparent by a given factor.
1051    ///
1052    /// This method is relative to the existing alpha value.
1053    /// If the scale is positive, calculates `alpha - alpha * scale`.
1054    /// Negative scales behave like `color.opacify(-scale)`.
1055    ///
1056    /// ```example
1057    /// #block(fill: red)[opaque]
1058    /// #block(fill: red.transparentize(50%))[half red]
1059    /// #block(fill: red.transparentize(75%))[quarter red]
1060    /// ```
1061    #[func]
1062    pub fn transparentize(
1063        self,
1064        /// The factor to change the alpha value by.
1065        scale: Ratio,
1066    ) -> StrResult<Color> {
1067        self.scale_alpha(-scale)
1068    }
1069
1070    /// Makes a color more opaque by a given scale.
1071    ///
1072    /// This method is relative to the existing alpha value.
1073    /// If the scale is positive, calculates `alpha + scale - alpha * scale`.
1074    /// Negative scales behave like `color.transparentize(-scale)`.
1075    ///
1076    /// ```example
1077    /// #let half-red = red.transparentize(50%)
1078    /// #block(fill: half-red.opacify(100%))[opaque]
1079    /// #block(fill: half-red.opacify(50%))[three quarters red]
1080    /// #block(fill: half-red.opacify(-50%))[one quarter red]
1081    /// ```
1082    #[func]
1083    pub fn opacify(
1084        self,
1085        /// The scale to change the alpha value by.
1086        scale: Ratio,
1087    ) -> StrResult<Color> {
1088        self.scale_alpha(scale)
1089    }
1090}
1091
1092impl Color {
1093    /// Same as [`Color::mix`], but takes an iterator instead of a vector.
1094    pub fn mix_iter(
1095        colors: impl IntoIterator<
1096            Item = WeightedColor,
1097            IntoIter = impl ExactSizeIterator<Item = WeightedColor>,
1098        >,
1099        space: ColorSpace,
1100    ) -> StrResult<Color> {
1101        let mut colors = colors.into_iter();
1102        if space.hue_index().is_some() && colors.len() > 2 {
1103            bail!("cannot mix more than two colors in a hue-based space");
1104        }
1105
1106        let m = if space.hue_index().is_some() && colors.len() == 2 {
1107            let mut m = [0.0; 4];
1108
1109            let WeightedColor { color: c0, weight: w0 } = colors.next().unwrap();
1110            let WeightedColor { color: c1, weight: w1 } = colors.next().unwrap();
1111
1112            let c0 = c0.to_space(space).to_vec4();
1113            let c1 = c1.to_space(space).to_vec4();
1114            let w0 = w0 as f32;
1115            let w1 = w1 as f32;
1116
1117            if w0 + w1 <= 0.0 {
1118                bail!("sum of weights must be positive");
1119            }
1120
1121            for i in 0..4 {
1122                m[i] = (w0 * c0[i] + w1 * c1[i]) / (w0 + w1);
1123            }
1124
1125            // Ensure that the hue circle is traversed in the short direction.
1126            if let Some(index) = space.hue_index()
1127                && (c0[index] - c1[index]).abs() > 180.0
1128            {
1129                let (h0, h1) = if c0[index] < c1[index] {
1130                    (c0[index] + 360.0, c1[index])
1131                } else {
1132                    (c0[index], c1[index] + 360.0)
1133                };
1134                m[index] = (w0 * h0 + w1 * h1) / (w0 + w1);
1135            }
1136
1137            m
1138        } else {
1139            let mut total = 0.0;
1140            let mut acc = [0.0; 4];
1141
1142            for WeightedColor { color, weight } in colors {
1143                let weight = weight as f32;
1144                let v = color.to_space(space).to_vec4();
1145                acc[0] += weight * v[0];
1146                acc[1] += weight * v[1];
1147                acc[2] += weight * v[2];
1148                acc[3] += weight * v[3];
1149                total += weight;
1150            }
1151
1152            if total <= 0.0 {
1153                bail!("sum of weights must be positive");
1154            }
1155
1156            acc.map(|v| v / total)
1157        };
1158
1159        Ok(match space {
1160            ColorSpace::Oklab => Color::Oklab(Oklab::new(m[0], m[1], m[2], m[3])),
1161            ColorSpace::Oklch => Color::Oklch(Oklch::new(m[0], m[1], m[2], m[3])),
1162            ColorSpace::Srgb => Color::Rgb(Rgb::new(m[0], m[1], m[2], m[3])),
1163            ColorSpace::LinearRgb => {
1164                Color::LinearRgb(LinearRgb::new(m[0], m[1], m[2], m[3]))
1165            }
1166            ColorSpace::Hsl => {
1167                Color::Hsl(Hsl::new(RgbHue::from_degrees(m[0]), m[1], m[2], m[3]))
1168            }
1169            ColorSpace::Hsv => {
1170                Color::Hsv(Hsv::new(RgbHue::from_degrees(m[0]), m[1], m[2], m[3]))
1171            }
1172            ColorSpace::Cmyk => Color::Cmyk(Cmyk::new(m[0], m[1], m[2], m[3])),
1173            ColorSpace::D65Gray => Color::Luma(Luma::new(m[0], m[3])),
1174        })
1175    }
1176
1177    /// Construct a new RGBA color from 8-bit values.
1178    pub fn from_u8(r: u8, g: u8, b: u8, a: u8) -> Self {
1179        Self::Rgb(Rgb::new(
1180            f32::from(r) / 255.0,
1181            f32::from(g) / 255.0,
1182            f32::from(b) / 255.0,
1183            f32::from(a) / 255.0,
1184        ))
1185    }
1186
1187    /// Converts a 32-bit integer to an RGBA color.
1188    pub fn from_u32(color: u32) -> Self {
1189        Self::from_u8(
1190            ((color >> 24) & 0xFF) as u8,
1191            ((color >> 16) & 0xFF) as u8,
1192            ((color >> 8) & 0xFF) as u8,
1193            (color & 0xFF) as u8,
1194        )
1195    }
1196
1197    /// Returns the alpha channel of the color, if it has one.
1198    pub fn alpha(&self) -> Option<f32> {
1199        match self {
1200            Color::Cmyk(_) => None,
1201            Color::Luma(c) => Some(c.alpha),
1202            Color::Oklab(c) => Some(c.alpha),
1203            Color::Oklch(c) => Some(c.alpha),
1204            Color::Rgb(c) => Some(c.alpha),
1205            Color::LinearRgb(c) => Some(c.alpha),
1206            Color::Hsl(c) => Some(c.alpha),
1207            Color::Hsv(c) => Some(c.alpha),
1208        }
1209    }
1210
1211    /// Sets the alpha channel of the color, if it has one.
1212    pub fn with_alpha(mut self, alpha: f32) -> Self {
1213        match &mut self {
1214            Color::Cmyk(_) => {}
1215            Color::Luma(c) => c.alpha = alpha,
1216            Color::Oklab(c) => c.alpha = alpha,
1217            Color::Oklch(c) => c.alpha = alpha,
1218            Color::Rgb(c) => c.alpha = alpha,
1219            Color::LinearRgb(c) => c.alpha = alpha,
1220            Color::Hsl(c) => c.alpha = alpha,
1221            Color::Hsv(c) => c.alpha = alpha,
1222        }
1223
1224        self
1225    }
1226
1227    /// Scales the alpha value of a color by a given amount.
1228    ///
1229    /// For positive scales, computes `alpha + scale - alpha * scale`.
1230    /// For non-positive scales, computes `alpha + alpha * scale`.
1231    fn scale_alpha(self, scale: Ratio) -> StrResult<Color> {
1232        #[inline]
1233        fn transform<C>(mut color: Alpha<C, f32>, scale: Ratio) -> Alpha<C, f32> {
1234            let scale = scale.get() as f32;
1235            let factor = if scale > 0.0 { 1.0 - color.alpha } else { color.alpha };
1236            color.alpha = (color.alpha + scale * factor).clamp(0.0, 1.0);
1237            color
1238        }
1239
1240        Ok(match self {
1241            Color::Luma(c) => Color::Luma(transform(c, scale)),
1242            Color::Oklab(c) => Color::Oklab(transform(c, scale)),
1243            Color::Oklch(c) => Color::Oklch(transform(c, scale)),
1244            Color::Rgb(c) => Color::Rgb(transform(c, scale)),
1245            Color::LinearRgb(c) => Color::LinearRgb(transform(c, scale)),
1246            Color::Cmyk(_) => bail!("CMYK does not have an alpha component"),
1247            Color::Hsl(c) => Color::Hsl(transform(c, scale)),
1248            Color::Hsv(c) => Color::Hsv(transform(c, scale)),
1249        })
1250    }
1251
1252    /// Converts the color to a vec of four floats.
1253    pub fn to_vec4(&self) -> [f32; 4] {
1254        match self {
1255            Color::Luma(c) => [c.luma, c.luma, c.luma, c.alpha],
1256            Color::Oklab(c) => [c.l, c.a, c.b, c.alpha],
1257            Color::Oklch(c) => {
1258                [c.l, c.chroma, c.hue.into_degrees().rem_euclid(360.0), c.alpha]
1259            }
1260            Color::Rgb(c) => [c.red, c.green, c.blue, c.alpha],
1261            Color::LinearRgb(c) => [c.red, c.green, c.blue, c.alpha],
1262            Color::Cmyk(c) => [c.c, c.m, c.y, c.k],
1263            Color::Hsl(c) => [
1264                c.hue.into_degrees().rem_euclid(360.0),
1265                c.saturation,
1266                c.lightness,
1267                c.alpha,
1268            ],
1269            Color::Hsv(c) => {
1270                [c.hue.into_degrees().rem_euclid(360.0), c.saturation, c.value, c.alpha]
1271            }
1272        }
1273    }
1274
1275    /// Converts the color to a vec of four [`u8`]s.
1276    pub fn to_vec4_u8(&self) -> [u8; 4] {
1277        self.to_vec4().map(|x| (x * 255.0).round() as u8)
1278    }
1279
1280    pub fn to_space(self, space: ColorSpace) -> Self {
1281        match space {
1282            ColorSpace::D65Gray => Self::Luma(self.to_luma()),
1283            ColorSpace::Oklab => Self::Oklab(self.to_oklab()),
1284            ColorSpace::Oklch => Self::Oklch(self.to_oklch()),
1285            ColorSpace::Srgb => Self::Rgb(self.to_rgb()),
1286            ColorSpace::LinearRgb => Self::LinearRgb(self.to_linear_rgb()),
1287            ColorSpace::Cmyk => Self::Cmyk(self.to_cmyk()),
1288            ColorSpace::Hsl => Self::Hsl(self.to_hsl()),
1289            ColorSpace::Hsv => Self::Hsv(self.to_hsv()),
1290        }
1291    }
1292
1293    pub fn to_luma(self) -> Luma {
1294        match self {
1295            Self::Luma(c) => c,
1296            Self::Oklab(c) => Luma::from_color(c),
1297            Self::Oklch(c) => Luma::from_color(c),
1298            Self::Rgb(c) => Luma::from_color(c),
1299            Self::LinearRgb(c) => Luma::from_color(c),
1300            Self::Cmyk(c) => Luma::from_color(c.to_rgba()),
1301            Self::Hsl(c) => Luma::from_color(c),
1302            Self::Hsv(c) => Luma::from_color(c),
1303        }
1304    }
1305
1306    pub fn to_oklab(self) -> Oklab {
1307        match self {
1308            Self::Luma(c) => Oklab::from_color(c),
1309            Self::Oklab(c) => c,
1310            Self::Oklch(c) => Oklab::from_color(c),
1311            Self::Rgb(c) => Oklab::from_color(c),
1312            Self::LinearRgb(c) => Oklab::from_color(c),
1313            Self::Cmyk(c) => Oklab::from_color(c.to_rgba()),
1314            Self::Hsl(c) => Oklab::from_color(c),
1315            Self::Hsv(c) => Oklab::from_color(c),
1316        }
1317    }
1318
1319    pub fn to_oklch(self) -> Oklch {
1320        match self {
1321            Self::Luma(c) => Oklch::from_color(c),
1322            Self::Oklab(c) => Oklch::from_color(c),
1323            Self::Oklch(c) => c,
1324            Self::Rgb(c) => Oklch::from_color(c),
1325            Self::LinearRgb(c) => Oklch::from_color(c),
1326            Self::Cmyk(c) => Oklch::from_color(c.to_rgba()),
1327            Self::Hsl(c) => Oklch::from_color(c),
1328            Self::Hsv(c) => Oklch::from_color(c),
1329        }
1330    }
1331
1332    pub fn to_rgb(self) -> Rgb {
1333        match self {
1334            Self::Luma(c) => Rgb::from_color(c),
1335            Self::Oklab(c) => Rgb::from_color(c),
1336            Self::Oklch(c) => Rgb::from_color(c),
1337            Self::Rgb(c) => c,
1338            Self::LinearRgb(c) => Rgb::from_linear(c),
1339            Self::Cmyk(c) => Rgb::from_color(c.to_rgba()),
1340            Self::Hsl(c) => Rgb::from_color(c),
1341            Self::Hsv(c) => Rgb::from_color(c),
1342        }
1343    }
1344
1345    pub fn to_linear_rgb(self) -> LinearRgb {
1346        match self {
1347            Self::Luma(c) => LinearRgb::from_color(c),
1348            Self::Oklab(c) => LinearRgb::from_color(c),
1349            Self::Oklch(c) => LinearRgb::from_color(c),
1350            Self::Rgb(c) => LinearRgb::from_color(c),
1351            Self::LinearRgb(c) => c,
1352            Self::Cmyk(c) => LinearRgb::from_color(c.to_rgba()),
1353            Self::Hsl(c) => Rgb::from_color(c).into_linear(),
1354            Self::Hsv(c) => Rgb::from_color(c).into_linear(),
1355        }
1356    }
1357
1358    pub fn to_cmyk(self) -> Cmyk {
1359        match self {
1360            Self::Luma(c) => Cmyk::from_luma(c),
1361            Self::Oklab(c) => Cmyk::from_rgba(Rgb::from_color(c)),
1362            Self::Oklch(c) => Cmyk::from_rgba(Rgb::from_color(c)),
1363            Self::Rgb(c) => Cmyk::from_rgba(c),
1364            Self::LinearRgb(c) => Cmyk::from_rgba(Rgb::from_linear(c)),
1365            Self::Cmyk(c) => c,
1366            Self::Hsl(c) => Cmyk::from_rgba(Rgb::from_color(c)),
1367            Self::Hsv(c) => Cmyk::from_rgba(Rgb::from_color(c)),
1368        }
1369    }
1370
1371    pub fn to_hsl(self) -> Hsl {
1372        match self {
1373            Self::Luma(c) => Hsl::from_color(c),
1374            Self::Oklab(c) => Hsl::from_color(c),
1375            Self::Oklch(c) => Hsl::from_color(c),
1376            Self::Rgb(c) => Hsl::from_color(c),
1377            Self::LinearRgb(c) => Hsl::from_color(Rgb::from_linear(c)),
1378            Self::Cmyk(c) => Hsl::from_color(c.to_rgba()),
1379            Self::Hsl(c) => c,
1380            Self::Hsv(c) => Hsl::from_color(c),
1381        }
1382    }
1383
1384    pub fn to_hsv(self) -> Hsv {
1385        match self {
1386            Self::Luma(c) => Hsv::from_color(c),
1387            Self::Oklab(c) => Hsv::from_color(c),
1388            Self::Oklch(c) => Hsv::from_color(c),
1389            Self::Rgb(c) => Hsv::from_color(c),
1390            Self::LinearRgb(c) => Hsv::from_color(Rgb::from_linear(c)),
1391            Self::Cmyk(c) => Hsv::from_color(c.to_rgba()),
1392            Self::Hsl(c) => Hsv::from_color(c),
1393            Self::Hsv(c) => c,
1394        }
1395    }
1396}
1397
1398impl Debug for Color {
1399    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
1400        match self {
1401            Self::Luma(v) => write!(f, "Luma({}, {})", v.luma, v.alpha),
1402            Self::Oklab(v) => write!(f, "Oklab({}, {}, {}, {})", v.l, v.a, v.b, v.alpha),
1403            Self::Oklch(v) => {
1404                write!(
1405                    f,
1406                    "Oklch({}, {}, {:?}, {})",
1407                    v.l,
1408                    v.chroma,
1409                    hue_angle(v.hue.into_degrees()),
1410                    v.alpha
1411                )
1412            }
1413            Self::Rgb(v) => {
1414                write!(f, "Rgb({}, {}, {}, {})", v.red, v.green, v.blue, v.alpha)
1415            }
1416            Self::LinearRgb(v) => {
1417                write!(f, "LinearRgb({}, {}, {}, {})", v.red, v.green, v.blue, v.alpha)
1418            }
1419            Self::Cmyk(v) => write!(f, "Cmyk({}, {}, {}, {})", v.c, v.m, v.y, v.k),
1420            Self::Hsl(v) => write!(
1421                f,
1422                "Hsl({:?}, {}, {}, {})",
1423                hue_angle(v.hue.into_degrees()),
1424                v.saturation,
1425                v.lightness,
1426                v.alpha
1427            ),
1428            Self::Hsv(v) => write!(
1429                f,
1430                "Hsv({:?}, {}, {}, {})",
1431                hue_angle(v.hue.into_degrees()),
1432                v.saturation,
1433                v.value,
1434                v.alpha
1435            ),
1436        }
1437    }
1438}
1439
1440impl Repr for Color {
1441    fn repr(&self) -> EcoString {
1442        match self {
1443            Self::Luma(c) => {
1444                if c.alpha == 1.0 {
1445                    eco_format!("luma({})", Ratio::new(c.luma.into()).repr())
1446                } else {
1447                    eco_format!(
1448                        "luma({}, {})",
1449                        Ratio::new(c.luma.into()).repr(),
1450                        Ratio::new(c.alpha.into()).repr(),
1451                    )
1452                }
1453            }
1454            Self::Rgb(_) => eco_format!("rgb({})", self.to_hex().repr()),
1455            Self::LinearRgb(c) => {
1456                if c.alpha == 1.0 {
1457                    eco_format!(
1458                        "color.linear-rgb({}, {}, {})",
1459                        Ratio::new(c.red.into()).repr(),
1460                        Ratio::new(c.green.into()).repr(),
1461                        Ratio::new(c.blue.into()).repr(),
1462                    )
1463                } else {
1464                    eco_format!(
1465                        "color.linear-rgb({}, {}, {}, {})",
1466                        Ratio::new(c.red.into()).repr(),
1467                        Ratio::new(c.green.into()).repr(),
1468                        Ratio::new(c.blue.into()).repr(),
1469                        Ratio::new(c.alpha.into()).repr(),
1470                    )
1471                }
1472            }
1473            Self::Cmyk(c) => {
1474                eco_format!(
1475                    "cmyk({}, {}, {}, {})",
1476                    Ratio::new(c.c.into()).repr(),
1477                    Ratio::new(c.m.into()).repr(),
1478                    Ratio::new(c.y.into()).repr(),
1479                    Ratio::new(c.k.into()).repr(),
1480                )
1481            }
1482            Self::Oklab(c) => {
1483                if c.alpha == 1.0 {
1484                    eco_format!(
1485                        "oklab({}, {}, {})",
1486                        Ratio::new(c.l.into()).repr(),
1487                        repr::format_float_component(c.a.into()),
1488                        repr::format_float_component(c.b.into()),
1489                    )
1490                } else {
1491                    eco_format!(
1492                        "oklab({}, {}, {}, {})",
1493                        Ratio::new(c.l.into()).repr(),
1494                        repr::format_float_component(c.a.into()),
1495                        repr::format_float_component(c.b.into()),
1496                        Ratio::new(c.alpha.into()).repr(),
1497                    )
1498                }
1499            }
1500            Self::Oklch(c) => {
1501                if c.alpha == 1.0 {
1502                    eco_format!(
1503                        "oklch({}, {}, {})",
1504                        Ratio::new(c.l.into()).repr(),
1505                        repr::format_float_component(c.chroma.into()),
1506                        hue_angle(c.hue.into_degrees()).repr(),
1507                    )
1508                } else {
1509                    eco_format!(
1510                        "oklch({}, {}, {}, {})",
1511                        Ratio::new(c.l.into()).repr(),
1512                        repr::format_float_component(c.chroma.into()),
1513                        hue_angle(c.hue.into_degrees()).repr(),
1514                        Ratio::new(c.alpha.into()).repr(),
1515                    )
1516                }
1517            }
1518            Self::Hsl(c) => {
1519                if c.alpha == 1.0 {
1520                    eco_format!(
1521                        "color.hsl({}, {}, {})",
1522                        hue_angle(c.hue.into_degrees()).repr(),
1523                        Ratio::new(c.saturation.into()).repr(),
1524                        Ratio::new(c.lightness.into()).repr(),
1525                    )
1526                } else {
1527                    eco_format!(
1528                        "color.hsl({}, {}, {}, {})",
1529                        hue_angle(c.hue.into_degrees()).repr(),
1530                        Ratio::new(c.saturation.into()).repr(),
1531                        Ratio::new(c.lightness.into()).repr(),
1532                        Ratio::new(c.alpha.into()).repr(),
1533                    )
1534                }
1535            }
1536            Self::Hsv(c) => {
1537                if c.alpha == 1.0 {
1538                    eco_format!(
1539                        "color.hsv({}, {}, {})",
1540                        hue_angle(c.hue.into_degrees()).repr(),
1541                        Ratio::new(c.saturation.into()).repr(),
1542                        Ratio::new(c.value.into()).repr(),
1543                    )
1544                } else {
1545                    eco_format!(
1546                        "color.hsv({}, {}, {}, {})",
1547                        hue_angle(c.hue.into_degrees()).repr(),
1548                        Ratio::new(c.saturation.into()).repr(),
1549                        Ratio::new(c.value.into()).repr(),
1550                        Ratio::new(c.alpha.into()).repr(),
1551                    )
1552                }
1553            }
1554        }
1555    }
1556}
1557
1558fn hue_angle(degrees: f32) -> Angle {
1559    Angle::deg(f64::from(degrees).rem_euclid(360.0))
1560}
1561
1562impl PartialEq for Color {
1563    fn eq(&self, other: &Self) -> bool {
1564        match (self, other) {
1565            // Lower precision for comparison to avoid rounding errors.
1566            // Keeps backward compatibility with previous versions of Typst.
1567            (Self::Rgb(_), Self::Rgb(_)) => self.to_vec4_u8() == other.to_vec4_u8(),
1568            (Self::Luma(a), Self::Luma(b)) => {
1569                (a.luma * 255.0).round() as u8 == (b.luma * 255.0).round() as u8
1570            }
1571            (Self::Oklab(a), Self::Oklab(b)) => a == b,
1572            (Self::Oklch(a), Self::Oklch(b)) => a == b,
1573            (Self::LinearRgb(a), Self::LinearRgb(b)) => a == b,
1574            (Self::Cmyk(a), Self::Cmyk(b)) => a == b,
1575            (Self::Hsl(a), Self::Hsl(b)) => a == b,
1576            (Self::Hsv(a), Self::Hsv(b)) => a == b,
1577            _ => false,
1578        }
1579    }
1580}
1581
1582impl Eq for Color {}
1583
1584impl Hash for Color {
1585    fn hash<H: Hasher>(&self, state: &mut H) {
1586        core::mem::discriminant(self).hash(state);
1587        let [x, y, z, w] = self.to_vec4();
1588        x.to_bits().hash(state);
1589        y.to_bits().hash(state);
1590        z.to_bits().hash(state);
1591        w.to_bits().hash(state);
1592    }
1593}
1594
1595impl FromStr for Color {
1596    type Err = &'static str;
1597
1598    /// Constructs a new color from hex strings like the following:
1599    /// - `#aef` (shorthand, with leading hash),
1600    /// - `7a03c2` (without alpha),
1601    /// - `abcdefff` (with alpha).
1602    ///
1603    /// The hash is optional and both lower and upper case are fine.
1604    fn from_str(hex_str: &str) -> Result<Self, Self::Err> {
1605        let hex_str = hex_str.strip_prefix('#').unwrap_or(hex_str);
1606        if hex_str.chars().any(|c| !c.is_ascii_hexdigit()) {
1607            return Err("color string contains non-hexadecimal letters");
1608        }
1609
1610        let len = hex_str.len();
1611        let long = len == 6 || len == 8;
1612        let short = len == 3 || len == 4;
1613        let alpha = len == 4 || len == 8;
1614        if !long && !short {
1615            return Err("color string has wrong length");
1616        }
1617
1618        let mut values: [u8; 4] = [u8::MAX; 4];
1619        for elem in if alpha { 0..4 } else { 0..3 } {
1620            let item_len = if long { 2 } else { 1 };
1621            let pos = elem * item_len;
1622
1623            let item = &hex_str[pos..(pos + item_len)];
1624            values[elem] = u8::from_str_radix(item, 16).unwrap();
1625
1626            if short {
1627                // Duplicate number for shorthand notation, i.e. `a` -> `aa`
1628                values[elem] += values[elem] * 16;
1629            }
1630        }
1631
1632        Ok(Self::from_u8(values[0], values[1], values[2], values[3]))
1633    }
1634}
1635
1636impl From<Luma> for Color {
1637    fn from(c: Luma) -> Self {
1638        Self::Luma(c)
1639    }
1640}
1641
1642impl From<Oklab> for Color {
1643    fn from(c: Oklab) -> Self {
1644        Self::Oklab(c)
1645    }
1646}
1647
1648impl From<Oklch> for Color {
1649    fn from(c: Oklch) -> Self {
1650        Self::Oklch(c)
1651    }
1652}
1653
1654impl From<Rgb> for Color {
1655    fn from(c: Rgb) -> Self {
1656        Self::Rgb(c)
1657    }
1658}
1659
1660impl From<LinearRgb> for Color {
1661    fn from(c: LinearRgb) -> Self {
1662        Self::LinearRgb(c)
1663    }
1664}
1665
1666impl From<Cmyk> for Color {
1667    fn from(c: Cmyk) -> Self {
1668        Self::Cmyk(c)
1669    }
1670}
1671
1672impl From<Hsl> for Color {
1673    fn from(c: Hsl) -> Self {
1674        Self::Hsl(c)
1675    }
1676}
1677
1678impl From<Hsv> for Color {
1679    fn from(c: Hsv) -> Self {
1680        Self::Hsv(c)
1681    }
1682}
1683
1684/// An 8-bit CMYK color.
1685#[derive(Debug, Copy, Clone, PartialEq)]
1686pub struct Cmyk {
1687    /// The cyan component.
1688    pub c: f32,
1689    /// The magenta component.
1690    pub m: f32,
1691    /// The yellow component.
1692    pub y: f32,
1693    /// The key (black) component.
1694    pub k: f32,
1695}
1696
1697impl Cmyk {
1698    fn new(c: f32, m: f32, y: f32, k: f32) -> Self {
1699        Self { c, m, y, k }
1700    }
1701
1702    fn from_luma(luma: Luma) -> Self {
1703        let l = 1.0 - luma.luma;
1704        Cmyk::new(l * 0.75, l * 0.68, l * 0.67, l * 0.90)
1705    }
1706
1707    // This still uses naive conversion, because qcms does not support
1708    // converting to CMYK yet.
1709    fn from_rgba(rgba: Rgb) -> Self {
1710        let r = rgba.red;
1711        let g = rgba.green;
1712        let b = rgba.blue;
1713
1714        let k = 1.0 - r.max(g).max(b);
1715        if k == 1.0 {
1716            return Cmyk::new(0.0, 0.0, 0.0, 1.0);
1717        }
1718
1719        let c = (1.0 - r - k) / (1.0 - k);
1720        let m = (1.0 - g - k) / (1.0 - k);
1721        let y = (1.0 - b - k) / (1.0 - k);
1722
1723        Cmyk::new(c, m, y, k)
1724    }
1725
1726    fn to_rgba(self) -> Rgb {
1727        let mut dest: [u8; 3] = [0; 3];
1728        TO_SRGB.convert(
1729            &[
1730                (self.c * 255.0).round() as u8,
1731                (self.m * 255.0).round() as u8,
1732                (self.y * 255.0).round() as u8,
1733                (self.k * 255.0).round() as u8,
1734            ],
1735            &mut dest,
1736        );
1737
1738        Rgb::new(
1739            f32::from(dest[0]) / 255.0,
1740            f32::from(dest[1]) / 255.0,
1741            f32::from(dest[2]) / 255.0,
1742            1.0,
1743        )
1744    }
1745
1746    fn lighten(self, factor: f32) -> Self {
1747        let lighten = |u: f32| (u - u * factor).clamp(0.0, 1.0);
1748        Self::new(lighten(self.c), lighten(self.m), lighten(self.y), lighten(self.k))
1749    }
1750
1751    fn darken(self, factor: f32) -> Self {
1752        let darken = |u: f32| (u + (1.0 - u) * factor).clamp(0.0, 1.0);
1753        Self::new(darken(self.c), darken(self.m), darken(self.y), darken(self.k))
1754    }
1755}
1756
1757/// A color with a weight.
1758pub struct WeightedColor {
1759    color: Color,
1760    weight: f64,
1761}
1762
1763impl WeightedColor {
1764    /// Create a new weighted color.
1765    pub const fn new(color: Color, weight: f64) -> Self {
1766        Self { color, weight }
1767    }
1768}
1769
1770cast! {
1771    WeightedColor,
1772    self => array![self.color, Value::Float(self.weight)].into_value(),
1773    color: Color => Self { color, weight: 1.0 },
1774    v: Array => {
1775        let mut iter = v.into_iter();
1776        match (iter.next(), iter.next(), iter.next()) {
1777            (Some(c), Some(w), None) => Self {
1778                color: c.cast()?,
1779                weight: w.cast::<Weight>()?.0,
1780            },
1781            _ => bail!("expected a color or color-weight pair"),
1782        }
1783    }
1784}
1785
1786/// A weight for color mixing.
1787struct Weight(f64);
1788
1789cast! {
1790    Weight,
1791    v: f64 => Self(v),
1792    v: Ratio => Self(v.get()),
1793}
1794
1795/// A color space for color manipulation.
1796#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
1797pub enum ColorSpace {
1798    /// The perceptual Oklab color space.
1799    Oklab,
1800    /// The perceptual Oklch color space.
1801    Oklch,
1802    /// The standard RGB color space.
1803    Srgb,
1804    /// The D65-gray color space.
1805    D65Gray,
1806    /// The linear RGB color space.
1807    LinearRgb,
1808    /// The HSL color space.
1809    Hsl,
1810    /// The HSV color space.
1811    Hsv,
1812    /// The CMYK color space.
1813    Cmyk,
1814}
1815
1816impl ColorSpace {
1817    /// Returns the index of the hue component in this color space, if it has
1818    /// one.
1819    pub fn hue_index(&self) -> Option<usize> {
1820        match self {
1821            Self::Hsl | Self::Hsv => Some(0),
1822            Self::Oklch => Some(2),
1823            _ => None,
1824        }
1825    }
1826}
1827
1828cast! {
1829    ColorSpace,
1830    self => match self {
1831        Self::Oklab => Color::oklab_data(),
1832        Self::Oklch => Color::oklch_data(),
1833        Self::Srgb => Color::rgb_data(),
1834        Self::D65Gray => Color::luma_data(),
1835        Self::LinearRgb => Color::linear_rgb_data(),
1836        Self::Hsl => Color::hsl_data(),
1837        Self::Hsv => Color::hsv_data(),
1838        Self::Cmyk => Color::cmyk_data(),
1839    }.into_value(),
1840    v: Value => {
1841        let expected = "expected `rgb`, `luma`, `cmyk`, `oklab`, `oklch`, `color.linear-rgb`, `color.hsl`, or `color.hsv`";
1842        let Value::Func(func) = v else {
1843            bail!("{expected}, found {}", v.ty());
1844        };
1845
1846        // Here comparing the function pointer since it's `Eq`
1847        // whereas the `NativeFuncData` is not.
1848        if func == Color::oklab_data() {
1849            Self::Oklab
1850        } else if func == Color::oklch_data() {
1851            Self::Oklch
1852        } else if func == Color::rgb_data() {
1853            Self::Srgb
1854        } else if func == Color::luma_data() {
1855            Self::D65Gray
1856        } else if func == Color::linear_rgb_data() {
1857            Self::LinearRgb
1858        } else if func == Color::hsl_data() {
1859            Self::Hsl
1860        } else if func == Color::hsv_data() {
1861            Self::Hsv
1862        } else if func == Color::cmyk_data() {
1863            Self::Cmyk
1864        } else {
1865            bail!("{expected}");
1866        }
1867    },
1868}
1869
1870/// A component that must be a ratio.
1871pub struct RatioComponent(Ratio);
1872
1873cast! {
1874    RatioComponent,
1875    self => self.0.into_value(),
1876    v: Ratio => if (0.0 ..= 1.0).contains(&v.get()) {
1877        Self(v)
1878    } else {
1879        bail!("ratio must be between 0% and 100%");
1880    },
1881}
1882
1883/// A chroma color component.
1884///
1885/// Must either be:
1886/// - a ratio, in which case it is relative to 0.4.
1887/// - a float, in which case it is taken literally.
1888pub struct ChromaComponent(f32);
1889
1890cast! {
1891    ChromaComponent,
1892    v: f64 => Self(v as f32),
1893    v: Ratio => Self((v.get() * 0.4) as f32),
1894}
1895
1896/// An integer or ratio component.
1897pub struct Component(Ratio);
1898
1899cast! {
1900    Component,
1901    self => self.0.into_value(),
1902    v: i64 => match v {
1903        0 ..= 255 => Self(Ratio::new(v as f64 / 255.0)),
1904        _ => bail!("number must be between 0 and 255"),
1905    },
1906    v: Ratio => if (0.0 ..= 1.0).contains(&v.get()) {
1907        Self(v)
1908    } else {
1909        bail!("ratio must be between 0% and 100%");
1910    },
1911}
1912
1913/// A module with all preset color maps.
1914fn map() -> Module {
1915    let mut scope = Scope::new();
1916    scope.define("turbo", turbo());
1917    scope.define("cividis", cividis());
1918    scope.define("rainbow", rainbow());
1919    scope.define("spectral", spectral());
1920    scope.define("viridis", viridis());
1921    scope.define("inferno", inferno());
1922    scope.define("magma", magma());
1923    scope.define("plasma", plasma());
1924    scope.define("rocket", rocket());
1925    scope.define("mako", mako());
1926    scope.define("vlag", vlag());
1927    scope.define("icefire", icefire());
1928    scope.define("flare", flare());
1929    scope.define("crest", crest());
1930    Module::new("map", scope)
1931}
1932
1933/// Defines a gradient preset as a series of colors expressed as u32s.
1934macro_rules! preset {
1935    ($name:ident; $($colors:literal),* $(,)*) => {
1936        fn $name() -> Array {
1937            Array::from(
1938                [$(Color::from_u32($colors)),*]
1939                    .iter()
1940                    .map(|c| c.into_value())
1941                    .collect::<EcoVec<_>>()
1942            )
1943        }
1944    };
1945}
1946
1947preset!(turbo; 0x23171bff, 0x271a28ff, 0x2b1c33ff, 0x2f1e3fff, 0x32204aff, 0x362354ff, 0x39255fff, 0x3b2768ff, 0x3e2a72ff, 0x402c7bff, 0x422f83ff, 0x44318bff, 0x453493ff, 0x46369bff, 0x4839a2ff, 0x493ca8ff, 0x493eafff, 0x4a41b5ff, 0x4a44bbff, 0x4b46c0ff, 0x4b49c5ff, 0x4b4ccaff, 0x4b4ecfff, 0x4b51d3ff, 0x4a54d7ff, 0x4a56dbff, 0x4959deff, 0x495ce2ff, 0x485fe5ff, 0x4761e7ff, 0x4664eaff, 0x4567ecff, 0x446aeeff, 0x446df0ff, 0x426ff2ff, 0x4172f3ff, 0x4075f5ff, 0x3f78f6ff, 0x3e7af7ff, 0x3d7df7ff, 0x3c80f8ff, 0x3a83f9ff, 0x3985f9ff, 0x3888f9ff, 0x378bf9ff, 0x368df9ff, 0x3590f8ff, 0x3393f8ff, 0x3295f7ff, 0x3198f7ff, 0x309bf6ff, 0x2f9df5ff, 0x2ea0f4ff, 0x2da2f3ff, 0x2ca5f1ff, 0x2ba7f0ff, 0x2aaaefff, 0x2aacedff, 0x29afecff, 0x28b1eaff, 0x28b4e8ff, 0x27b6e6ff, 0x27b8e5ff, 0x26bbe3ff, 0x26bde1ff, 0x26bfdfff, 0x25c1dcff, 0x25c3daff, 0x25c6d8ff, 0x25c8d6ff, 0x25cad3ff, 0x25ccd1ff, 0x25cecfff, 0x26d0ccff, 0x26d2caff, 0x26d4c8ff, 0x27d6c5ff, 0x27d8c3ff, 0x28d9c0ff, 0x29dbbeff, 0x29ddbbff, 0x2adfb8ff, 0x2be0b6ff, 0x2ce2b3ff, 0x2de3b1ff, 0x2ee5aeff, 0x30e6acff, 0x31e8a9ff, 0x32e9a6ff, 0x34eba4ff, 0x35eca1ff, 0x37ed9fff, 0x39ef9cff, 0x3af09aff, 0x3cf197ff, 0x3ef295ff, 0x40f392ff, 0x42f490ff, 0x44f58dff, 0x46f68bff, 0x48f788ff, 0x4af786ff, 0x4df884ff, 0x4ff981ff, 0x51fa7fff, 0x54fa7dff, 0x56fb7aff, 0x59fb78ff, 0x5cfc76ff, 0x5efc74ff, 0x61fd71ff, 0x64fd6fff, 0x66fd6dff, 0x69fd6bff, 0x6cfd69ff, 0x6ffe67ff, 0x72fe65ff, 0x75fe63ff, 0x78fe61ff, 0x7bfe5fff, 0x7efd5dff, 0x81fd5cff, 0x84fd5aff, 0x87fd58ff, 0x8afc56ff, 0x8dfc55ff, 0x90fb53ff, 0x93fb51ff, 0x96fa50ff, 0x99fa4eff, 0x9cf94dff, 0x9ff84bff, 0xa2f84aff, 0xa6f748ff, 0xa9f647ff, 0xacf546ff, 0xaff444ff, 0xb2f343ff, 0xb5f242ff, 0xb8f141ff, 0xbbf03fff, 0xbeef3eff, 0xc1ed3dff, 0xc3ec3cff, 0xc6eb3bff, 0xc9e93aff, 0xcce839ff, 0xcfe738ff, 0xd1e537ff, 0xd4e336ff, 0xd7e235ff, 0xd9e034ff, 0xdcdf33ff, 0xdedd32ff, 0xe0db32ff, 0xe3d931ff, 0xe5d730ff, 0xe7d52fff, 0xe9d42fff, 0xecd22eff, 0xeed02dff, 0xf0ce2cff, 0xf1cb2cff, 0xf3c92bff, 0xf5c72bff, 0xf7c52aff, 0xf8c329ff, 0xfac029ff, 0xfbbe28ff, 0xfdbc28ff, 0xfeb927ff, 0xffb727ff, 0xffb526ff, 0xffb226ff, 0xffb025ff, 0xffad25ff, 0xffab24ff, 0xffa824ff, 0xffa623ff, 0xffa323ff, 0xffa022ff, 0xff9e22ff, 0xff9b21ff, 0xff9921ff, 0xff9621ff, 0xff9320ff, 0xff9020ff, 0xff8e1fff, 0xff8b1fff, 0xff881eff, 0xff851eff, 0xff831dff, 0xff801dff, 0xff7d1dff, 0xff7a1cff, 0xff781cff, 0xff751bff, 0xff721bff, 0xff6f1aff, 0xfd6c1aff, 0xfc6a19ff, 0xfa6719ff, 0xf96418ff, 0xf76118ff, 0xf65f18ff, 0xf45c17ff, 0xf25916ff, 0xf05716ff, 0xee5415ff, 0xec5115ff, 0xea4f14ff, 0xe84c14ff, 0xe64913ff, 0xe44713ff, 0xe24412ff, 0xdf4212ff, 0xdd3f11ff, 0xda3d10ff, 0xd83a10ff, 0xd5380fff, 0xd3360fff, 0xd0330eff, 0xce310dff, 0xcb2f0dff, 0xc92d0cff, 0xc62a0bff, 0xc3280bff, 0xc1260aff, 0xbe2409ff, 0xbb2309ff, 0xb92108ff, 0xb61f07ff, 0xb41d07ff, 0xb11b06ff, 0xaf1a05ff, 0xac1805ff, 0xaa1704ff, 0xa81604ff, 0xa51403ff, 0xa31302ff, 0xa11202ff, 0x9f1101ff, 0x9d1000ff, 0x9b0f00ff, 0x9a0e00ff, 0x980e00ff, 0x960d00ff, 0x950c00ff, 0x940c00ff, 0x930c00ff, 0x920c00ff, 0x910b00ff, 0x910c00ff, 0x900c00ff, 0x900c00ff, 0x900c00ff);
1948preset!(cividis; 0x002051ff, 0x002153ff, 0x002255ff, 0x002356ff, 0x002358ff, 0x002459ff, 0x00255aff, 0x00255cff, 0x00265dff, 0x00275eff, 0x00275fff, 0x002860ff, 0x002961ff, 0x002962ff, 0x002a63ff, 0x002b64ff, 0x012b65ff, 0x022c65ff, 0x032d66ff, 0x042d67ff, 0x052e67ff, 0x052f68ff, 0x063069ff, 0x073069ff, 0x08316aff, 0x09326aff, 0x0b326aff, 0x0c336bff, 0x0d346bff, 0x0e346bff, 0x0f356cff, 0x10366cff, 0x12376cff, 0x13376dff, 0x14386dff, 0x15396dff, 0x17396dff, 0x183a6dff, 0x193b6dff, 0x1a3b6dff, 0x1c3c6eff, 0x1d3d6eff, 0x1e3e6eff, 0x203e6eff, 0x213f6eff, 0x23406eff, 0x24406eff, 0x25416eff, 0x27426eff, 0x28436eff, 0x29436eff, 0x2b446eff, 0x2c456eff, 0x2e456eff, 0x2f466eff, 0x30476eff, 0x32486eff, 0x33486eff, 0x34496eff, 0x364a6eff, 0x374a6eff, 0x394b6eff, 0x3a4c6eff, 0x3b4d6eff, 0x3d4d6eff, 0x3e4e6eff, 0x3f4f6eff, 0x414f6eff, 0x42506eff, 0x43516dff, 0x44526dff, 0x46526dff, 0x47536dff, 0x48546dff, 0x4a546dff, 0x4b556dff, 0x4c566dff, 0x4d576dff, 0x4e576eff, 0x50586eff, 0x51596eff, 0x52596eff, 0x535a6eff, 0x545b6eff, 0x565c6eff, 0x575c6eff, 0x585d6eff, 0x595e6eff, 0x5a5e6eff, 0x5b5f6eff, 0x5c606eff, 0x5d616eff, 0x5e616eff, 0x60626eff, 0x61636fff, 0x62646fff, 0x63646fff, 0x64656fff, 0x65666fff, 0x66666fff, 0x67676fff, 0x686870ff, 0x696970ff, 0x6a6970ff, 0x6b6a70ff, 0x6c6b70ff, 0x6d6c70ff, 0x6d6c71ff, 0x6e6d71ff, 0x6f6e71ff, 0x706f71ff, 0x716f71ff, 0x727071ff, 0x737172ff, 0x747172ff, 0x757272ff, 0x767372ff, 0x767472ff, 0x777473ff, 0x787573ff, 0x797673ff, 0x7a7773ff, 0x7b7774ff, 0x7b7874ff, 0x7c7974ff, 0x7d7a74ff, 0x7e7a74ff, 0x7f7b75ff, 0x807c75ff, 0x807d75ff, 0x817d75ff, 0x827e75ff, 0x837f76ff, 0x848076ff, 0x858076ff, 0x858176ff, 0x868276ff, 0x878376ff, 0x888477ff, 0x898477ff, 0x898577ff, 0x8a8677ff, 0x8b8777ff, 0x8c8777ff, 0x8d8877ff, 0x8e8978ff, 0x8e8a78ff, 0x8f8a78ff, 0x908b78ff, 0x918c78ff, 0x928d78ff, 0x938e78ff, 0x938e78ff, 0x948f78ff, 0x959078ff, 0x969178ff, 0x979278ff, 0x989278ff, 0x999378ff, 0x9a9478ff, 0x9b9578ff, 0x9b9678ff, 0x9c9678ff, 0x9d9778ff, 0x9e9878ff, 0x9f9978ff, 0xa09a78ff, 0xa19a78ff, 0xa29b78ff, 0xa39c78ff, 0xa49d78ff, 0xa59e77ff, 0xa69e77ff, 0xa79f77ff, 0xa8a077ff, 0xa9a177ff, 0xaaa276ff, 0xaba376ff, 0xaca376ff, 0xada476ff, 0xaea575ff, 0xafa675ff, 0xb0a775ff, 0xb2a874ff, 0xb3a874ff, 0xb4a974ff, 0xb5aa73ff, 0xb6ab73ff, 0xb7ac72ff, 0xb8ad72ff, 0xbaae72ff, 0xbbae71ff, 0xbcaf71ff, 0xbdb070ff, 0xbeb170ff, 0xbfb26fff, 0xc1b36fff, 0xc2b46eff, 0xc3b56dff, 0xc4b56dff, 0xc5b66cff, 0xc7b76cff, 0xc8b86bff, 0xc9b96aff, 0xcaba6aff, 0xccbb69ff, 0xcdbc68ff, 0xcebc68ff, 0xcfbd67ff, 0xd1be66ff, 0xd2bf66ff, 0xd3c065ff, 0xd4c164ff, 0xd6c263ff, 0xd7c363ff, 0xd8c462ff, 0xd9c561ff, 0xdbc660ff, 0xdcc660ff, 0xddc75fff, 0xdec85eff, 0xe0c95dff, 0xe1ca5cff, 0xe2cb5cff, 0xe3cc5bff, 0xe4cd5aff, 0xe6ce59ff, 0xe7cf58ff, 0xe8d058ff, 0xe9d157ff, 0xead256ff, 0xebd355ff, 0xecd454ff, 0xedd453ff, 0xeed553ff, 0xf0d652ff, 0xf1d751ff, 0xf1d850ff, 0xf2d950ff, 0xf3da4fff, 0xf4db4eff, 0xf5dc4dff, 0xf6dd4dff, 0xf7de4cff, 0xf8df4bff, 0xf8e04bff, 0xf9e14aff, 0xfae249ff, 0xfae349ff, 0xfbe448ff, 0xfbe548ff, 0xfce647ff, 0xfce746ff, 0xfde846ff, 0xfde946ff, 0xfdea45ff);
1949preset!(rainbow;  0x7c4bbbff, 0x7f4bbcff, 0x824bbdff, 0x854abeff, 0x884abeff, 0x8b4abfff, 0x8e49bfff, 0x9149c0ff, 0x9449c0ff, 0x9748c0ff, 0x9a48c1ff, 0x9e48c1ff, 0xa148c1ff, 0xa447c1ff, 0xa747c1ff, 0xaa47c0ff, 0xad47c0ff, 0xb046c0ff, 0xb446bfff, 0xb746bfff, 0xba46beff, 0xbd46beff, 0xc046bdff, 0xc346bcff, 0xc646bbff, 0xc946baff, 0xcc46b9ff, 0xcf46b8ff, 0xd246b7ff, 0xd446b5ff, 0xd747b4ff, 0xda47b3ff, 0xdd47b1ff, 0xdf47b0ff, 0xe248aeff, 0xe448acff, 0xe748abff, 0xe949a9ff, 0xec49a7ff, 0xee4aa5ff, 0xf04ba3ff, 0xf34ba1ff, 0xf54c9fff, 0xf74c9dff, 0xf94d9bff, 0xfb4e98ff, 0xfd4f96ff, 0xfe5094ff, 0xff5191ff, 0xff528fff, 0xff538dff, 0xff548aff, 0xff5588ff, 0xff5685ff, 0xff5783ff, 0xff5880ff, 0xff5a7eff, 0xff5b7bff, 0xff5c79ff, 0xff5e76ff, 0xff5f74ff, 0xff6171ff, 0xff626fff, 0xff646cff, 0xff666aff, 0xff6767ff, 0xff6965ff, 0xff6b63ff, 0xff6d60ff, 0xff6e5eff, 0xff705cff, 0xff7259ff, 0xff7457ff, 0xff7655ff, 0xff7853ff, 0xff7a51ff, 0xff7c4fff, 0xff7f4dff, 0xff814bff, 0xff8349ff, 0xff8547ff, 0xff8745ff, 0xff8a44ff, 0xff8c42ff, 0xff8e40ff, 0xff913fff, 0xff933eff, 0xff953cff, 0xff983bff, 0xfd9a3aff, 0xfb9c39ff, 0xfa9f38ff, 0xf8a137ff, 0xf6a436ff, 0xf4a636ff, 0xf2a935ff, 0xf0ab35ff, 0xeeae34ff, 0xecb034ff, 0xeab234ff, 0xe8b534ff, 0xe6b734ff, 0xe4ba34ff, 0xe1bc34ff, 0xdfbf35ff, 0xddc135ff, 0xdbc336ff, 0xd9c636ff, 0xd6c837ff, 0xd4ca38ff, 0xd2cd39ff, 0xd0cf3aff, 0xcdd13bff, 0xcbd33dff, 0xc9d63eff, 0xc7d840ff, 0xc5da41ff, 0xc3dc43ff, 0xc1de45ff, 0xbfe047ff, 0xbde249ff, 0xbbe44bff, 0xb9e64dff, 0xb7e84fff, 0xb5ea52ff, 0xb3ec54ff, 0xb2ed57ff, 0xb0ef59ff, 0xadf05aff, 0xaaf15aff, 0xa6f159ff, 0xa2f259ff, 0x9ff259ff, 0x9bf358ff, 0x97f358ff, 0x94f459ff, 0x90f459ff, 0x8df559ff, 0x89f559ff, 0x85f65aff, 0x82f65bff, 0x7ff65bff, 0x7ef75cff, 0x7cf75dff, 0x7bf75eff, 0x7af75fff, 0x79f760ff, 0x78f762ff, 0x77f763ff, 0x76f764ff, 0x75f766ff, 0x74f768ff, 0x73f769ff, 0x72f76bff, 0x71f76dff, 0x70f76fff, 0x6ff671ff, 0x6ef673ff, 0x6df675ff, 0x6df577ff, 0x6cf579ff, 0x6bf47cff, 0x6af37eff, 0x69f380ff, 0x68f283ff, 0x67f185ff, 0x66f188ff, 0x66f08aff, 0x65ef8dff, 0x64ee8fff, 0x63ed92ff, 0x62ec94ff, 0x62eb97ff, 0x61ea9aff, 0x60e89cff, 0x5fe79fff, 0x5fe6a1ff, 0x5ee4a4ff, 0x5de3a7ff, 0x5ce2a9ff, 0x5ce0acff, 0x5bdfafff, 0x5addb1ff, 0x5adbb4ff, 0x59dab6ff, 0x58d8b9ff, 0x58d6bbff, 0x57d5beff, 0x56d3c0ff, 0x56d1c2ff, 0x55cfc5ff, 0x54cdc7ff, 0x54cbc9ff, 0x53c9cbff, 0x52c7cdff, 0x52c5cfff, 0x51c3d1ff, 0x51c1d3ff, 0x50bfd5ff, 0x50bdd7ff, 0x4fbbd9ff, 0x4eb9daff, 0x4eb6dcff, 0x4db4ddff, 0x4db2dfff, 0x4cb0e0ff, 0x4caee2ff, 0x4babe3ff, 0x4ba9e4ff, 0x4aa7e5ff, 0x4aa4e6ff, 0x49a2e7ff, 0x49a0e8ff, 0x489ee8ff, 0x489be9ff, 0x4799e9ff, 0x4797eaff, 0x4694eaff, 0x4692eaff, 0x4690ebff, 0x458eebff, 0x478bebff, 0x4889ebff, 0x4a87eaff, 0x4c85eaff, 0x4e82eaff, 0x5080e9ff, 0x527ee9ff, 0x537ce8ff, 0x557ae7ff, 0x5778e7ff, 0x5975e6ff, 0x5b73e5ff, 0x5c71e4ff, 0x5e6fe3ff, 0x606de1ff, 0x626be0ff, 0x6369dfff, 0x6567ddff, 0x6765dcff, 0x6864daff, 0x6a62d9ff, 0x6b60d7ff, 0x6d5ed5ff, 0x6e5cd3ff, 0x705bd1ff, 0x7159cfff, 0x7357cdff, 0x7456cbff, 0x7554c9ff, 0x7652c7ff, 0x7751c5ff, 0x794fc2ff, 0x7a4ec0ff, 0x7b4dbeff, 0x7c4bbbff);
1950preset!(spectral; 0x9e0142ff, 0xd53e4fff, 0xf46d43ff, 0xfdae61ff, 0xfee08bff, 0xffffbfff, 0xe6f598ff, 0xabdda4ff, 0x66c2a5ff, 0x3288bdff, 0x5e4fa2ff);
1951preset!(viridis; 0x440154ff, 0x482777ff, 0x3f4a8aff, 0x31678eff, 0x26838fff, 0x1f9d8aff, 0x6cce5aff, 0xb6de2bff, 0xfee825ff);
1952preset!(inferno; 0x000004ff, 0x170b3aff, 0x420a68ff, 0x6b176eff, 0x932667ff, 0xbb3654ff, 0xdd513aff, 0xf3771aff, 0xfca50aff, 0xf6d644ff, 0xfcffa4ff);
1953preset!(magma; 0x000004ff, 0x140e37ff, 0x3b0f70ff, 0x641a80ff, 0x8c2981ff, 0xb63679ff, 0xde4968ff, 0xf66f5cff, 0xfe9f6dff, 0xfece91ff, 0xfcfdbfff);
1954preset!(plasma; 0x0d0887ff, 0x42039dff, 0x6a00a8ff, 0x900da3ff, 0xb12a90ff, 0xcb4678ff, 0xe16462ff, 0xf1834bff, 0xfca636ff, 0xfccd25ff, 0xf0f921ff);
1955preset!(rocket; 0x3051aff, 0x4051aff, 0x5061bff, 0x6071cff, 0x7071dff, 0x8081eff, 0xa091fff, 0xb0920ff, 0xd0a21ff, 0xe0b22ff, 0x100b23ff, 0x110c24ff, 0x130d25ff, 0x140e26ff, 0x160e27ff, 0x170f28ff, 0x180f29ff, 0x1a102aff, 0x1b112bff, 0x1d112cff, 0x1e122dff, 0x20122eff, 0x211330ff, 0x221331ff, 0x241432ff, 0x251433ff, 0x271534ff, 0x281535ff, 0x2a1636ff, 0x2b1637ff, 0x2d1738ff, 0x2e1739ff, 0x30173aff, 0x31183bff, 0x33183cff, 0x34193dff, 0x35193eff, 0x37193fff, 0x381a40ff, 0x3a1a41ff, 0x3c1a42ff, 0x3d1a42ff, 0x3f1b43ff, 0x401b44ff, 0x421b45ff, 0x431c46ff, 0x451c47ff, 0x461c48ff, 0x481c48ff, 0x491d49ff, 0x4b1d4aff, 0x4c1d4bff, 0x4e1d4bff, 0x501d4cff, 0x511e4dff, 0x531e4dff, 0x541e4eff, 0x561e4fff, 0x581e4fff, 0x591e50ff, 0x5b1e51ff, 0x5c1e51ff, 0x5e1f52ff, 0x601f52ff, 0x611f53ff, 0x631f53ff, 0x641f54ff, 0x661f54ff, 0x681f55ff, 0x691f55ff, 0x6b1f56ff, 0x6d1f56ff, 0x6e1f57ff, 0x701f57ff, 0x711f57ff, 0x731f58ff, 0x751f58ff, 0x761f58ff, 0x781f59ff, 0x7a1f59ff, 0x7b1f59ff, 0x7d1f5aff, 0x7f1e5aff, 0x811e5aff, 0x821e5aff, 0x841e5aff, 0x861e5bff, 0x871e5bff, 0x891e5bff, 0x8b1d5bff, 0x8c1d5bff, 0x8e1d5bff, 0x901d5bff, 0x921c5bff, 0x931c5bff, 0x951c5bff, 0x971c5bff, 0x981b5bff, 0x9a1b5bff, 0x9c1b5bff, 0x9e1a5bff, 0x9f1a5bff, 0xa11a5bff, 0xa3195bff, 0xa4195bff, 0xa6195aff, 0xa8185aff, 0xaa185aff, 0xab185aff, 0xad1759ff, 0xaf1759ff, 0xb01759ff, 0xb21758ff, 0xb41658ff, 0xb51657ff, 0xb71657ff, 0xb91657ff, 0xba1656ff, 0xbc1656ff, 0xbd1655ff, 0xbf1654ff, 0xc11754ff, 0xc21753ff, 0xc41753ff, 0xc51852ff, 0xc71951ff, 0xc81951ff, 0xca1a50ff, 0xcb1b4fff, 0xcd1c4eff, 0xce1d4eff, 0xcf1e4dff, 0xd11f4cff, 0xd2204cff, 0xd3214bff, 0xd5224aff, 0xd62449ff, 0xd72549ff, 0xd82748ff, 0xd92847ff, 0xdb2946ff, 0xdc2b46ff, 0xdd2c45ff, 0xde2e44ff, 0xdf2f44ff, 0xe03143ff, 0xe13342ff, 0xe23442ff, 0xe33641ff, 0xe43841ff, 0xe53940ff, 0xe63b40ff, 0xe73d3fff, 0xe83f3fff, 0xe8403eff, 0xe9423eff, 0xea443eff, 0xeb463eff, 0xeb483eff, 0xec4a3eff, 0xec4c3eff, 0xed4e3eff, 0xed503eff, 0xee523fff, 0xee543fff, 0xef5640ff, 0xef5840ff, 0xef5a41ff, 0xf05c42ff, 0xf05e42ff, 0xf06043ff, 0xf16244ff, 0xf16445ff, 0xf16646ff, 0xf26747ff, 0xf26948ff, 0xf26b49ff, 0xf26d4bff, 0xf26f4cff, 0xf3714dff, 0xf3734eff, 0xf37450ff, 0xf37651ff, 0xf37852ff, 0xf47a54ff, 0xf47c55ff, 0xf47d57ff, 0xf47f58ff, 0xf4815aff, 0xf4835bff, 0xf4845dff, 0xf4865eff, 0xf58860ff, 0xf58a61ff, 0xf58b63ff, 0xf58d64ff, 0xf58f66ff, 0xf59067ff, 0xf59269ff, 0xf5946bff, 0xf5966cff, 0xf5976eff, 0xf59970ff, 0xf69b71ff, 0xf69c73ff, 0xf69e75ff, 0xf6a077ff, 0xf6a178ff, 0xf6a37aff, 0xf6a47cff, 0xf6a67eff, 0xf6a880ff, 0xf6a981ff, 0xf6ab83ff, 0xf6ad85ff, 0xf6ae87ff, 0xf6b089ff, 0xf6b18bff, 0xf6b38dff, 0xf6b48fff, 0xf6b691ff, 0xf6b893ff, 0xf6b995ff, 0xf6bb97ff, 0xf6bc99ff, 0xf6be9bff, 0xf6bf9dff, 0xf6c19fff, 0xf7c2a2ff, 0xf7c4a4ff, 0xf7c6a6ff, 0xf7c7a8ff, 0xf7c9aaff, 0xf7caacff, 0xf7ccafff, 0xf7cdb1ff, 0xf7cfb3ff, 0xf7d0b5ff, 0xf8d1b8ff, 0xf8d3baff, 0xf8d4bcff, 0xf8d6beff, 0xf8d7c0ff, 0xf8d9c3ff, 0xf8dac5ff, 0xf8dcc7ff, 0xf9ddc9ff, 0xf9dfcbff, 0xf9e0cdff, 0xf9e2d0ff, 0xf9e3d2ff, 0xf9e5d4ff, 0xfae6d6ff, 0xfae8d8ff, 0xfae9daff, 0xfaebddff);
1956preset!(mako; 0xb0405ff, 0xd0406ff, 0xe0508ff, 0xf0609ff, 0x10060aff, 0x11070cff, 0x12080dff, 0x13090fff, 0x140910ff, 0x150a12ff, 0x160b13ff, 0x170c15ff, 0x180d16ff, 0x190e18ff, 0x1a0e19ff, 0x1b0f1aff, 0x1c101cff, 0x1d111dff, 0x1e111fff, 0x1f1220ff, 0x201322ff, 0x211423ff, 0x221425ff, 0x231526ff, 0x241628ff, 0x251729ff, 0x26172bff, 0x27182dff, 0x28192eff, 0x291930ff, 0x291a31ff, 0x2a1b33ff, 0x2b1c35ff, 0x2c1c36ff, 0x2d1d38ff, 0x2e1e39ff, 0x2e1e3bff, 0x2f1f3dff, 0x30203eff, 0x312140ff, 0x312142ff, 0x322243ff, 0x332345ff, 0x342447ff, 0x342548ff, 0x35254aff, 0x35264cff, 0x36274dff, 0x37284fff, 0x372851ff, 0x382953ff, 0x382a54ff, 0x392b56ff, 0x3a2c58ff, 0x3a2c59ff, 0x3b2d5bff, 0x3b2e5dff, 0x3b2f5fff, 0x3c3060ff, 0x3c3162ff, 0x3d3164ff, 0x3d3266ff, 0x3e3367ff, 0x3e3469ff, 0x3e356bff, 0x3f366dff, 0x3f366fff, 0x3f3770ff, 0x403872ff, 0x403974ff, 0x403a76ff, 0x403b78ff, 0x403c79ff, 0x413d7bff, 0x413e7dff, 0x413e7fff, 0x413f80ff, 0x414082ff, 0x414184ff, 0x414285ff, 0x414387ff, 0x414488ff, 0x40468aff, 0x40478bff, 0x40488dff, 0x40498eff, 0x3f4a8fff, 0x3f4b90ff, 0x3f4c92ff, 0x3e4d93ff, 0x3e4f94ff, 0x3e5095ff, 0x3d5195ff, 0x3d5296ff, 0x3c5397ff, 0x3c5598ff, 0x3b5698ff, 0x3b5799ff, 0x3b589aff, 0x3a599aff, 0x3a5b9bff, 0x3a5c9bff, 0x395d9cff, 0x395e9cff, 0x385f9cff, 0x38619dff, 0x38629dff, 0x38639dff, 0x37649eff, 0x37659eff, 0x37669eff, 0x37689fff, 0x36699fff, 0x366a9fff, 0x366b9fff, 0x366ca0ff, 0x366da0ff, 0x366fa0ff, 0x3670a0ff, 0x3671a0ff, 0x3572a1ff, 0x3573a1ff, 0x3574a1ff, 0x3575a1ff, 0x3576a2ff, 0x3578a2ff, 0x3579a2ff, 0x357aa2ff, 0x357ba3ff, 0x357ca3ff, 0x357da3ff, 0x357ea4ff, 0x347fa4ff, 0x3480a4ff, 0x3482a4ff, 0x3483a5ff, 0x3484a5ff, 0x3485a5ff, 0x3486a5ff, 0x3487a6ff, 0x3488a6ff, 0x3489a6ff, 0x348ba6ff, 0x348ca7ff, 0x348da7ff, 0x348ea7ff, 0x348fa7ff, 0x3490a8ff, 0x3491a8ff, 0x3492a8ff, 0x3493a8ff, 0x3495a9ff, 0x3496a9ff, 0x3497a9ff, 0x3498a9ff, 0x3499aaff, 0x349aaaff, 0x359baaff, 0x359caaff, 0x359eaaff, 0x359fabff, 0x35a0abff, 0x35a1abff, 0x36a2abff, 0x36a3abff, 0x36a4abff, 0x37a5acff, 0x37a6acff, 0x37a8acff, 0x38a9acff, 0x38aaacff, 0x39abacff, 0x39acacff, 0x3aadacff, 0x3aaeadff, 0x3bafadff, 0x3cb1adff, 0x3cb2adff, 0x3db3adff, 0x3eb4adff, 0x3fb5adff, 0x3fb6adff, 0x40b7adff, 0x41b8adff, 0x42b9adff, 0x43baadff, 0x44bcadff, 0x45bdadff, 0x46beadff, 0x47bfadff, 0x48c0adff, 0x49c1adff, 0x4bc2adff, 0x4cc3adff, 0x4dc4adff, 0x4fc5adff, 0x50c6adff, 0x52c7adff, 0x53c9adff, 0x55caadff, 0x57cbadff, 0x59ccadff, 0x5bcdadff, 0x5ecdadff, 0x60ceacff, 0x62cfacff, 0x65d0adff, 0x68d1adff, 0x6ad2adff, 0x6dd3adff, 0x70d4adff, 0x73d4adff, 0x76d5aeff, 0x79d6aeff, 0x7cd6afff, 0x7fd7afff, 0x82d8b0ff, 0x85d9b1ff, 0x88d9b1ff, 0x8bdab2ff, 0x8edbb3ff, 0x91dbb4ff, 0x94dcb5ff, 0x96ddb5ff, 0x99ddb6ff, 0x9cdeb7ff, 0x9edfb8ff, 0xa1dfb9ff, 0xa4e0bbff, 0xa6e1bcff, 0xa9e1bdff, 0xabe2beff, 0xaee3c0ff, 0xb0e4c1ff, 0xb2e4c2ff, 0xb5e5c4ff, 0xb7e6c5ff, 0xb9e6c7ff, 0xbbe7c8ff, 0xbee8caff, 0xc0e9ccff, 0xc2e9cdff, 0xc4eacfff, 0xc6ebd1ff, 0xc8ecd2ff, 0xcaedd4ff, 0xccedd6ff, 0xceeed7ff, 0xd0efd9ff, 0xd2f0dbff, 0xd4f1dcff, 0xd6f1deff, 0xd8f2e0ff, 0xdaf3e1ff, 0xdcf4e3ff, 0xdef5e5ff);
1957preset!(vlag; 0x2369bdff, 0x266abdff, 0x296cbcff, 0x2c6dbcff, 0x2f6ebcff, 0x316fbcff, 0x3470bcff, 0x3671bcff, 0x3972bcff, 0x3b73bcff, 0x3d74bcff, 0x3f75bcff, 0x4276bcff, 0x4477bcff, 0x4678bcff, 0x4879bcff, 0x4a7bbcff, 0x4c7cbcff, 0x4e7dbcff, 0x507ebcff, 0x517fbcff, 0x5380bcff, 0x5581bcff, 0x5782bcff, 0x5983bdff, 0x5b84bdff, 0x5c85bdff, 0x5e86bdff, 0x6087bdff, 0x6288bdff, 0x6489beff, 0x658abeff, 0x678bbeff, 0x698cbeff, 0x6a8dbfff, 0x6c8ebfff, 0x6e90bfff, 0x6f91bfff, 0x7192c0ff, 0x7393c0ff, 0x7594c0ff, 0x7695c1ff, 0x7896c1ff, 0x7997c1ff, 0x7b98c2ff, 0x7d99c2ff, 0x7e9ac2ff, 0x809bc3ff, 0x829cc3ff, 0x839dc4ff, 0x859ec4ff, 0x87a0c4ff, 0x88a1c5ff, 0x8aa2c5ff, 0x8ba3c6ff, 0x8da4c6ff, 0x8fa5c7ff, 0x90a6c7ff, 0x92a7c8ff, 0x93a8c8ff, 0x95a9c8ff, 0x97abc9ff, 0x98acc9ff, 0x9aadcaff, 0x9baecbff, 0x9dafcbff, 0x9fb0ccff, 0xa0b1ccff, 0xa2b2cdff, 0xa3b4cdff, 0xa5b5ceff, 0xa7b6ceff, 0xa8b7cfff, 0xaab8d0ff, 0xabb9d0ff, 0xadbbd1ff, 0xafbcd1ff, 0xb0bdd2ff, 0xb2bed3ff, 0xb3bfd3ff, 0xb5c0d4ff, 0xb7c2d5ff, 0xb8c3d5ff, 0xbac4d6ff, 0xbbc5d7ff, 0xbdc6d7ff, 0xbfc8d8ff, 0xc0c9d9ff, 0xc2cadaff, 0xc3cbdaff, 0xc5cddbff, 0xc7cedcff, 0xc8cfddff, 0xcad0ddff, 0xcbd1deff, 0xcdd3dfff, 0xcfd4e0ff, 0xd0d5e0ff, 0xd2d7e1ff, 0xd4d8e2ff, 0xd5d9e3ff, 0xd7dae4ff, 0xd9dce5ff, 0xdadde5ff, 0xdcdee6ff, 0xdde0e7ff, 0xdfe1e8ff, 0xe1e2e9ff, 0xe2e3eaff, 0xe4e5ebff, 0xe6e6ecff, 0xe7e7ecff, 0xe9e9edff, 0xebeaeeff, 0xecebefff, 0xeeedf0ff, 0xefeef1ff, 0xf1eff2ff, 0xf2f0f2ff, 0xf3f1f3ff, 0xf5f2f4ff, 0xf6f3f4ff, 0xf7f4f4ff, 0xf8f4f5ff, 0xf9f5f5ff, 0xf9f5f5ff, 0xfaf5f5ff, 0xfaf5f5ff, 0xfaf5f4ff, 0xfaf5f4ff, 0xfaf4f3ff, 0xfaf3f3ff, 0xfaf3f2ff, 0xfaf2f1ff, 0xfaf0efff, 0xf9efeeff, 0xf9eeedff, 0xf8edebff, 0xf7ebeaff, 0xf7eae8ff, 0xf6e8e7ff, 0xf5e7e5ff, 0xf5e5e4ff, 0xf4e3e2ff, 0xf3e2e0ff, 0xf2e0dfff, 0xf2dfddff, 0xf1dddbff, 0xf0dbdaff, 0xefdad8ff, 0xefd8d6ff, 0xeed7d5ff, 0xedd5d3ff, 0xecd3d2ff, 0xecd2d0ff, 0xebd0ceff, 0xeacfcdff, 0xeacdcbff, 0xe9cbc9ff, 0xe8cac8ff, 0xe7c8c6ff, 0xe7c7c5ff, 0xe6c5c3ff, 0xe5c3c1ff, 0xe5c2c0ff, 0xe4c0beff, 0xe3bfbdff, 0xe3bdbbff, 0xe2bcb9ff, 0xe1bab8ff, 0xe1b9b6ff, 0xe0b7b5ff, 0xdfb5b3ff, 0xdfb4b2ff, 0xdeb2b0ff, 0xdeb1aeff, 0xddafadff, 0xdcaeabff, 0xdcacaaff, 0xdbaba8ff, 0xdaa9a7ff, 0xdaa8a5ff, 0xd9a6a4ff, 0xd9a5a2ff, 0xd8a3a0ff, 0xd7a29fff, 0xd7a09dff, 0xd69f9cff, 0xd59d9aff, 0xd59c99ff, 0xd49a97ff, 0xd49896ff, 0xd39794ff, 0xd29593ff, 0xd29491ff, 0xd19290ff, 0xd1918eff, 0xd08f8dff, 0xcf8e8bff, 0xcf8c8aff, 0xce8b88ff, 0xcd8987ff, 0xcd8885ff, 0xcc8784ff, 0xcc8582ff, 0xcb8481ff, 0xca827fff, 0xca817eff, 0xc97f7dff, 0xc87e7bff, 0xc87c7aff, 0xc77b78ff, 0xc77977ff, 0xc67875ff, 0xc57674ff, 0xc57572ff, 0xc47371ff, 0xc3726fff, 0xc3706eff, 0xc26f6dff, 0xc16d6bff, 0xc16c6aff, 0xc06a68ff, 0xc06967ff, 0xbf6765ff, 0xbe6664ff, 0xbe6463ff, 0xbd6361ff, 0xbc6160ff, 0xbc605eff, 0xbb5e5dff, 0xba5d5cff, 0xb95b5aff, 0xb95a59ff, 0xb85857ff, 0xb75756ff, 0xb75555ff, 0xb65453ff, 0xb55252ff, 0xb55151ff, 0xb44f4fff, 0xb34d4eff, 0xb24c4cff, 0xb24a4bff, 0xb1494aff, 0xb04748ff, 0xaf4647ff, 0xaf4446ff, 0xae4244ff, 0xad4143ff, 0xac3f42ff, 0xac3e40ff, 0xab3c3fff, 0xaa3a3eff, 0xa9393cff, 0xa9373bff);
1958preset!(icefire; 0xbde7dbff, 0xbae5daff, 0xb7e3d9ff, 0xb4e1d9ff, 0xb2dfd8ff, 0xafddd7ff, 0xacdbd7ff, 0xa9d9d6ff, 0xa7d7d5ff, 0xa4d5d5ff, 0xa1d3d4ff, 0x9ed1d3ff, 0x9bcfd3ff, 0x98cdd2ff, 0x95cbd2ff, 0x93cad1ff, 0x90c8d1ff, 0x8dc6d0ff, 0x8ac4d0ff, 0x87c2cfff, 0x84c1cfff, 0x81bfcfff, 0x7ebdceff, 0x7bbbceff, 0x78b9ceff, 0x75b8ceff, 0x72b6ceff, 0x6eb4cdff, 0x6bb2cdff, 0x68b0cdff, 0x65afcdff, 0x63adcdff, 0x60abcdff, 0x5da9cdff, 0x5aa7cdff, 0x58a5cdff, 0x55a3cdff, 0x53a2cdff, 0x50a0cdff, 0x4e9ecdff, 0x4c9ccdff, 0x499aceff, 0x4798ceff, 0x4596ceff, 0x4394ceff, 0x4192ceff, 0x3f90ceff, 0x3e8ecfff, 0x3c8ccfff, 0x3a89cfff, 0x3987cfff, 0x3885d0ff, 0x3783d0ff, 0x3781d0ff, 0x377fd0ff, 0x377cd0ff, 0x377ad0ff, 0x3878cfff, 0x3975cfff, 0x3a73ceff, 0x3b71cdff, 0x3d6eccff, 0x3e6ccbff, 0x3f69c9ff, 0x4167c7ff, 0x4265c5ff, 0x4363c3ff, 0x4560c1ff, 0x465ebeff, 0x475cbcff, 0x475ab9ff, 0x4858b6ff, 0x4956b3ff, 0x4954b0ff, 0x4952adff, 0x4a50a9ff, 0x4a4fa5ff, 0x494da1ff, 0x494c9eff, 0x494a9aff, 0x484996ff, 0x474792ff, 0x47468eff, 0x46458aff, 0x454386ff, 0x444282ff, 0x43417fff, 0x42407bff, 0x413e77ff, 0x3f3d74ff, 0x3e3c70ff, 0x3d3b6dff, 0x3c3a69ff, 0x3b3866ff, 0x393763ff, 0x38365fff, 0x37355cff, 0x363459ff, 0x343356ff, 0x333153ff, 0x323050ff, 0x312f4dff, 0x302e4aff, 0x2e2d48ff, 0x2d2c45ff, 0x2c2b42ff, 0x2b2a40ff, 0x2a293dff, 0x29283bff, 0x282739ff, 0x272636ff, 0x262534ff, 0x252532ff, 0x242430ff, 0x24232eff, 0x23222dff, 0x22222bff, 0x222129ff, 0x212028ff, 0x212026ff, 0x202025ff, 0x201f24ff, 0x1f1f23ff, 0x1f1f21ff, 0x1f1e21ff, 0x1f1e20ff, 0x1f1e1fff, 0x1f1e1eff, 0x1f1e1eff, 0x201e1eff, 0x211e1eff, 0x221e1eff, 0x231e1eff, 0x251e1fff, 0x261e1fff, 0x271e1fff, 0x291e20ff, 0x2a1e20ff, 0x2c1e21ff, 0x2d1f21ff, 0x2f1f22ff, 0x311f23ff, 0x332023ff, 0x352024ff, 0x372025ff, 0x392126ff, 0x3b2127ff, 0x3d2228ff, 0x3f2228ff, 0x412329ff, 0x43232aff, 0x46242bff, 0x48242cff, 0x4a252eff, 0x4d252fff, 0x4f2630ff, 0x522731ff, 0x542732ff, 0x572833ff, 0x5a2834ff, 0x5c2935ff, 0x5f2936ff, 0x622937ff, 0x642a38ff, 0x672a39ff, 0x6a2b3aff, 0x6d2b3bff, 0x702b3cff, 0x722c3dff, 0x752c3eff, 0x782c3fff, 0x7b2d40ff, 0x7e2d40ff, 0x812d41ff, 0x842d42ff, 0x872d42ff, 0x8a2e43ff, 0x8d2e43ff, 0x902e44ff, 0x932e44ff, 0x962e44ff, 0x992e44ff, 0x9c2f45ff, 0x9f2f44ff, 0xa22f44ff, 0xa52f44ff, 0xa83044ff, 0xab3043ff, 0xae3143ff, 0xb13242ff, 0xb33341ff, 0xb63441ff, 0xb93540ff, 0xbb363fff, 0xbe373eff, 0xc0393dff, 0xc33a3cff, 0xc53c3cff, 0xc73d3bff, 0xc93f3aff, 0xcc4139ff, 0xce4338ff, 0xd04537ff, 0xd24737ff, 0xd34936ff, 0xd54b35ff, 0xd74e35ff, 0xd95034ff, 0xda5334ff, 0xdc5534ff, 0xde5733ff, 0xdf5a33ff, 0xe15c33ff, 0xe25f33ff, 0xe36233ff, 0xe56433ff, 0xe66734ff, 0xe76a34ff, 0xe86d35ff, 0xe96f36ff, 0xea7238ff, 0xeb753aff, 0xec783bff, 0xed7b3eff, 0xed7e40ff, 0xee8142ff, 0xef8445ff, 0xef8748ff, 0xf0894bff, 0xf18c4eff, 0xf18f51ff, 0xf29255ff, 0xf29558ff, 0xf3985bff, 0xf39a5fff, 0xf49d63ff, 0xf5a066ff, 0xf5a36aff, 0xf6a56dff, 0xf6a871ff, 0xf7ab75ff, 0xf7ae79ff, 0xf8b07cff, 0xf8b380ff, 0xf9b684ff, 0xfab887ff, 0xfabb8bff, 0xfbbe8fff, 0xfbc192ff, 0xfcc396ff, 0xfcc69aff, 0xfdc99eff, 0xfdcca1ff, 0xfecea5ff, 0xfed1a9ff, 0xffd4acff);
1959preset!(flare; 0xedb081ff, 0xedaf80ff, 0xedae7fff, 0xedad7fff, 0xedac7eff, 0xedab7eff, 0xecaa7dff, 0xeca97cff, 0xeca87cff, 0xeca77bff, 0xeca67bff, 0xeca57aff, 0xeca479ff, 0xeca379ff, 0xeca278ff, 0xeca178ff, 0xeca077ff, 0xec9f76ff, 0xeb9e76ff, 0xeb9d75ff, 0xeb9c75ff, 0xeb9b74ff, 0xeb9a73ff, 0xeb9973ff, 0xeb9972ff, 0xeb9872ff, 0xeb9771ff, 0xea9671ff, 0xea9570ff, 0xea946fff, 0xea936fff, 0xea926eff, 0xea916eff, 0xea906dff, 0xea8f6cff, 0xea8e6cff, 0xe98d6bff, 0xe98c6bff, 0xe98b6aff, 0xe98a6aff, 0xe98969ff, 0xe98868ff, 0xe98768ff, 0xe98667ff, 0xe88567ff, 0xe88466ff, 0xe88366ff, 0xe88265ff, 0xe88165ff, 0xe88064ff, 0xe87f64ff, 0xe77e63ff, 0xe77d63ff, 0xe77c63ff, 0xe77b62ff, 0xe77a62ff, 0xe67961ff, 0xe67861ff, 0xe67760ff, 0xe67660ff, 0xe67560ff, 0xe5745fff, 0xe5735fff, 0xe5725fff, 0xe5715eff, 0xe5705eff, 0xe46f5eff, 0xe46e5eff, 0xe46d5dff, 0xe46c5dff, 0xe36b5dff, 0xe36a5dff, 0xe3695dff, 0xe3685cff, 0xe2675cff, 0xe2665cff, 0xe2655cff, 0xe1645cff, 0xe1635cff, 0xe1625cff, 0xe0615cff, 0xe0605cff, 0xe05f5cff, 0xdf5f5cff, 0xdf5e5cff, 0xde5d5cff, 0xde5c5cff, 0xde5b5cff, 0xdd5a5cff, 0xdd595cff, 0xdc585cff, 0xdc575cff, 0xdb565dff, 0xdb565dff, 0xda555dff, 0xda545dff, 0xd9535dff, 0xd9525eff, 0xd8525eff, 0xd7515eff, 0xd7505eff, 0xd64f5fff, 0xd64f5fff, 0xd54e5fff, 0xd44d60ff, 0xd44c60ff, 0xd34c60ff, 0xd24b60ff, 0xd24a61ff, 0xd14a61ff, 0xd04962ff, 0xd04962ff, 0xcf4862ff, 0xce4763ff, 0xcd4763ff, 0xcc4663ff, 0xcc4664ff, 0xcb4564ff, 0xca4564ff, 0xc94465ff, 0xc84465ff, 0xc84365ff, 0xc74366ff, 0xc64366ff, 0xc54266ff, 0xc44267ff, 0xc34167ff, 0xc24167ff, 0xc14168ff, 0xc14068ff, 0xc04068ff, 0xbf4069ff, 0xbe3f69ff, 0xbd3f69ff, 0xbc3f69ff, 0xbb3f6aff, 0xba3e6aff, 0xb93e6aff, 0xb83e6bff, 0xb73d6bff, 0xb63d6bff, 0xb53d6bff, 0xb43d6bff, 0xb33c6cff, 0xb23c6cff, 0xb13c6cff, 0xb13c6cff, 0xb03b6dff, 0xaf3b6dff, 0xae3b6dff, 0xad3b6dff, 0xac3a6dff, 0xab3a6dff, 0xaa3a6eff, 0xa93a6eff, 0xa8396eff, 0xa7396eff, 0xa6396eff, 0xa5396eff, 0xa4386fff, 0xa3386fff, 0xa2386fff, 0xa1386fff, 0xa1376fff, 0xa0376fff, 0x9f376fff, 0x9e3770ff, 0x9d3670ff, 0x9c3670ff, 0x9b3670ff, 0x9a3670ff, 0x993570ff, 0x983570ff, 0x973570ff, 0x963570ff, 0x953470ff, 0x943470ff, 0x943471ff, 0x933471ff, 0x923371ff, 0x913371ff, 0x903371ff, 0x8f3371ff, 0x8e3271ff, 0x8d3271ff, 0x8c3271ff, 0x8b3271ff, 0x8a3171ff, 0x893171ff, 0x883171ff, 0x873171ff, 0x873171ff, 0x863071ff, 0x853071ff, 0x843071ff, 0x833070ff, 0x822f70ff, 0x812f70ff, 0x802f70ff, 0x7f2f70ff, 0x7e2f70ff, 0x7d2e70ff, 0x7c2e70ff, 0x7b2e70ff, 0x7a2e70ff, 0x792e6fff, 0x782e6fff, 0x772d6fff, 0x762d6fff, 0x752d6fff, 0x752d6fff, 0x742d6eff, 0x732c6eff, 0x722c6eff, 0x712c6eff, 0x702c6eff, 0x6f2c6dff, 0x6e2c6dff, 0x6d2b6dff, 0x6c2b6dff, 0x6b2b6cff, 0x6a2b6cff, 0x692b6cff, 0x682a6cff, 0x672a6bff, 0x662a6bff, 0x652a6bff, 0x642a6aff, 0x642a6aff, 0x63296aff, 0x62296aff, 0x612969ff, 0x602969ff, 0x5f2969ff, 0x5e2868ff, 0x5d2868ff, 0x5c2868ff, 0x5b2867ff, 0x5a2767ff, 0x592767ff, 0x582766ff, 0x582766ff, 0x572766ff, 0x562666ff, 0x552665ff, 0x542665ff, 0x532665ff, 0x522564ff, 0x512564ff, 0x502564ff, 0x4f2463ff, 0x4f2463ff, 0x4e2463ff, 0x4d2463ff, 0x4c2362ff, 0x4b2362ff);
1960preset!(crest; 0xa5cd90ff, 0xa4cc90ff, 0xa3cc91ff, 0xa2cb91ff, 0xa0cb91ff, 0x9fca91ff, 0x9eca91ff, 0x9dc991ff, 0x9cc891ff, 0x9bc891ff, 0x9ac791ff, 0x99c791ff, 0x98c691ff, 0x96c691ff, 0x95c591ff, 0x94c591ff, 0x93c491ff, 0x92c491ff, 0x91c391ff, 0x90c391ff, 0x8fc291ff, 0x8ec291ff, 0x8dc191ff, 0x8bc191ff, 0x8ac091ff, 0x89bf91ff, 0x88bf91ff, 0x87be91ff, 0x86be91ff, 0x85bd91ff, 0x84bd91ff, 0x82bc91ff, 0x81bc91ff, 0x80bb91ff, 0x7fbb91ff, 0x7eba91ff, 0x7dba91ff, 0x7cb991ff, 0x7bb991ff, 0x79b891ff, 0x78b891ff, 0x77b791ff, 0x76b791ff, 0x75b690ff, 0x74b690ff, 0x73b590ff, 0x72b490ff, 0x71b490ff, 0x70b390ff, 0x6fb390ff, 0x6eb290ff, 0x6db290ff, 0x6cb190ff, 0x6bb190ff, 0x6ab090ff, 0x69b090ff, 0x68af90ff, 0x67ae90ff, 0x66ae90ff, 0x65ad90ff, 0x64ad90ff, 0x63ac90ff, 0x62ac90ff, 0x62ab90ff, 0x61aa90ff, 0x60aa90ff, 0x5fa990ff, 0x5ea990ff, 0x5da890ff, 0x5ca890ff, 0x5ba790ff, 0x5ba690ff, 0x5aa690ff, 0x59a590ff, 0x58a590ff, 0x57a490ff, 0x57a490ff, 0x56a390ff, 0x55a290ff, 0x54a290ff, 0x53a190ff, 0x53a190ff, 0x52a090ff, 0x519f90ff, 0x509f90ff, 0x509e90ff, 0x4f9e90ff, 0x4e9d90ff, 0x4e9d90ff, 0x4d9c90ff, 0x4c9b90ff, 0x4b9b90ff, 0x4b9a8fff, 0x4a9a8fff, 0x49998fff, 0x49988fff, 0x48988fff, 0x47978fff, 0x47978fff, 0x46968fff, 0x45958fff, 0x45958fff, 0x44948fff, 0x43948fff, 0x43938fff, 0x42928fff, 0x41928fff, 0x41918fff, 0x40918fff, 0x40908eff, 0x3f8f8eff, 0x3e8f8eff, 0x3e8e8eff, 0x3d8e8eff, 0x3c8d8eff, 0x3c8c8eff, 0x3b8c8eff, 0x3a8b8eff, 0x3a8b8eff, 0x398a8eff, 0x388a8eff, 0x38898eff, 0x37888eff, 0x37888dff, 0x36878dff, 0x35878dff, 0x35868dff, 0x34858dff, 0x33858dff, 0x33848dff, 0x32848dff, 0x31838dff, 0x31828dff, 0x30828dff, 0x2f818dff, 0x2f818dff, 0x2e808dff, 0x2d808cff, 0x2d7f8cff, 0x2c7e8cff, 0x2c7e8cff, 0x2b7d8cff, 0x2a7d8cff, 0x2a7c8cff, 0x297b8cff, 0x287b8cff, 0x287a8cff, 0x277a8cff, 0x27798cff, 0x26788cff, 0x25788cff, 0x25778cff, 0x24778bff, 0x24768bff, 0x23758bff, 0x23758bff, 0x22748bff, 0x22748bff, 0x21738bff, 0x21728bff, 0x20728bff, 0x20718bff, 0x20718bff, 0x1f708bff, 0x1f6f8aff, 0x1e6f8aff, 0x1e6e8aff, 0x1e6d8aff, 0x1e6d8aff, 0x1d6c8aff, 0x1d6c8aff, 0x1d6b8aff, 0x1d6a8aff, 0x1d6a8aff, 0x1c6989ff, 0x1c6889ff, 0x1c6889ff, 0x1c6789ff, 0x1c6689ff, 0x1c6689ff, 0x1c6589ff, 0x1c6488ff, 0x1c6488ff, 0x1c6388ff, 0x1d6388ff, 0x1d6288ff, 0x1d6188ff, 0x1d6187ff, 0x1d6087ff, 0x1d5f87ff, 0x1d5f87ff, 0x1e5e87ff, 0x1e5d86ff, 0x1e5d86ff, 0x1e5c86ff, 0x1e5b86ff, 0x1f5b86ff, 0x1f5a85ff, 0x1f5985ff, 0x1f5985ff, 0x205885ff, 0x205784ff, 0x205784ff, 0x205684ff, 0x215584ff, 0x215583ff, 0x215483ff, 0x225383ff, 0x225283ff, 0x225282ff, 0x225182ff, 0x235082ff, 0x235081ff, 0x234f81ff, 0x244e81ff, 0x244e80ff, 0x244d80ff, 0x254c80ff, 0x254c7fff, 0x254b7fff, 0x254a7fff, 0x26497eff, 0x26497eff, 0x26487eff, 0x27477dff, 0x27477dff, 0x27467cff, 0x27457cff, 0x28457cff, 0x28447bff, 0x28437bff, 0x28427aff, 0x29427aff, 0x29417aff, 0x294079ff, 0x294079ff, 0x2a3f78ff, 0x2a3e78ff, 0x2a3d78ff, 0x2a3d77ff, 0x2a3c77ff, 0x2a3b76ff, 0x2b3b76ff, 0x2b3a76ff, 0x2b3975ff, 0x2b3875ff, 0x2b3875ff, 0x2b3774ff, 0x2b3674ff, 0x2c3574ff, 0x2c3573ff, 0x2c3473ff, 0x2c3373ff, 0x2c3272ff, 0x2c3172ff, 0x2c3172ff);
1961
1962#[cfg(test)]
1963mod tests {
1964    use super::*;
1965
1966    #[test]
1967    fn test_parse_color_strings() {
1968        #[track_caller]
1969        fn test(hex: &str, r: u8, g: u8, b: u8, a: u8) {
1970            assert_eq!(Color::from_str(hex), Ok(Color::from_u8(r, g, b, a)));
1971        }
1972
1973        test("f61243ff", 0xf6, 0x12, 0x43, 255);
1974        test("b3d8b3", 0xb3, 0xd8, 0xb3, 255);
1975        test("fCd2a9AD", 0xfc, 0xd2, 0xa9, 0xad);
1976        test("233", 0x22, 0x33, 0x33, 255);
1977        test("111b", 0x11, 0x11, 0x11, 0xbb);
1978    }
1979
1980    #[test]
1981    fn test_parse_invalid_colors() {
1982        #[track_caller]
1983        fn test(hex: &str, message: &str) {
1984            assert_eq!(Color::from_str(hex), Err(message));
1985        }
1986
1987        test("a5", "color string has wrong length");
1988        test("12345", "color string has wrong length");
1989        test("f075ff011", "color string has wrong length");
1990        test("hmmm", "color string contains non-hexadecimal letters");
1991        test("14B2AH", "color string contains non-hexadecimal letters");
1992    }
1993}