pub struct Color { /* private fields */ }Expand description
A color in RGBA form, optionally with an alpha channel.
Internally, stores:
r,g,bas 8-bit values.aas an f32 in [0..1].- a boolean
has_alphaindicating whether the color has transparency.
This struct offers methods to create and manipulate colors in a
CSS/Sass-like manner (e.g. grayscale(), invert(), mix(), etc.).
Implementations§
Source§impl Color
impl Color
Sourcepub fn new(r: u8, g: u8, b: u8, a: f32) -> Self
pub fn new(r: u8, g: u8, b: u8, a: f32) -> Self
Creates a new color from RGBA components.
§Arguments
r- Red channel (0..255).g- Green channel (0..255).b- Blue channel (0..255).a- Alpha channel (0..1). Ifa< 1.0, color is considered to have alpha.
§Examples
use grimoire_css_lib::color::Color;
let c = Color::new(255, 0, 0, 1.0); // Fully opaque redSourcepub fn from_rgb(r: u8, g: u8, b: u8, a: f32) -> Self
pub fn from_rgb(r: u8, g: u8, b: u8, a: f32) -> Self
Creates a color from RGB and alpha components.
Equivalent to Color::new, provided for clarity.
Sourcepub fn from_hwb(h: f32, w: f32, bk: f32, a: f32) -> Self
pub fn from_hwb(h: f32, w: f32, bk: f32, a: f32) -> Self
Creates a color from HWB values, plus alpha.
§Arguments
h- Hue in degrees (0..360).w- Whiteness (0..100).bk- Blackness (0..100).a- Alpha channel (0..1).
See the CSS spec.
Sourcepub fn from_hex(hex: &str) -> Option<Self>
pub fn from_hex(hex: &str) -> Option<Self>
Attempts to create a color from a hex string (e.g. "#ff00ff", "#fff").
Returns None if the string is invalid.
Supports 3-, 4-, 6-, and 8-digit hex forms (including alpha).
Sourcepub fn try_from_str(input: &str) -> Option<Self>
pub fn try_from_str(input: &str) -> Option<Self>
Attempts to create a color from a general CSS-like string:
- Named color (e.g.
"red","aliceblue") - Hex code (e.g.
"#fff","#ff00ff","#ffffffff") - Functional notation (e.g.
"rgb(...)","hsl(...)","hwb(...)")
Returns None if the color cannot be parsed.
Sourcepub fn to_hsl(&self) -> (f32, f32, f32)
pub fn to_hsl(&self) -> (f32, f32, f32)
Converts the current color to HSL and
returns (h, s, l) where:
his in [0..360] degrees,sandlare in [0..100] percent.
This does not change the alpha channel.
§Examples
use grimoire_css_lib::color::Color;
let c = Color::new(255, 0, 0, 1.0);
let (h, s, l) = c.to_hsl();
assert_eq!(h, 0.0);
assert_eq!(s, 100.0);
assert_eq!(l, 50.0);Sourcepub fn to_rgba(&self) -> (u8, u8, u8, f32)
pub fn to_rgba(&self) -> (u8, u8, u8, f32)
Returns (r,g,b,a) with:
r,g,bin [0..255]ain [0..1]
Sourcepub fn to_hex_string(&self) -> String
pub fn to_hex_string(&self) -> String
Returns a CSS hex string (e.g. "#7fffd4" or "#7fffd480" if alpha < 1.0).
§Examples
use grimoire_css_lib::color::Color;
let c = Color::new(127, 255, 212, 1.0);
assert_eq!(c.to_hex_string(), "#7fffd4");Sourcepub fn to_named_color_str(&self) -> Option<&'static str>
pub fn to_named_color_str(&self) -> Option<&'static str>
Returns the named color string (e.g. "red", "blue") if this color
matches one of the predefined colors exactly, otherwise None.
§Examples
use grimoire_css_lib::color::Color;
let c = Color::new(255, 0, 0, 1.0);
assert_eq!(c.to_named_color_str(), Some("red"));Sourcepub fn grayscale(&self) -> Self
pub fn grayscale(&self) -> Self
Converts the color to grayscale by setting saturation to 0%.
Other channels (hue, lightness, alpha) remain unchanged.
Sourcepub fn complement(&self) -> Self
pub fn complement(&self) -> Self
Returns the complementary color by adding 180° to the hue.
Sourcepub fn invert(&self, weight: Option<f32>) -> Self
pub fn invert(&self, weight: Option<f32>) -> Self
Inverts the color.
weight controls how much the color is inverted (0..100%, defaults to 100).
§Examples
use grimoire_css_lib::color::Color;
let white = Color::new(255, 255, 255, 1.0);
let black = white.invert(None);
assert_eq!(black.to_hex_string(), "#000000");Sourcepub fn mix(c1: Color, c2: Color, weight: f32) -> Self
pub fn mix(c1: Color, c2: Color, weight: f32) -> Self
Mixes two colors by a given weight (0..100%).
A weight of 50% returns an average of the two colors.
§Examples
use grimoire_css_lib::color::Color;
let red = Color::new(255, 0, 0, 1.0);
let blue = Color::new(0, 0, 255, 1.0);
let purple = Color::mix(red, blue, 50.0);
assert_eq!(purple.to_hex_string(), "#800080");Sourcepub fn adjust_hue(&self, degrees: f32) -> Self
pub fn adjust_hue(&self, degrees: f32) -> Self
Adjusts the hue by degrees.
If degrees is positive, hue rotates “forward”;
if negative, it rotates “backward”. Values can wrap beyond 360°.
Sourcepub fn adjust_color(
&self,
red_delta: Option<i32>,
green_delta: Option<i32>,
blue_delta: Option<i32>,
hue_delta: Option<f32>,
sat_delta: Option<f32>,
light_delta: Option<f32>,
alpha_delta: Option<f32>,
) -> Self
pub fn adjust_color( &self, red_delta: Option<i32>, green_delta: Option<i32>, blue_delta: Option<i32>, hue_delta: Option<f32>, sat_delta: Option<f32>, light_delta: Option<f32>, alpha_delta: Option<f32>, ) -> Self
Adjusts color by optionally modifying RGB deltas or HSL deltas.
§Arguments
red_delta,green_delta,blue_delta- The integer deltas to add to each channel.hue_delta,sat_delta,light_delta,alpha_delta- The float deltas for hue, saturation, lightness, alpha.
Missing arguments (i.e. None) leave that component unchanged.
§Examples
use grimoire_css_lib::color::Color;
let c = Color::new(128, 128, 128, 1.0);
// Make it slightly redder
let c2 = c.adjust_color(Some(10), None, None, None, None, None, None);Sourcepub fn change_color(
&self,
red: Option<u8>,
green: Option<u8>,
blue: Option<u8>,
hue_val: Option<f32>,
sat_val: Option<f32>,
light_val: Option<f32>,
alpha_val: Option<f32>,
) -> Self
pub fn change_color( &self, red: Option<u8>, green: Option<u8>, blue: Option<u8>, hue_val: Option<f32>, sat_val: Option<f32>, light_val: Option<f32>, alpha_val: Option<f32>, ) -> Self
Changes color by setting absolute values (if provided) for RGB or HSL.
§Arguments
red,green,blue- Final values for each channel (0..255).hue_val,sat_val,light_val- Final HSL values.alpha_val- Final alpha in [0..1].
Missing arguments (i.e. None) leave that component unchanged.
Sourcepub fn scale_color(
&self,
red_scale: Option<f32>,
green_scale: Option<f32>,
blue_scale: Option<f32>,
saturation_scale: Option<f32>,
lightness_scale: Option<f32>,
alpha_scale: Option<f32>,
) -> Self
pub fn scale_color( &self, red_scale: Option<f32>, green_scale: Option<f32>, blue_scale: Option<f32>, saturation_scale: Option<f32>, lightness_scale: Option<f32>, alpha_scale: Option<f32>, ) -> Self
Scales color channels by given percentages.
Positive scale values increase the channel, negative decrease.
E.g. red_scale=10.0 => +10% red from current value.
Missing arguments (i.e. None) leave that channel unchanged.
Sourcepub fn rgba(&self, alpha: f32) -> Self
pub fn rgba(&self, alpha: f32) -> Self
Returns a new color with the same RGB, but alpha set to alpha.
§Examples
use grimoire_css_lib::color::Color;
let c = Color::new(255, 0, 0, 1.0);
let half_transparent_red = c.rgba(0.5);Sourcepub fn lighten(&self, amount: f32) -> Self
pub fn lighten(&self, amount: f32) -> Self
Lightens the color by amount percent.
§Examples
use grimoire_css_lib::color::Color;
let red = Color::new(255, 0, 0, 1.0);
let lighter_red = red.lighten(10.0); // ~ #ff3333Sourcepub fn desaturate(&self, amount: f32) -> Self
pub fn desaturate(&self, amount: f32) -> Self
Decreases saturation by amount percent
Sourcepub fn opacify(&self, amount: f32) -> Self
pub fn opacify(&self, amount: f32) -> Self
Increases alpha by amount in [0..1], making the color more opaque.
§Examples
use grimoire_css_lib::color::Color;
let mut c = Color::new(255, 0, 0, 0.5);
c = c.opacify(0.3); // new alpha = 0.8Sourcepub fn transparentize(&self, amount: f32) -> Self
pub fn transparentize(&self, amount: f32) -> Self
Decreases alpha (increases transparency) by amount (0..1)
Trait Implementations§
impl Copy for Color
impl Eq for Color
Auto Trait Implementations§
impl Freeze for Color
impl RefUnwindSafe for Color
impl Send for Color
impl Sync for Color
impl Unpin for Color
impl UnwindSafe for Color
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more