palette

Trait RelativeContrast

source
pub trait RelativeContrast: Sized {
    type Scalar: Real + PartialCmp;

    // Required method
    fn get_contrast_ratio(self, other: Self) -> Self::Scalar;

    // Provided methods
    fn has_min_contrast_text(
        self,
        other: Self,
    ) -> <Self::Scalar as HasBoolMask>::Mask { ... }
    fn has_min_contrast_large_text(
        self,
        other: Self,
    ) -> <Self::Scalar as HasBoolMask>::Mask { ... }
    fn has_enhanced_contrast_text(
        self,
        other: Self,
    ) -> <Self::Scalar as HasBoolMask>::Mask { ... }
    fn has_enhanced_contrast_large_text(
        self,
        other: Self,
    ) -> <Self::Scalar as HasBoolMask>::Mask { ... }
    fn has_min_contrast_graphics(
        self,
        other: Self,
    ) -> <Self::Scalar as HasBoolMask>::Mask { ... }
}
๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast
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 background: Srgb<f32> = Srgb::from(0x353535).into_format();
let foreground = Srgb::from_str("#ddd")?.into_format();

assert!(background.has_enhanced_contrast_text(foreground));

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 desirable.

For more details, visit the following links:

Success Criterion 1.4.3 Contrast (Minimum) (Level AA)

Success Criterion 1.4.6 Contrast (Enhanced) (Level AAA)

Success Criterion 1.4.11 Non-text Contrast (Level AA)

Required Associated Typesยง

source

type Scalar: Real + PartialCmp

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

The type of the contrast ratio.

Required Methodsยง

source

fn get_contrast_ratio(self, other: Self) -> Self::Scalar

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

Calculate the contrast ratio between two colors.

Provided Methodsยง

source

fn has_min_contrast_text( self, other: Self, ) -> <Self::Scalar as HasBoolMask>::Mask

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

Verify the contrast between two colors satisfies SC 1.4.3. Contrast is at least 4.5:1 (Level AA).

source

fn has_min_contrast_large_text( self, other: Self, ) -> <Self::Scalar as HasBoolMask>::Mask

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

Verify the contrast between two colors satisfies SC 1.4.3 for large text. Contrast is at least 3:1 (Level AA).

source

fn has_enhanced_contrast_text( self, other: Self, ) -> <Self::Scalar as HasBoolMask>::Mask

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

Verify the contrast between two colors satisfies SC 1.4.6. Contrast is at least 7:1 (Level AAA).

source

fn has_enhanced_contrast_large_text( self, other: Self, ) -> <Self::Scalar as HasBoolMask>::Mask

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

Verify the contrast between two colors satisfies SC 1.4.6 for large text. Contrast is at least 4.5:1 (Level AAA).

source

fn has_min_contrast_graphics( self, other: Self, ) -> <Self::Scalar as HasBoolMask>::Mask

๐Ÿ‘ŽDeprecated since 0.7.2: replaced by palette::color_difference::Wcag21RelativeContrast

Verify the contrast between two colors satisfies SC 1.4.11 for graphical objects. Contrast is at least 3:1 (Level AA).

Object Safetyยง

This trait is not object safe.

Implementorsยง

sourceยง

impl<S, T> RelativeContrast for Luma<S, T>

sourceยง

impl<S, T> RelativeContrast for Rgb<S, T>

sourceยง

impl<S, T> RelativeContrast for Hsl<S, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, S: RgbStandard, Xyz<<S::Space as RgbSpace>::WhitePoint, T>: FromColor<Self>,

sourceยง

impl<S, T> RelativeContrast for Hsv<S, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, S: RgbStandard, Xyz<<S::Space as RgbSpace>::WhitePoint, T>: FromColor<Self>,

sourceยง

impl<S, T> RelativeContrast for Hwb<S, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, S: RgbStandard, Xyz<<S::Space as RgbSpace>::WhitePoint, T>: FromColor<Self>,

sourceยง

impl<T> RelativeContrast for Okhsl<T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<D65, T>: FromColor<Self>,

sourceยง

impl<T> RelativeContrast for Okhwb<T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<D65, T>: FromColor<Self>,

sourceยง

impl<T> RelativeContrast for Oklab<T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<D65, T>: FromColor<Self>,

sourceยง

impl<T> RelativeContrast for Oklch<T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<D65, T>: FromColor<Self>,

sourceยง

impl<Wp, T> RelativeContrast for Hsluv<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<Wp, T>: FromColor<Self>,

sourceยง

impl<Wp, T> RelativeContrast for Lab<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<Wp, T>: FromColor<Self>,

sourceยง

impl<Wp, T> RelativeContrast for Lch<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<Wp, T>: FromColor<Self>,

sourceยง

impl<Wp, T> RelativeContrast for Lchuv<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Xyz<Wp, T>: FromColor<Self>,

sourceยง

impl<Wp, T> RelativeContrast for Luv<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>, Wp: WhitePoint<T>, Xyz<Wp, T>: FromColor<Self>,

sourceยง

impl<Wp, T> RelativeContrast for Xyz<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>,

sourceยง

impl<Wp, T> RelativeContrast for Yxy<Wp, T>
where T: Real + Arithmetics + PartialCmp, T::Mask: LazySelect<T>,