diff options
author | 2024-09-11 01:28:03 +0200 | |
---|---|---|
committer | 2024-09-11 01:28:03 +0200 | |
commit | 7901d4737c5c75467bc694e2fa37057fbf5ca111 (patch) | |
tree | e9cfb2f138cb7921ccdcb18b129ff8677b44d968 /core | |
parent | 523708b5b1665f647bbe377a459e7c26410f7af8 (diff) | |
download | iced-7901d4737c5c75467bc694e2fa37057fbf5ca111.tar.gz iced-7901d4737c5c75467bc694e2fa37057fbf5ca111.tar.bz2 iced-7901d4737c5c75467bc694e2fa37057fbf5ca111.zip |
Encourage use of `color!` macro in `Color::parse` docs
Diffstat (limited to '')
-rw-r--r-- | core/src/color.rs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/core/src/color.rs b/core/src/color.rs index 89ec0e5b..46fe9ecd 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -88,10 +88,35 @@ impl Color { } } + /// Creates a [`Color`] from its linear RGBA components. + pub fn from_linear_rgba(r: f32, g: f32, b: f32, a: f32) -> Self { + // As described in: + // https://en.wikipedia.org/wiki/SRGB + fn gamma_component(u: f32) -> f32 { + if u < 0.0031308 { + 12.92 * u + } else { + 1.055 * u.powf(1.0 / 2.4) - 0.055 + } + } + + Self { + r: gamma_component(r), + g: gamma_component(g), + b: gamma_component(b), + a, + } + } + /// Parses a [`Color`] from a hex string. /// - /// Supported formats are #rrggbb, #rrggbbaa, #rgb, and #rgba. + /// Supported formats are `#rrggbb`, `#rrggbbaa`, `#rgb`, and `#rgba`. /// The starting "#" is optional. Both uppercase and lowercase are supported. + /// + /// If you have a static color string, using the [`color!`] macro should be preferred + /// since it leverages hexadecimal literal notation and arithmetic directly. + /// + /// [`color!`]: crate::color! pub fn parse(s: &str) -> Option<Color> { let hex = s.strip_prefix('#').unwrap_or(s); @@ -130,26 +155,6 @@ impl Color { }) } - /// Creates a [`Color`] from its linear RGBA components. - pub fn from_linear_rgba(r: f32, g: f32, b: f32, a: f32) -> Self { - // As described in: - // https://en.wikipedia.org/wiki/SRGB - fn gamma_component(u: f32) -> f32 { - if u < 0.0031308 { - 12.92 * u - } else { - 1.055 * u.powf(1.0 / 2.4) - 0.055 - } - } - - Self { - r: gamma_component(r), - g: gamma_component(g), - b: gamma_component(b), - a, - } - } - /// Converts the [`Color`] into its RGBA8 equivalent. #[must_use] pub fn into_rgba8(self) -> [u8; 4] { |