#[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: u8Implementations§
Source§impl ColorU
impl ColorU
pub const ALPHA_TRANSPARENT: u8 = 0
pub const ALPHA_OPAQUE: u8 = 255
pub const RED: Self
pub const GREEN: Self
pub const BLUE: Self
pub const WHITE: Self
pub const BLACK: Self
pub const TRANSPARENT: Self
pub const YELLOW: Self
pub const CYAN: Self
pub const MAGENTA: Self
pub const ORANGE: Self
pub const PINK: Self
pub const PURPLE: Self
pub const BROWN: Self
pub const GRAY: Self
pub const LIGHT_GRAY: Self
pub const DARK_GRAY: Self
pub const NAVY: Self
pub const TEAL: Self
pub const OLIVE: Self
pub const MAROON: Self
pub const LIME: Self
pub const AQUA: Self
pub const SILVER: Self
pub const FUCHSIA: Self
pub const INDIGO: Self
pub const GOLD: Self
pub const CORAL: Self
pub const SALMON: Self
pub const TURQUOISE: Self
pub const VIOLET: Self
pub const CRIMSON: Self
pub const CHOCOLATE: Self
pub const SKY_BLUE: Self
pub const FOREST_GREEN: Self
pub const SEA_GREEN: Self
pub const SLATE_GRAY: Self
pub const MIDNIGHT_BLUE: Self
pub const DARK_RED: Self
pub const DARK_GREEN: Self
pub const DARK_BLUE: Self
pub const LIGHT_BLUE: Self
pub const LIGHT_GREEN: Self
pub const LIGHT_YELLOW: Self
pub const LIGHT_PINK: Self
pub const fn red() -> Self
pub const fn green() -> Self
pub const fn blue() -> Self
pub const fn white() -> Self
pub const fn black() -> Self
pub const fn transparent() -> Self
pub const fn yellow() -> Self
pub const fn cyan() -> Self
pub const fn magenta() -> Self
pub const fn orange() -> Self
pub const fn pink() -> Self
pub const fn purple() -> Self
pub const fn brown() -> Self
pub const fn gray() -> Self
pub const fn light_gray() -> Self
pub const fn dark_gray() -> Self
pub const fn teal() -> Self
pub const fn olive() -> Self
pub const fn maroon() -> Self
pub const fn lime() -> Self
pub const fn aqua() -> Self
pub const fn silver() -> Self
pub const fn fuchsia() -> Self
pub const fn indigo() -> Self
pub const fn gold() -> Self
pub const fn coral() -> Self
pub const fn salmon() -> Self
pub const fn turquoise() -> Self
pub const fn violet() -> Self
pub const fn crimson() -> Self
pub const fn chocolate() -> Self
pub const fn sky_blue() -> Self
pub const fn forest_green() -> Self
pub const fn sea_green() -> Self
pub const fn slate_gray() -> Self
pub const fn midnight_blue() -> Self
pub const fn dark_red() -> Self
pub const fn dark_green() -> Self
pub const fn dark_blue() -> Self
pub const fn light_blue() -> Self
pub const fn light_green() -> Self
pub const fn light_yellow() -> Self
pub const fn light_pink() -> Self
Sourcepub const fn rgb(r: u8, g: u8, b: u8) -> Self
pub const fn rgb(r: u8, g: u8, b: u8) -> Self
Creates a new color with RGB values (alpha = 255).
Sourcepub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self
Alias for rgba - kept for internal compatibility, not exposed in FFI.
Sourcepub const fn new_rgb(r: u8, g: u8, b: u8) -> Self
pub const fn new_rgb(r: u8, g: u8, b: u8) -> Self
Alias for rgb - kept for internal compatibility, not exposed in FFI.
Sourcepub fn interpolate(&self, other: &Self, t: f32) -> Self
pub fn interpolate(&self, other: &Self, t: f32) -> Self
Linearly interpolate all four RGBA channels between self and other.
t = 0.0 returns self, t = 1.0 returns other.
Sourcepub fn lighten(&self, amount: f32) -> Self
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, preserving the original alpha.
Sourcepub fn darken(&self, amount: f32) -> Self
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, preserving the original alpha.
Sourcepub fn mix(&self, other: &Self, ratio: f32) -> Self
pub fn mix(&self, other: &Self, ratio: f32) -> Self
Mix two colors together with a given ratio (0.0 = self, 1.0 = other).
Sourcepub fn hover_variant(&self) -> Self
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.
Sourcepub fn active_variant(&self) -> Self
pub fn active_variant(&self) -> Self
Create an active/pressed variant (darker than hover). This is useful for button active states.
Sourcepub fn luminance(&self) -> f32
pub fn luminance(&self) -> f32
Calculate approximate luminance (0.0 = black, 1.0 = white).
Note: This applies BT.709 coefficients directly to gamma-encoded sRGB
values without linearizing first, so it is only an approximation.
For accurate results (e.g. WCAG contrast checks), use [relative_luminance()].
Sourcepub fn contrast_text(&self) -> Self
pub fn contrast_text(&self) -> Self
Returns white or black text color for best contrast on this background.
Sourcepub fn relative_luminance(&self) -> f32
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.
Sourcepub fn contrast_ratio(&self, other: &Self) -> f32
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
Sourcepub fn meets_wcag_aa(&self, other: &Self) -> bool
pub fn meets_wcag_aa(&self, other: &Self) -> bool
Check if the contrast ratio meets WCAG AA requirements for normal text (>= 4.5:1).
Sourcepub fn meets_wcag_aa_large(&self, other: &Self) -> bool
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.
Sourcepub fn meets_wcag_aaa(&self, other: &Self) -> bool
pub fn meets_wcag_aaa(&self, other: &Self) -> bool
Check if the contrast ratio meets WCAG AAA requirements for normal text (>= 7.0:1).
Sourcepub fn meets_wcag_aaa_large(&self, other: &Self) -> bool
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).
Sourcepub fn is_light(&self) -> bool
pub fn is_light(&self) -> bool
Returns true if this color is considered “light” (relative luminance > 0.5). Useful for determining if dark or light text should be used.
Sourcepub fn is_dark(&self) -> bool
pub fn is_dark(&self) -> bool
Returns true if this color is considered “dark” (relative luminance <= 0.5).
Sourcepub fn best_contrast_text(&self) -> Self
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.
Sourcepub fn ensure_contrast(&self, background: &Self, min_ratio: f32) -> Self
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.
Sourcepub fn apca_contrast(&self, background: &Self) -> f32
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: This is an approximation — it reuses the WCAG piecewise sRGB linearization and BT.709 luminance coefficients rather than the APCA-specific TRC exponents and coefficients from the full 0.0.98G specification.
The sign indicates polarity (negative = light text on dark bg). For most purposes, use the absolute value.
Sourcepub fn meets_apca_body(&self, background: &Self) -> bool
pub fn meets_apca_body(&self, background: &Self) -> bool
Check if the APCA contrast meets the recommended minimum for body text (|Lc| >= 60).
Sourcepub fn meets_apca_large(&self, background: &Self) -> bool
pub fn meets_apca_large(&self, background: &Self) -> bool
Check if the APCA contrast meets the minimum for large text (|Lc| >= 45).
Sourcepub const fn with_alpha(&self, a: u8) -> Self
pub const fn with_alpha(&self, a: u8) -> Self
Set the alpha channel while keeping RGB values.
Sourcepub fn with_alpha_f32(&self, a: f32) -> Self
pub fn with_alpha_f32(&self, a: f32) -> Self
Set the alpha as a float (0.0 to 1.0).
Sourcepub fn to_grayscale(&self) -> Self
pub fn to_grayscale(&self) -> Self
Convert to grayscale using luminance weights.
Sourcepub const fn has_alpha(&self) -> bool
pub const fn has_alpha(&self) -> bool
Returns true if the alpha channel is not fully opaque (i.e. a != 255).
Sourcepub fn to_hash(&self) -> String
pub fn to_hash(&self) -> String
Format the color as an 8-digit lowercase hex string (e.g. #ff0000ff).
Sourcepub const fn strawberry(shade: usize) -> Self
pub const fn strawberry(shade: usize) -> Self
Strawberry color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn palette_orange(shade: usize) -> Self
pub const fn palette_orange(shade: usize) -> Self
Orange color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn banana(shade: usize) -> Self
pub const fn banana(shade: usize) -> Self
Banana color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn palette_lime(shade: usize) -> Self
pub const fn palette_lime(shade: usize) -> Self
Lime color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn blueberry(shade: usize) -> Self
pub const fn blueberry(shade: usize) -> Self
Blueberry color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn bubblegum(shade: usize) -> Self
pub const fn bubblegum(shade: usize) -> Self
Bubblegum color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn palette_silver(shade: usize) -> Self
pub const fn palette_silver(shade: usize) -> Self
Silver color palette (shade: 100, 300, 500, 700, 900)
Sourcepub const fn apple_red_dark() -> Self
pub const fn apple_red_dark() -> Self
Apple Red (dark mode)
Sourcepub const fn apple_orange() -> Self
pub const fn apple_orange() -> Self
Apple Orange (light mode)
Sourcepub const fn apple_orange_dark() -> Self
pub const fn apple_orange_dark() -> Self
Apple Orange (dark mode)
Sourcepub const fn apple_yellow() -> Self
pub const fn apple_yellow() -> Self
Apple Yellow (light mode)
Sourcepub const fn apple_yellow_dark() -> Self
pub const fn apple_yellow_dark() -> Self
Apple Yellow (dark mode)
Sourcepub const fn apple_green() -> Self
pub const fn apple_green() -> Self
Apple Green (light mode)
Sourcepub const fn apple_green_dark() -> Self
pub const fn apple_green_dark() -> Self
Apple Green (dark mode)
Sourcepub const fn apple_mint() -> Self
pub const fn apple_mint() -> Self
Apple Mint (light mode)
Sourcepub const fn apple_mint_dark() -> Self
pub const fn apple_mint_dark() -> Self
Apple Mint (dark mode)
Sourcepub const fn apple_teal() -> Self
pub const fn apple_teal() -> Self
Apple Teal (light mode)
Sourcepub const fn apple_teal_dark() -> Self
pub const fn apple_teal_dark() -> Self
Apple Teal (dark mode)
Sourcepub const fn apple_cyan() -> Self
pub const fn apple_cyan() -> Self
Apple Cyan (light mode)
Sourcepub const fn apple_cyan_dark() -> Self
pub const fn apple_cyan_dark() -> Self
Apple Cyan (dark mode)
Sourcepub const fn apple_blue() -> Self
pub const fn apple_blue() -> Self
Apple Blue (light mode)
Sourcepub const fn apple_blue_dark() -> Self
pub const fn apple_blue_dark() -> Self
Apple Blue (dark mode)
Sourcepub const fn apple_indigo() -> Self
pub const fn apple_indigo() -> Self
Apple Indigo (light mode)
Sourcepub const fn apple_indigo_dark() -> Self
pub const fn apple_indigo_dark() -> Self
Apple Indigo (dark mode)
Sourcepub const fn apple_purple() -> Self
pub const fn apple_purple() -> Self
Apple Purple (light mode)
Sourcepub const fn apple_purple_dark() -> Self
pub const fn apple_purple_dark() -> Self
Apple Purple (dark mode)
Sourcepub const fn apple_pink() -> Self
pub const fn apple_pink() -> Self
Apple Pink (light mode)
Sourcepub const fn apple_pink_dark() -> Self
pub const fn apple_pink_dark() -> Self
Apple Pink (dark mode)
Sourcepub const fn apple_brown() -> Self
pub const fn apple_brown() -> Self
Apple Brown (light mode)
Sourcepub const fn apple_brown_dark() -> Self
pub const fn apple_brown_dark() -> Self
Apple Brown (dark mode)
Sourcepub const fn apple_gray() -> Self
pub const fn apple_gray() -> Self
Apple Gray (light mode)
Sourcepub const fn apple_gray_dark() -> Self
pub const fn apple_gray_dark() -> Self
Apple Gray (dark mode)
Sourcepub const fn bootstrap_primary() -> Self
pub const fn bootstrap_primary() -> Self
Primary button color (blue) - used for main actions
pub const fn bootstrap_primary_hover() -> Self
pub const fn bootstrap_primary_active() -> Self
Sourcepub const fn bootstrap_secondary() -> Self
pub const fn bootstrap_secondary() -> Self
Secondary button color (gray) - used for secondary actions
pub const fn bootstrap_secondary_hover() -> Self
pub const fn bootstrap_secondary_active() -> Self
Sourcepub const fn bootstrap_success() -> Self
pub const fn bootstrap_success() -> Self
Success button color (green) - used for confirmations
pub const fn bootstrap_success_hover() -> Self
pub const fn bootstrap_success_active() -> Self
Sourcepub const fn bootstrap_danger() -> Self
pub const fn bootstrap_danger() -> Self
Danger button color (red) - used for destructive actions
pub const fn bootstrap_danger_hover() -> Self
pub const fn bootstrap_danger_active() -> Self
Sourcepub const fn bootstrap_warning() -> Self
pub const fn bootstrap_warning() -> Self
Warning button color (yellow) - used for warnings, uses BLACK text
pub const fn bootstrap_warning_hover() -> Self
pub const fn bootstrap_warning_active() -> Self
Sourcepub const fn bootstrap_info() -> Self
pub const fn bootstrap_info() -> Self
Info button color (teal/cyan) - used for informational actions
pub const fn bootstrap_info_hover() -> Self
pub const fn bootstrap_info_active() -> Self
Sourcepub const fn bootstrap_light() -> Self
pub const fn bootstrap_light() -> Self
Light button color - used for light-themed buttons
pub const fn bootstrap_light_hover() -> Self
pub const fn bootstrap_light_active() -> Self
Sourcepub const fn bootstrap_dark() -> Self
pub const fn bootstrap_dark() -> Self
Dark button color - used for dark-themed buttons
pub const fn bootstrap_dark_hover() -> Self
pub const fn bootstrap_dark_active() -> Self
Sourcepub const fn bootstrap_link() -> Self
pub const fn bootstrap_link() -> Self
Link button text color