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) -> Color
pub fn new(r: u8, g: u8, b: u8, a: f32) -> Color
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_color_toolkit_lib::Color;
let c = Color::new(255, 0, 0, 1.0); // Fully opaque redSourcepub fn from_rgb(r: u8, g: u8, b: u8, a: f32) -> Color
pub fn from_rgb(r: u8, g: u8, b: u8, a: f32) -> Color
Creates a color from RGB and alpha components.
Equivalent to Color::new, provided for clarity.
Sourcepub fn from_hsl(h: f32, s: f32, l: f32, a: f32) -> Color
pub fn from_hsl(h: f32, s: f32, l: f32, a: f32) -> Color
Creates a color from HSL values, plus alpha.
§Arguments
h- Hue, in degrees (0..360).s- Saturation, in percent (0..100).l- Lightness, in percent (0..100).a- Alpha channel (0..1).
§Examples
use grimoire_css_color_toolkit_lib::Color;
// Pure red via HSL
let c = Color::from_hsl(0.0, 100.0, 50.0, 1.0);Sourcepub fn from_hwb(h: f32, w: f32, bk: f32, a: f32) -> Color
pub fn from_hwb(h: f32, w: f32, bk: f32, a: f32) -> Color
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<Color>
pub fn from_hex(hex: &str) -> Option<Color>
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<Color>
pub fn try_from_str(input: &str) -> Option<Color>
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_color_toolkit_lib::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_color_toolkit_lib::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_color_toolkit_lib::Color;
let c = Color::new(255, 0, 0, 1.0);
assert_eq!(c.to_named_color_str(), Some("red"));Sourcepub fn grayscale(&self) -> Color
pub fn grayscale(&self) -> Color
Converts the color to grayscale by setting saturation to 0%.
Other channels (hue, lightness, alpha) remain unchanged.
Sourcepub fn complement(&self) -> Color
pub fn complement(&self) -> Color
Returns the complementary color by adding 180° to the hue.
Sourcepub fn invert(&self, weight: Option<f32>) -> Color
pub fn invert(&self, weight: Option<f32>) -> Color
Inverts the color.
weight controls how much the color is inverted (0..100%, defaults to 100).
§Examples
use grimoire_css_color_toolkit_lib::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) -> Color
pub fn mix(c1: Color, c2: Color, weight: f32) -> Color
Mixes two colors by a given weight (0..100%).
A weight of 50% returns an average of the two colors.
§Examples
use grimoire_css_color_toolkit_lib::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) -> Color
pub fn adjust_hue(&self, degrees: f32) -> Color
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>,
) -> Color
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>, ) -> Color
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_color_toolkit_lib::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>,
) -> Color
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>, ) -> Color
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>,
) -> Color
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>, ) -> Color
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) -> Color
pub fn rgba(&self, alpha: f32) -> Color
Returns a new color with the same RGB, but alpha set to alpha.
§Examples
use grimoire_css_color_toolkit_lib::Color;
let c = Color::new(255, 0, 0, 1.0);
let half_transparent_red = c.rgba(0.5);Sourcepub fn lighten(&self, amount: f32) -> Color
pub fn lighten(&self, amount: f32) -> Color
Lightens the color by amount percent.
§Examples
use grimoire_css_color_toolkit_lib::Color;
let red = Color::new(255, 0, 0, 1.0);
let lighter_red = red.lighten(10.0); // ~ #ff3333Sourcepub fn desaturate(&self, amount: f32) -> Color
pub fn desaturate(&self, amount: f32) -> Color
Decreases saturation by amount percent
Sourcepub fn opacify(&self, amount: f32) -> Color
pub fn opacify(&self, amount: f32) -> Color
Increases alpha by amount in [0..1], making the color more opaque.
§Examples
use grimoire_css_color_toolkit_lib::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) -> Color
pub fn transparentize(&self, amount: f32) -> Color
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 moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more