use crate::{
bool_mask::{HasBoolMask, LazySelect},
num::{Arithmetics, PartialCmp, Real},
};
#[doc(alias = "wcag")]
#[deprecated(
since = "0.7.2",
note = "replaced by `palette::color_difference::Wcag21RelativeContrast`"
)]
pub trait RelativeContrast: Sized {
type Scalar: Real + PartialCmp;
#[must_use]
fn get_contrast_ratio(self, other: Self) -> Self::Scalar;
#[must_use]
#[inline]
fn has_min_contrast_text(self, other: Self) -> <Self::Scalar as HasBoolMask>::Mask {
self.get_contrast_ratio(other)
.gt_eq(&Self::Scalar::from_f64(4.5))
}
#[must_use]
#[inline]
fn has_min_contrast_large_text(self, other: Self) -> <Self::Scalar as HasBoolMask>::Mask {
self.get_contrast_ratio(other)
.gt_eq(&Self::Scalar::from_f64(3.0))
}
#[must_use]
#[inline]
fn has_enhanced_contrast_text(self, other: Self) -> <Self::Scalar as HasBoolMask>::Mask {
self.get_contrast_ratio(other)
.gt_eq(&Self::Scalar::from_f64(7.0))
}
#[must_use]
#[inline]
fn has_enhanced_contrast_large_text(self, other: Self) -> <Self::Scalar as HasBoolMask>::Mask {
self.has_min_contrast_text(other)
}
#[must_use]
#[inline]
fn has_min_contrast_graphics(self, other: Self) -> <Self::Scalar as HasBoolMask>::Mask {
self.has_min_contrast_large_text(other)
}
}
#[inline]
#[deprecated(
since = "0.7.2",
note = "replaced by `LinLuma::relative_contrast`, via `Wcag21RelativeContrast`"
)]
pub fn contrast_ratio<T>(luma1: T, luma2: T) -> T
where
T: Real + Arithmetics + PartialCmp,
T::Mask: LazySelect<T>,
{
lazy_select! {
if luma1.gt(&luma2) => (T::from_f64(0.05) + &luma1) / (T::from_f64(0.05) + &luma2),
else => (T::from_f64(0.05) + &luma2) / (T::from_f64(0.05) + &luma1)
}
}
#[cfg(feature = "approx")]
#[cfg(test)]
#[allow(deprecated)]
mod test {
use core::str::FromStr;
use crate::RelativeContrast;
use crate::Srgb;
#[test]
fn relative_contrast() {
let white = Srgb::new(1.0f32, 1.0, 1.0);
let black = Srgb::new(0.0, 0.0, 0.0);
assert_relative_eq!(white.get_contrast_ratio(white), 1.0);
assert_relative_eq!(white.get_contrast_ratio(black), 21.0);
assert_relative_eq!(
white.get_contrast_ratio(black),
black.get_contrast_ratio(white)
);
let c1 = Srgb::from_str("#600").unwrap().into_format();
assert_relative_eq!(c1.get_contrast_ratio(white), 13.41, epsilon = 0.01);
assert_relative_eq!(c1.get_contrast_ratio(black), 1.56, epsilon = 0.01);
assert!(c1.has_min_contrast_text(white));
assert!(c1.has_min_contrast_large_text(white));
assert!(c1.has_enhanced_contrast_text(white));
assert!(c1.has_enhanced_contrast_large_text(white));
assert!(c1.has_min_contrast_graphics(white));
assert!(!c1.has_min_contrast_text(black));
assert!(!c1.has_min_contrast_large_text(black));
assert!(!c1.has_enhanced_contrast_text(black));
assert!(!c1.has_enhanced_contrast_large_text(black));
assert!(!c1.has_min_contrast_graphics(black));
let c1 = Srgb::from_str("#066").unwrap().into_format();
assert_relative_eq!(c1.get_contrast_ratio(white), 6.79, epsilon = 0.01);
assert_relative_eq!(c1.get_contrast_ratio(black), 3.09, epsilon = 0.01);
let c1 = Srgb::from_str("#9f9").unwrap().into_format();
assert_relative_eq!(c1.get_contrast_ratio(white), 1.22, epsilon = 0.01);
assert_relative_eq!(c1.get_contrast_ratio(black), 17.11, epsilon = 0.01);
}
}