Trait palette::RelativeContrast [−][src]
pub trait RelativeContrast { type Scalar: FromF64 + PartialOrd; fn get_contrast_ratio(&self, other: &Self) -> Self::Scalar; fn has_min_contrast_text(&self, other: &Self) -> bool { ... } fn has_min_contrast_large_text(&self, other: &Self) -> bool { ... } fn has_enhanced_contrast_text(&self, other: &Self) -> bool { ... } fn has_enhanced_contrast_large_text(&self, other: &Self) -> bool { ... } fn has_min_contrast_graphics(&self, other: &Self) -> bool { ... } }
Expand description
A trait for calculating relative contrast between two colors.
W3C’s Web Content Accessibility Guidelines (WCAG) 2.1 suggest a method to calculate accessible contrast ratios of text and background colors for those with low vision or color deficiencies, and for contrast of colors used in user interface graphics objects.
These criteria are recommendations, not hard and fast rules. Most importantly, look at the colors in action and make sure they’re clear and comfortable to read. A pair of colors may pass contrast guidelines but still be uncomfortable to look at. Favor readability over only satisfying the contrast ratio metric. It is recommended to verify the contrast ratio in the output format of the colors and not to assume the contrast ratio remains exactly the same across color formats. The following example checks the contrast ratio of two colors in RGB format.
use std::str::FromStr; use palette::{Srgb, RelativeContrast}; // the rustdoc "DARK" theme background and text colors let my_background_rgb: Srgb<f32> = Srgb::from(0x353535).into_format(); let my_foreground_rgb = Srgb::from_str("#ddd")?.into_format(); assert!(my_background_rgb.has_enhanced_contrast_text(&my_foreground_rgb));
The possible range of contrast ratios is from 1:1 to 21:1. There is a
Success Criterion for Contrast (Minimum) and a Success Criterion for
Contrast (Enhanced), SC 1.4.3 and SC 1.4.6 respectively, which are concerned
with text and images of text. SC 1.4.11 is a Success Criterion for “non-text
contrast” such as user interface components and other graphics. The relative
contrast is calculated by (L1 + 0.05) / (L2 + 0.05)
, where L1
is the
luminance of the brighter color and L2
is the luminance of the darker
color both in sRGB linear space. A higher contrast ratio is generally
desireable.
For more details, visit the following links:
Success Criterion 1.4.3 Contrast (Minimum) (Level AA)
Associated Types
type Scalar: FromF64 + PartialOrd
type Scalar: FromF64 + PartialOrd
The type of the contrast ratio.
Required methods
fn get_contrast_ratio(&self, other: &Self) -> Self::Scalar
fn get_contrast_ratio(&self, other: &Self) -> Self::Scalar
Calculate the contrast ratio between two colors.
Provided methods
fn has_min_contrast_text(&self, other: &Self) -> bool
fn has_min_contrast_text(&self, other: &Self) -> bool
Verify the contrast between two colors satisfies SC 1.4.3. Contrast is at least 4.5:1 (Level AA).
fn has_min_contrast_large_text(&self, other: &Self) -> bool
fn has_min_contrast_large_text(&self, other: &Self) -> bool
Verify the contrast between two colors satisfies SC 1.4.3 for large text. Contrast is at least 3:1 (Level AA).
fn has_enhanced_contrast_text(&self, other: &Self) -> bool
fn has_enhanced_contrast_text(&self, other: &Self) -> bool
Verify the contrast between two colors satisfies SC 1.4.6. Contrast is at least 7:1 (Level AAA).
fn has_enhanced_contrast_large_text(&self, other: &Self) -> bool
fn has_enhanced_contrast_large_text(&self, other: &Self) -> bool
Verify the contrast between two colors satisfies SC 1.4.6 for large text. Contrast is at least 4.5:1 (Level AAA).
fn has_min_contrast_graphics(&self, other: &Self) -> bool
fn has_min_contrast_graphics(&self, other: &Self) -> bool
Verify the contrast between two colors satisfies SC 1.4.11 for graphical objects. Contrast is at least 3:1 (Level AA).