Skip to main content

ColorU

Struct ColorU 

Source
#[repr(C)]
pub struct ColorU { pub r: u8, pub g: u8, pub b: u8, pub a: u8, }
Expand description

u8-based color, range 0 to 255 (similar to webrenders ColorU)

Fields§

§r: u8§g: u8§b: u8§a: u8

Implementations§

Source§

impl ColorU

Source

pub const ALPHA_TRANSPARENT: u8 = 0

Source

pub const ALPHA_OPAQUE: u8 = 255

Source

pub const RED: ColorU

Source

pub const GREEN: ColorU

Source

pub const BLUE: ColorU

Source

pub const WHITE: ColorU

Source

pub const BLACK: ColorU

Source

pub const TRANSPARENT: ColorU

Source

pub const YELLOW: ColorU

Source

pub const CYAN: ColorU

Source

pub const MAGENTA: ColorU

Source

pub const ORANGE: ColorU

Source

pub const PINK: ColorU

Source

pub const PURPLE: ColorU

Source

pub const BROWN: ColorU

Source

pub const GRAY: ColorU

Source

pub const LIGHT_GRAY: ColorU

Source

pub const DARK_GRAY: ColorU

Source

pub const NAVY: ColorU

Source

pub const TEAL: ColorU

Source

pub const OLIVE: ColorU

Source

pub const MAROON: ColorU

Source

pub const LIME: ColorU

Source

pub const AQUA: ColorU

Source

pub const SILVER: ColorU

Source

pub const FUCHSIA: ColorU

Source

pub const INDIGO: ColorU

Source

pub const GOLD: ColorU

Source

pub const CORAL: ColorU

Source

pub const SALMON: ColorU

Source

pub const TURQUOISE: ColorU

Source

pub const VIOLET: ColorU

Source

pub const CRIMSON: ColorU

Source

pub const CHOCOLATE: ColorU

Source

pub const SKY_BLUE: ColorU

Source

pub const FOREST_GREEN: ColorU

Source

pub const SEA_GREEN: ColorU

Source

pub const SLATE_GRAY: ColorU

Source

pub const MIDNIGHT_BLUE: ColorU

Source

pub const DARK_RED: ColorU

Source

pub const DARK_GREEN: ColorU

Source

pub const DARK_BLUE: ColorU

Source

pub const LIGHT_BLUE: ColorU

Source

pub const LIGHT_GREEN: ColorU

Source

pub const LIGHT_YELLOW: ColorU

Source

pub const LIGHT_PINK: ColorU

Source

pub fn red() -> Self

Source

pub fn green() -> Self

Source

pub fn blue() -> Self

Source

pub fn white() -> Self

Source

pub fn black() -> Self

Source

pub fn transparent() -> Self

Source

pub fn yellow() -> Self

Source

pub fn cyan() -> Self

Source

pub fn magenta() -> Self

Source

pub fn orange() -> Self

Source

pub fn pink() -> Self

Source

pub fn purple() -> Self

Source

pub fn brown() -> Self

Source

pub fn gray() -> Self

Source

pub fn light_gray() -> Self

Source

pub fn dark_gray() -> Self

Source

pub fn navy() -> Self

Source

pub fn teal() -> Self

Source

pub fn olive() -> Self

Source

pub fn maroon() -> Self

Source

pub fn lime() -> Self

Source

pub fn aqua() -> Self

Source

pub fn silver() -> Self

Source

pub fn fuchsia() -> Self

Source

pub fn indigo() -> Self

Source

pub fn gold() -> Self

Source

pub fn coral() -> Self

Source

pub fn salmon() -> Self

Source

pub fn turquoise() -> Self

Source

pub fn violet() -> Self

Source

pub fn crimson() -> Self

Source

pub fn chocolate() -> Self

Source

pub fn sky_blue() -> Self

Source

pub fn forest_green() -> Self

Source

pub fn sea_green() -> Self

Source

pub fn slate_gray() -> Self

Source

pub fn midnight_blue() -> Self

Source

pub fn dark_red() -> Self

Source

pub fn dark_green() -> Self

Source

pub fn dark_blue() -> Self

Source

pub fn light_blue() -> Self

Source

pub fn light_green() -> Self

Source

pub fn light_yellow() -> Self

Source

pub fn light_pink() -> Self

Source

pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self

Creates a new color with RGBA values.

Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Creates a new color with RGB values (alpha = 255).

Source

pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self

Alias for rgba - kept for internal compatibility, not exposed in FFI.

Source

pub const fn new_rgb(r: u8, g: u8, b: u8) -> Self

Alias for rgb - kept for internal compatibility, not exposed in FFI.

Source

pub fn interpolate(&self, other: &Self, t: f32) -> Self

Source

pub fn lighten(&self, amount: f32) -> Self

Lighten a color by a percentage (0.0 to 1.0). Returns a new color blended towards white.

Source

pub fn darken(&self, amount: f32) -> Self

Darken a color by a percentage (0.0 to 1.0). Returns a new color blended towards black.

Source

pub fn mix(&self, other: &Self, ratio: f32) -> Self

Mix two colors together with a given ratio (0.0 = self, 1.0 = other).

Source

pub fn hover_variant(&self) -> Self

Create a hover variant (slightly lighter for dark colors, darker for light colors). This is useful for button hover states.

Source

pub fn active_variant(&self) -> Self

Create an active/pressed variant (darker than hover). This is useful for button active states.

Source

pub fn luminance(&self) -> f32

Calculate relative luminance (0.0 = black, 1.0 = white). Uses the sRGB luminance formula.

Source

pub fn contrast_text(&self) -> Self

Returns white or black text color for best contrast on this background.

Source

pub fn relative_luminance(&self) -> f32

Calculate relative luminance per WCAG 2.1 specification. Returns a value between 0.0 (darkest) and 1.0 (lightest). Uses the sRGB to linear conversion for accurate results.

Source

pub fn contrast_ratio(&self, other: &Self) -> f32

Calculate the contrast ratio between this color and another. Returns a value between 1.0 (no contrast) and 21.0 (max contrast).

WCAG 2.1 requirements:

  • AA normal text: >= 4.5:1
  • AA large text: >= 3.0:1
  • AAA normal text: >= 7.0:1
  • AAA large text: >= 4.5:1
Source

pub fn meets_wcag_aa(&self, other: &Self) -> bool

Check if the contrast ratio meets WCAG AA requirements for normal text (>= 4.5:1).

Source

pub fn meets_wcag_aa_large(&self, other: &Self) -> bool

Check if the contrast ratio meets WCAG AA requirements for large text (>= 3.0:1). Large text is defined as 18pt+ or 14pt+ bold.

Source

pub fn meets_wcag_aaa(&self, other: &Self) -> bool

Check if the contrast ratio meets WCAG AAA requirements for normal text (>= 7.0:1).

Source

pub fn meets_wcag_aaa_large(&self, other: &Self) -> bool

Check if the contrast ratio meets WCAG AAA requirements for large text (>= 4.5:1).

Source

pub fn is_light(&self) -> bool

Returns true if this color is considered “light” (luminance > 0.5). Useful for determining if dark or light text should be used.

Source

pub fn is_dark(&self) -> bool

Returns true if this color is considered “dark” (luminance <= 0.5).

Source

pub fn best_contrast_text(&self) -> Self

Suggest the best text color (black or white) for this background, ensuring WCAG AA compliance for normal text.

If neither black nor white meets AA requirements (unlikely), returns the one with higher contrast.

Source

pub fn ensure_contrast(&self, background: &Self, min_ratio: f32) -> Self

Adjust the color to ensure it meets the minimum contrast ratio against a background. Lightens or darkens the color as needed.

Returns the original color if it already meets the requirement, otherwise returns an adjusted color that meets the minimum contrast.

Source

pub fn apca_contrast(&self, background: &Self) -> f32

Calculate the APCA (Accessible Perceptual Contrast Algorithm) contrast. This is a newer algorithm that may replace WCAG contrast in future standards. Returns a value between -108 (white on black) and 106 (black on white).

Note: The sign indicates polarity (negative = light text on dark bg). For most purposes, use the absolute value.

Source

pub fn meets_apca_body(&self, background: &Self) -> bool

Check if the APCA contrast meets the recommended minimum for body text (|Lc| >= 60).

Source

pub fn meets_apca_large(&self, background: &Self) -> bool

Check if the APCA contrast meets the minimum for large text (|Lc| >= 45).

Source

pub fn with_alpha(&self, a: u8) -> Self

Set the alpha channel while keeping RGB values.

Source

pub fn with_alpha_f32(&self, a: f32) -> Self

Set the alpha as a float (0.0 to 1.0).

Source

pub fn invert(&self) -> Self

Invert the color (keeping alpha).

Source

pub fn to_grayscale(&self) -> Self

Convert to grayscale using luminance weights.

Source

pub const fn has_alpha(&self) -> bool

Source

pub fn to_hash(&self) -> String

Source

pub fn strawberry(shade: usize) -> Self

Strawberry color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn palette_orange(shade: usize) -> Self

Orange color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn banana(shade: usize) -> Self

Banana color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn palette_lime(shade: usize) -> Self

Lime color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn mint(shade: usize) -> Self

Mint color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn blueberry(shade: usize) -> Self

Blueberry color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn grape(shade: usize) -> Self

Grape color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn bubblegum(shade: usize) -> Self

Bubblegum color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn cocoa(shade: usize) -> Self

Cocoa color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn palette_silver(shade: usize) -> Self

Silver color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn slate(shade: usize) -> Self

Slate color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn dark(shade: usize) -> Self

Dark color palette (shade: 100, 300, 500, 700, 900)

Source

pub fn apple_red() -> Self

Apple Red (light mode)

Source

pub fn apple_red_dark() -> Self

Apple Red (dark mode)

Source

pub fn apple_orange() -> Self

Apple Orange (light mode)

Source

pub fn apple_orange_dark() -> Self

Apple Orange (dark mode)

Source

pub fn apple_yellow() -> Self

Apple Yellow (light mode)

Source

pub fn apple_yellow_dark() -> Self

Apple Yellow (dark mode)

Source

pub fn apple_green() -> Self

Apple Green (light mode)

Source

pub fn apple_green_dark() -> Self

Apple Green (dark mode)

Source

pub fn apple_mint() -> Self

Apple Mint (light mode)

Source

pub fn apple_mint_dark() -> Self

Apple Mint (dark mode)

Source

pub fn apple_teal() -> Self

Apple Teal (light mode)

Source

pub fn apple_teal_dark() -> Self

Apple Teal (dark mode)

Source

pub fn apple_cyan() -> Self

Apple Cyan (light mode)

Source

pub fn apple_cyan_dark() -> Self

Apple Cyan (dark mode)

Source

pub fn apple_blue() -> Self

Apple Blue (light mode)

Source

pub fn apple_blue_dark() -> Self

Apple Blue (dark mode)

Source

pub fn apple_indigo() -> Self

Apple Indigo (light mode)

Source

pub fn apple_indigo_dark() -> Self

Apple Indigo (dark mode)

Source

pub fn apple_purple() -> Self

Apple Purple (light mode)

Source

pub fn apple_purple_dark() -> Self

Apple Purple (dark mode)

Source

pub fn apple_pink() -> Self

Apple Pink (light mode)

Source

pub fn apple_pink_dark() -> Self

Apple Pink (dark mode)

Source

pub fn apple_brown() -> Self

Apple Brown (light mode)

Source

pub fn apple_brown_dark() -> Self

Apple Brown (dark mode)

Source

pub fn apple_gray() -> Self

Apple Gray (light mode)

Source

pub fn apple_gray_dark() -> Self

Apple Gray (dark mode)

Source

pub fn bootstrap_primary() -> Self

Primary button color (blue) - used for main actions

Source

pub fn bootstrap_primary_hover() -> Self

Source

pub fn bootstrap_primary_active() -> Self

Source

pub fn bootstrap_secondary() -> Self

Secondary button color (gray) - used for secondary actions

Source

pub fn bootstrap_secondary_hover() -> Self

Source

pub fn bootstrap_secondary_active() -> Self

Source

pub fn bootstrap_success() -> Self

Success button color (green) - used for confirmations

Source

pub fn bootstrap_success_hover() -> Self

Source

pub fn bootstrap_success_active() -> Self

Source

pub fn bootstrap_danger() -> Self

Danger button color (red) - used for destructive actions

Source

pub fn bootstrap_danger_hover() -> Self

Source

pub fn bootstrap_danger_active() -> Self

Source

pub fn bootstrap_warning() -> Self

Warning button color (yellow) - used for warnings, uses BLACK text

Source

pub fn bootstrap_warning_hover() -> Self

Source

pub fn bootstrap_warning_active() -> Self

Source

pub fn bootstrap_info() -> Self

Info button color (teal/cyan) - used for informational actions

Source

pub fn bootstrap_info_hover() -> Self

Source

pub fn bootstrap_info_active() -> Self

Source

pub fn bootstrap_light() -> Self

Light button color - used for light-themed buttons

Source

pub fn bootstrap_light_hover() -> Self

Source

pub fn bootstrap_light_active() -> Self

Source

pub fn bootstrap_dark() -> Self

Dark button color - used for dark-themed buttons

Source

pub fn bootstrap_dark_hover() -> Self

Source

pub fn bootstrap_dark_active() -> Self

Link button text color

Trait Implementations§

Source§

impl Clone for ColorU

Source§

fn clone(&self) -> ColorU

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ColorU

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ColorU

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for ColorU

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<ColorF> for ColorU

Source§

fn from(input: ColorF) -> ColorU

Converts to this type from the input type.
Source§

impl From<ColorU> for ColorF

Source§

fn from(input: ColorU) -> ColorF

Converts to this type from the input type.
Source§

impl From<ColorU> for ColorOrSystem

Source§

fn from(color: ColorU) -> Self

Converts to this type from the input type.
Source§

impl Hash for ColorU

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for ColorU

Source§

fn cmp(&self, other: &ColorU) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ColorU

Source§

fn eq(&self, other: &ColorU) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ColorU

Source§

fn partial_cmp(&self, other: &ColorU) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for ColorU

Source§

impl Eq for ColorU

Source§

impl StructuralPartialEq for ColorU

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GetHash for T
where T: Hash,

Source§

fn get_hash(&self) -> u64

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.