cssparser/
color.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! General color-parsing utilities, independent on the specific color storage and parsing
6//! implementation.
7//!
8//! For a more complete css-color implementation take a look at cssparser-color crate, or at
9//! Gecko's color module.
10
11// Allow text like <color> in docs.
12#![allow(rustdoc::invalid_html_tags)]
13
14/// The opaque alpha value of 1.0.
15pub const OPAQUE: f32 = 1.0;
16
17use crate::{BasicParseError, Parser, ToCss, Token};
18use std::fmt;
19
20/// Clamp a 0..1 number to a 0..255 range to u8.
21///
22/// Whilst scaling by 256 and flooring would provide
23/// an equal distribution of integers to percentage inputs,
24/// this is not what Gecko does so we instead multiply by 255
25/// and round (adding 0.5 and flooring is equivalent to rounding)
26///
27/// Chrome does something similar for the alpha value, but not
28/// the rgb values.
29///
30/// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1340484>
31///
32/// Clamping to 256 and rounding after would let 1.0 map to 256, and
33/// `256.0_f32 as u8` is undefined behavior:
34///
35/// <https://github.com/rust-lang/rust/issues/10184>
36#[inline]
37pub fn clamp_unit_f32(val: f32) -> u8 {
38    clamp_floor_256_f32(val * 255.)
39}
40
41/// Round and clamp a single number to a u8.
42#[inline]
43pub fn clamp_floor_256_f32(val: f32) -> u8 {
44    val.round().clamp(0., 255.) as u8
45}
46
47/// Serialize the alpha copmonent of a color according to the specification.
48/// <https://drafts.csswg.org/css-color-4/#serializing-alpha-values>
49#[inline]
50pub fn serialize_color_alpha(
51    dest: &mut impl fmt::Write,
52    alpha: Option<f32>,
53    legacy_syntax: bool,
54) -> fmt::Result {
55    let alpha = match alpha {
56        None => return dest.write_str(" / none"),
57        Some(a) => a,
58    };
59
60    // If the alpha component is full opaque, don't emit the alpha value in CSS.
61    if alpha == OPAQUE {
62        return Ok(());
63    }
64
65    dest.write_str(if legacy_syntax { ", " } else { " / " })?;
66
67    // Try first with two decimal places, then with three.
68    let mut rounded_alpha = (alpha * 100.).round() / 100.;
69    if clamp_unit_f32(rounded_alpha) != clamp_unit_f32(alpha) {
70        rounded_alpha = (alpha * 1000.).round() / 1000.;
71    }
72
73    rounded_alpha.to_css(dest)
74}
75
76/// A Predefined color space specified in:
77/// <https://drafts.csswg.org/css-color-4/#predefined>
78#[derive(Clone, Copy, Eq, PartialEq, Debug)]
79#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
80#[cfg_attr(feature = "serde", serde(tag = "type"))]
81pub enum PredefinedColorSpace {
82    /// <https://drafts.csswg.org/css-color-4/#predefined-sRGB>
83    Srgb,
84    /// <https://drafts.csswg.org/css-color-4/#predefined-sRGB-linear>
85    SrgbLinear,
86    /// <https://drafts.csswg.org/css-color-4/#predefined-display-p3>
87    DisplayP3,
88    /// <https://drafts.csswg.org/css-color-4/#predefined-a98-rgb>
89    A98Rgb,
90    /// <https://drafts.csswg.org/css-color-4/#predefined-prophoto-rgb>
91    ProphotoRgb,
92    /// <https://drafts.csswg.org/css-color-4/#predefined-rec2020>
93    Rec2020,
94    /// <https://drafts.csswg.org/css-color-4/#predefined-xyz>
95    XyzD50,
96    /// <https://drafts.csswg.org/css-color-4/#predefined-xyz>
97    XyzD65,
98}
99
100impl PredefinedColorSpace {
101    /// Parse a PredefinedColorSpace from the given input.
102    pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Self, BasicParseError<'i>> {
103        let location = input.current_source_location();
104
105        let ident = input.expect_ident()?;
106        Ok(match_ignore_ascii_case! { ident,
107            "srgb" => Self::Srgb,
108            "srgb-linear" => Self::SrgbLinear,
109            "display-p3" => Self::DisplayP3,
110            "a98-rgb" => Self::A98Rgb,
111            "prophoto-rgb" => Self::ProphotoRgb,
112            "rec2020" => Self::Rec2020,
113            "xyz-d50" => Self::XyzD50,
114            "xyz" | "xyz-d65" => Self::XyzD65,
115            _ => return Err(location.new_basic_unexpected_token_error(Token::Ident(ident.clone()))),
116        })
117    }
118}
119
120impl ToCss for PredefinedColorSpace {
121    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
122    where
123        W: fmt::Write,
124    {
125        dest.write_str(match self {
126            Self::Srgb => "srgb",
127            Self::SrgbLinear => "srgb-linear",
128            Self::DisplayP3 => "display-p3",
129            Self::A98Rgb => "a98-rgb",
130            Self::ProphotoRgb => "prophoto-rgb",
131            Self::Rec2020 => "rec2020",
132            Self::XyzD50 => "xyz-d50",
133            Self::XyzD65 => "xyz-d65",
134        })
135    }
136}
137
138/// Parse a color hash, without the leading '#' character.
139#[allow(clippy::result_unit_err)]
140#[inline]
141pub fn parse_hash_color(value: &[u8]) -> Result<(u8, u8, u8, f32), ()> {
142    Ok(match value.len() {
143        8 => (
144            from_hex(value[0])? * 16 + from_hex(value[1])?,
145            from_hex(value[2])? * 16 + from_hex(value[3])?,
146            from_hex(value[4])? * 16 + from_hex(value[5])?,
147            (from_hex(value[6])? * 16 + from_hex(value[7])?) as f32 / 255.0,
148        ),
149        6 => (
150            from_hex(value[0])? * 16 + from_hex(value[1])?,
151            from_hex(value[2])? * 16 + from_hex(value[3])?,
152            from_hex(value[4])? * 16 + from_hex(value[5])?,
153            OPAQUE,
154        ),
155        4 => (
156            from_hex(value[0])? * 17,
157            from_hex(value[1])? * 17,
158            from_hex(value[2])? * 17,
159            (from_hex(value[3])? * 17) as f32 / 255.0,
160        ),
161        3 => (
162            from_hex(value[0])? * 17,
163            from_hex(value[1])? * 17,
164            from_hex(value[2])? * 17,
165            OPAQUE,
166        ),
167        _ => return Err(()),
168    })
169}
170
171ascii_case_insensitive_phf_map! {
172    named_colors -> (u8, u8, u8) = {
173        "black" => (0, 0, 0),
174        "silver" => (192, 192, 192),
175        "gray" => (128, 128, 128),
176        "white" => (255, 255, 255),
177        "maroon" => (128, 0, 0),
178        "red" => (255, 0, 0),
179        "purple" => (128, 0, 128),
180        "fuchsia" => (255, 0, 255),
181        "green" => (0, 128, 0),
182        "lime" => (0, 255, 0),
183        "olive" => (128, 128, 0),
184        "yellow" => (255, 255, 0),
185        "navy" => (0, 0, 128),
186        "blue" => (0, 0, 255),
187        "teal" => (0, 128, 128),
188        "aqua" => (0, 255, 255),
189
190        "aliceblue" => (240, 248, 255),
191        "antiquewhite" => (250, 235, 215),
192        "aquamarine" => (127, 255, 212),
193        "azure" => (240, 255, 255),
194        "beige" => (245, 245, 220),
195        "bisque" => (255, 228, 196),
196        "blanchedalmond" => (255, 235, 205),
197        "blueviolet" => (138, 43, 226),
198        "brown" => (165, 42, 42),
199        "burlywood" => (222, 184, 135),
200        "cadetblue" => (95, 158, 160),
201        "chartreuse" => (127, 255, 0),
202        "chocolate" => (210, 105, 30),
203        "coral" => (255, 127, 80),
204        "cornflowerblue" => (100, 149, 237),
205        "cornsilk" => (255, 248, 220),
206        "crimson" => (220, 20, 60),
207        "cyan" => (0, 255, 255),
208        "darkblue" => (0, 0, 139),
209        "darkcyan" => (0, 139, 139),
210        "darkgoldenrod" => (184, 134, 11),
211        "darkgray" => (169, 169, 169),
212        "darkgreen" => (0, 100, 0),
213        "darkgrey" => (169, 169, 169),
214        "darkkhaki" => (189, 183, 107),
215        "darkmagenta" => (139, 0, 139),
216        "darkolivegreen" => (85, 107, 47),
217        "darkorange" => (255, 140, 0),
218        "darkorchid" => (153, 50, 204),
219        "darkred" => (139, 0, 0),
220        "darksalmon" => (233, 150, 122),
221        "darkseagreen" => (143, 188, 143),
222        "darkslateblue" => (72, 61, 139),
223        "darkslategray" => (47, 79, 79),
224        "darkslategrey" => (47, 79, 79),
225        "darkturquoise" => (0, 206, 209),
226        "darkviolet" => (148, 0, 211),
227        "deeppink" => (255, 20, 147),
228        "deepskyblue" => (0, 191, 255),
229        "dimgray" => (105, 105, 105),
230        "dimgrey" => (105, 105, 105),
231        "dodgerblue" => (30, 144, 255),
232        "firebrick" => (178, 34, 34),
233        "floralwhite" => (255, 250, 240),
234        "forestgreen" => (34, 139, 34),
235        "gainsboro" => (220, 220, 220),
236        "ghostwhite" => (248, 248, 255),
237        "gold" => (255, 215, 0),
238        "goldenrod" => (218, 165, 32),
239        "greenyellow" => (173, 255, 47),
240        "grey" => (128, 128, 128),
241        "honeydew" => (240, 255, 240),
242        "hotpink" => (255, 105, 180),
243        "indianred" => (205, 92, 92),
244        "indigo" => (75, 0, 130),
245        "ivory" => (255, 255, 240),
246        "khaki" => (240, 230, 140),
247        "lavender" => (230, 230, 250),
248        "lavenderblush" => (255, 240, 245),
249        "lawngreen" => (124, 252, 0),
250        "lemonchiffon" => (255, 250, 205),
251        "lightblue" => (173, 216, 230),
252        "lightcoral" => (240, 128, 128),
253        "lightcyan" => (224, 255, 255),
254        "lightgoldenrodyellow" => (250, 250, 210),
255        "lightgray" => (211, 211, 211),
256        "lightgreen" => (144, 238, 144),
257        "lightgrey" => (211, 211, 211),
258        "lightpink" => (255, 182, 193),
259        "lightsalmon" => (255, 160, 122),
260        "lightseagreen" => (32, 178, 170),
261        "lightskyblue" => (135, 206, 250),
262        "lightslategray" => (119, 136, 153),
263        "lightslategrey" => (119, 136, 153),
264        "lightsteelblue" => (176, 196, 222),
265        "lightyellow" => (255, 255, 224),
266        "limegreen" => (50, 205, 50),
267        "linen" => (250, 240, 230),
268        "magenta" => (255, 0, 255),
269        "mediumaquamarine" => (102, 205, 170),
270        "mediumblue" => (0, 0, 205),
271        "mediumorchid" => (186, 85, 211),
272        "mediumpurple" => (147, 112, 219),
273        "mediumseagreen" => (60, 179, 113),
274        "mediumslateblue" => (123, 104, 238),
275        "mediumspringgreen" => (0, 250, 154),
276        "mediumturquoise" => (72, 209, 204),
277        "mediumvioletred" => (199, 21, 133),
278        "midnightblue" => (25, 25, 112),
279        "mintcream" => (245, 255, 250),
280        "mistyrose" => (255, 228, 225),
281        "moccasin" => (255, 228, 181),
282        "navajowhite" => (255, 222, 173),
283        "oldlace" => (253, 245, 230),
284        "olivedrab" => (107, 142, 35),
285        "orange" => (255, 165, 0),
286        "orangered" => (255, 69, 0),
287        "orchid" => (218, 112, 214),
288        "palegoldenrod" => (238, 232, 170),
289        "palegreen" => (152, 251, 152),
290        "paleturquoise" => (175, 238, 238),
291        "palevioletred" => (219, 112, 147),
292        "papayawhip" => (255, 239, 213),
293        "peachpuff" => (255, 218, 185),
294        "peru" => (205, 133, 63),
295        "pink" => (255, 192, 203),
296        "plum" => (221, 160, 221),
297        "powderblue" => (176, 224, 230),
298        "rebeccapurple" => (102, 51, 153),
299        "rosybrown" => (188, 143, 143),
300        "royalblue" => (65, 105, 225),
301        "saddlebrown" => (139, 69, 19),
302        "salmon" => (250, 128, 114),
303        "sandybrown" => (244, 164, 96),
304        "seagreen" => (46, 139, 87),
305        "seashell" => (255, 245, 238),
306        "sienna" => (160, 82, 45),
307        "skyblue" => (135, 206, 235),
308        "slateblue" => (106, 90, 205),
309        "slategray" => (112, 128, 144),
310        "slategrey" => (112, 128, 144),
311        "snow" => (255, 250, 250),
312        "springgreen" => (0, 255, 127),
313        "steelblue" => (70, 130, 180),
314        "tan" => (210, 180, 140),
315        "thistle" => (216, 191, 216),
316        "tomato" => (255, 99, 71),
317        "turquoise" => (64, 224, 208),
318        "violet" => (238, 130, 238),
319        "wheat" => (245, 222, 179),
320        "whitesmoke" => (245, 245, 245),
321        "yellowgreen" => (154, 205, 50),
322    }
323}
324
325/// Returns the named color with the given name.
326/// <https://drafts.csswg.org/css-color-4/#typedef-named-color>
327#[allow(clippy::result_unit_err)]
328#[inline]
329pub fn parse_named_color(ident: &str) -> Result<(u8, u8, u8), ()> {
330    named_colors::get(ident).copied().ok_or(())
331}
332
333/// Returns an iterator over all named CSS colors.
334/// <https://drafts.csswg.org/css-color-4/#typedef-named-color>
335#[inline]
336pub fn all_named_colors() -> impl Iterator<Item = (&'static str, (u8, u8, u8))> {
337    named_colors::entries().map(|(k, v)| (*k, *v))
338}
339
340#[inline]
341fn from_hex(c: u8) -> Result<u8, ()> {
342    match c {
343        b'0'..=b'9' => Ok(c - b'0'),
344        b'a'..=b'f' => Ok(c - b'a' + 10),
345        b'A'..=b'F' => Ok(c - b'A' + 10),
346        _ => Err(()),
347    }
348}