/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! General color-parsing utilities, independent on the specific color storage and parsing //! implementation. //! //! For a more complete css-color implementation take a look at cssparser-color crate, or at //! Gecko's color module.
// Allow text like <color> in docs. #![allow(rustdoc::invalid_html_tags)]
/// The opaque alpha value of 1.0. pubconst OPAQUE: f32 = 1.0;
usecrate::{BasicParseError, Parser, ToCss, Token}; use std::fmt;
/// Clamp a 0..1 number to a 0..255 range to u8. /// /// Whilst scaling by 256 and flooring would provide /// an equal distribution of integers to percentage inputs, /// this is not what Gecko does so we instead multiply by 255 /// and round (adding 0.5 and flooring is equivalent to rounding) /// /// Chrome does something similar for the alpha value, but not /// the rgb values. /// /// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1340484> /// /// Clamping to 256 and rounding after would let 1.0 map to 256, and /// `256.0_f32 as u8` is undefined behavior: /// /// <https://github.com/rust-lang/rust/issues/10184> #[inline] pubfn clamp_unit_f32(val: f32) -> u8 {
clamp_floor_256_f32(val * 255.)
}
/// Round and clamp a single number to a u8. #[inline] pubfn clamp_floor_256_f32(val: f32) -> u8 {
val.round().clamp(0., 255.) as u8
}
/// Serialize the alpha copmonent of a color according to the specification. /// <https://drafts.csswg.org/css-color-4/#serializing-alpha-values> #[inline] pubfn serialize_color_alpha(
dest: &mutimpl fmt::Write,
alpha: Option<f32>,
legacy_syntax: bool,
) -> fmt::Result { let alpha = match alpha {
None => return dest.write_str(" / none"),
Some(a) => a,
};
// If the alpha component is full opaque, don't emit the alpha value in CSS. if alpha == OPAQUE { return Ok(());
}
impl PredefinedColorSpace { /// Parse a PredefinedColorSpace from the given input. pubfn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Self, BasicParseError<'i>> { let location = input.current_source_location();
/// Returns the named color with the given name. /// <https://drafts.csswg.org/css-color-4/#typedef-named-color> #[allow(clippy::result_unit_err)] #[inline] pubfn parse_named_color(ident: &str) -> Result<(u8, u8, u8), ()> {
named_colors::get(ident).copied().ok_or(())
}
/// Returns an iterator over all named CSS colors. /// <https://drafts.csswg.org/css-color-4/#typedef-named-color> #[inline] pubfn all_named_colors() -> impl Iterator<Item = (&'static str, (u8, u8, u8))> {
named_colors::entries().map(|(k, v)| (*k, *v))
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.