Debian Codegraph @

src/relative_contrast.rs

raw symbols SCIP
1 use craterust-analyzer cargo palette 0.7.6 crate/::{
2 bool_maskrust-analyzer cargo palette 0.7.6 bool_mask/::{HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#, LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#},
3 numrust-analyzer cargo palette 0.7.6 num/::{Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics#, PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp#, Realrust-analyzer cargo palette 0.7.6 num/Real#},
4 };
5
6 /// A trait for calculating relative contrast between two colors.
7 ///
8 /// W3C's Web Content Accessibility Guidelines (WCAG) 2.1 suggest a method
9 /// to calculate accessible contrast ratios of text and background colors for
10 /// those with low vision or color deficiencies, and for contrast of colors used
11 /// in user interface graphics objects.
12 ///
13 /// These criteria are recommendations, not hard and fast rules. Most
14 /// importantly, look at the colors in action and make sure they're clear and
15 /// comfortable to read. A pair of colors may pass contrast guidelines but still
16 /// be uncomfortable to look at. Favor readability over only satisfying the
17 /// contrast ratio metric. It is recommended to verify the contrast ratio
18 /// in the output format of the colors and not to assume the contrast ratio
19 /// remains exactly the same across color formats. The following example checks
20 /// the contrast ratio of two colors in RGB format.
21 ///
22 /// ```rust
23 /// use std::str::FromStr;
24 /// use palette::{Srgb, RelativeContrast};
25 /// # fn main() -> Result<(), palette::rgb::FromHexError> {
26 ///
27 /// // the rustdoc "DARK" theme background and text colors
28 /// let background: Srgb<f32> = Srgb::from(0x353535).into_format();
29 /// let foreground = Srgb::from_str("#ddd")?.into_format();
30 ///
31 /// assert!(background.has_enhanced_contrast_text(foreground));
32 /// # Ok(())
33 /// # }
34 /// ```
35 ///
36 /// The possible range of contrast ratios is from 1:1 to 21:1. There is a
37 /// Success Criterion for Contrast (Minimum) and a Success Criterion for
38 /// Contrast (Enhanced), SC 1.4.3 and SC 1.4.6 respectively, which are concerned
39 /// with text and images of text. SC 1.4.11 is a Success Criterion for "non-text
40 /// contrast" such as user interface components and other graphics. The relative
41 /// contrast is calculated by `(L1 + 0.05) / (L2 + 0.05)`, where `L1` is the
42 /// luminance of the brighter color and `L2` is the luminance of the darker
43 /// color both in sRGB linear space. A higher contrast ratio is generally
44 /// desirable.
45 ///
46 /// For more details, visit the following links:
47 ///
48 /// [Success Criterion 1.4.3 Contrast (Minimum) (Level AA)](https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum)
49 ///
50 /// [Success Criterion 1.4.6 Contrast (Enhanced) (Level AAA)](https://www.w3.org/WAI/WCAG21/Understanding/contrast-enhanced)
51 ///
52 /// [Success Criterion 1.4.11 Non-text Contrast (Level AA)](https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html)
53 #[doc(alias = "wcag")]
54 #[deprecated(
55 since = "0.7.2",
56 note = "replaced by `palette::color_difference::Wcag21RelativeContrast`"
57 )]
58 pub trait RelativeContrastrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
59 /// The type of the contrast ratio.
60 type Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar#: Realrust-analyzer cargo palette 0.7.6 num/Real# + PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp#;
61
62 /// Calculate the contrast ratio between two colors.
63 #[must_use]
64 fn get_contrast_ratiorust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#get_contrast_ratio().(self, other: Self) -> Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar#;
65
66 /// Verify the contrast between two colors satisfies SC 1.4.3. Contrast
67 /// is at least 4.5:1 (Level AA).
68 #[must_use]
69 #[inline]
70 fn has_min_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_text().(self, other: Self) -> <Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar# as HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#>::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# {
71 self.get_contrast_ratiorust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#get_contrast_ratio().(other)
72 .gt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#gt_eq().(&Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar#::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(4.5))
73 }
74
75 /// Verify the contrast between two colors satisfies SC 1.4.3 for large
76 /// text. Contrast is at least 3:1 (Level AA).
77 #[must_use]
78 #[inline]
79 fn has_min_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_large_text().(self, other: Self) -> <Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar# as HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#>::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# {
80 self.get_contrast_ratiorust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#get_contrast_ratio().(other)
81 .gt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#gt_eq().(&Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar#::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(3.0))
82 }
83
84 /// Verify the contrast between two colors satisfies SC 1.4.6. Contrast
85 /// is at least 7:1 (Level AAA).
86 #[must_use]
87 #[inline]
88 fn has_enhanced_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_enhanced_contrast_text().(self, other: Self) -> <Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar# as HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#>::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# {
89 self.get_contrast_ratiorust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#get_contrast_ratio().(other)
90 .gt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#gt_eq().(&Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar#::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(7.0))
91 }
92
93 /// Verify the contrast between two colors satisfies SC 1.4.6 for large
94 /// text. Contrast is at least 4.5:1 (Level AAA).
95 #[must_use]
96 #[inline]
97 fn has_enhanced_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_enhanced_contrast_large_text().(self, other: Self) -> <Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar# as HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#>::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# {
98 self.has_min_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_text().(other)
99 }
100
101 /// Verify the contrast between two colors satisfies SC 1.4.11 for graphical
102 /// objects. Contrast is at least 3:1 (Level AA).
103 #[must_use]
104 #[inline]
105 fn has_min_contrast_graphicsrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_graphics().(self, other: Self) -> <Self::Scalarrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#Scalar# as HasBoolMaskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#>::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask# {
106 self.has_min_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_large_text().(other)
107 }
108 }
109
110 /// Calculate the ratio between two `luma` values.
111 #[inline]
112 #[deprecated(
113 since = "0.7.2",
114 note = "replaced by `LinLuma::relative_contrast`, via `Wcag21RelativeContrast`"
115 )]
116 pub fn contrast_ratiorust-analyzer cargo palette 0.7.6 relative_contrast/contrast_ratio().<T>(luma1: T, luma2: T) -> T
117 where
118 T: Realrust-analyzer cargo palette 0.7.6 num/Real# + Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# + PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp#,
119 T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#: LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T>,
120 {
121 lazy_selectrust-analyzer cargo palette 0.7.6 macros/lazy_select/lazy_select!! {
122 if luma1.gtrust-analyzer cargo palette 0.7.6 num/PartialCmp#gt().(&luma2) => (T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(0.05) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). &luma1) /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). (T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(0.05) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). &luma2),
123 else => (T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(0.05) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). &luma2) /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). (T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(0.05) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). &luma1)
124 }
125 }
126
127 #[cfg(feature = "approx")]
128 #[cfg(test)]
129 #[allow(deprecated)]
130 mod testrust-analyzer cargo palette 0.7.6 relative_contrast/test/ {
131 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
132
133 use craterust-analyzer cargo palette 0.7.6 crate/::RelativeContrastrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#;
134 use craterust-analyzer cargo palette 0.7.6 crate/::Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#;
135
136 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
137 fn relative_contrastrust-analyzer cargo palette 0.7.6 relative_contrast/test/relative_contrast().() {
138 let white = Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(1.0f32, 1.0, 1.0);
139 let black = Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::newrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]new().(0.0, 0.0, 0.0);
140
141 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(white.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(white), 1.0);
142 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(white.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(black), 21.0);
143 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(
144 white.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(black),
145 black.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(white)
146 );
147
148 let c1 = Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::from_strrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, u8>`][FromStr]from_str().("#600").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().into_formatrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]into_format().();
149
150 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(c1.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(white), 13.41, epsilonrust-analyzer cargo approx 0.5.1 impl#[`Relative<A, B>`]epsilon(). = 0.01);
151 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(c1.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(black), 1.56, epsilonrust-analyzer cargo approx 0.5.1 impl#[`Relative<A, B>`]epsilon(). = 0.01);
152
153 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c1.has_min_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_text().(white));
154 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c1.has_min_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_large_text().(white));
155 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c1.has_enhanced_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_enhanced_contrast_text().(white));
156 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c1.has_enhanced_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_enhanced_contrast_large_text().(white));
157 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(c1.has_min_contrast_graphicsrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_graphics().(white));
158
159 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c1.has_min_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_text().(black));
160 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c1.has_min_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_large_text().(black));
161 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c1.has_enhanced_contrast_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_enhanced_contrast_text().(black));
162 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c1.has_enhanced_contrast_large_textrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_enhanced_contrast_large_text().(black));
163 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c1.has_min_contrast_graphicsrust-analyzer cargo palette 0.7.6 relative_contrast/RelativeContrast#has_min_contrast_graphics().(black));
164
165 let c1 = Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::from_strrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, u8>`][FromStr]from_str().("#066").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().into_formatrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]into_format().();
166
167 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(c1.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(white), 6.79, epsilonrust-analyzer cargo approx 0.5.1 impl#[`Relative<A, B>`]epsilon(). = 0.01);
168 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(c1.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(black), 3.09, epsilonrust-analyzer cargo approx 0.5.1 impl#[`Relative<A, B>`]epsilon(). = 0.01);
169
170 let c1 = Srgbrust-analyzer cargo palette 0.7.6 rgb/Srgb#::from_strrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, u8>`][FromStr]from_str().("#9f9").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().into_formatrust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`]into_format().();
171
172 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(c1.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(white), 1.22, epsilonrust-analyzer cargo approx 0.5.1 impl#[`Relative<A, B>`]epsilon(). = 0.01);
173 assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(c1.get_contrast_ratiorust-analyzer cargo palette 0.7.6 rgb/rgb/impl#[`Rgb<S, T>`][RelativeContrast]get_contrast_ratio().(black), 17.11, epsilonrust-analyzer cargo approx 0.5.1 impl#[`Relative<A, B>`]epsilon(). = 0.01);
174 }
175 }