| 1 | use crate::angle::{AngleEq, HalfRotation, RealAngle, SignedAngle}; |
| 2 | use crate::num::{One, Zero}; |
| 3 | use crate::visual::{VisualColor, VisuallyEqual}; |
| 4 | use crate::{HasBoolMask, Okhsl, OklabHue}; |
| 5 | use approx::AbsDiffEq; |
| 6 | use std::borrow::Borrow; |
| 7 | use std::ops::{Mul, Neg, Sub}; |
| 8 | |
| 9 | impl<T> VisualColor<T> for Okhsl<T> |
| 10 | where |
| 11 | T: PartialOrd |
| 12 | + HasBoolMask<Mask = bool> |
| 13 | + AbsDiffEq<Epsilon = T> |
| 14 | + One |
| 15 | + Zero |
| 16 | + Neg<Output = T>, |
| 17 | T::Epsilon: Clone, |
| 18 | OklabHue<T>: AbsDiffEq<Epsilon = T::Epsilon>, |
| 19 | { |
| 20 | /// Returns true, if `saturation == 0` |
| 21 | fn is_grey(&self, epsilon: T::Epsilon) -> bool { |
| 22 | debug_assert!(self.saturation >= -epsilon.clone()); |
| 23 | self.saturation.abs_diff_eq(&T::zero(), epsilon) |
| 24 | } |
| 25 | |
| 26 | /// Returns true, if `Self::is_grey` && `lightness >= 1`, |
| 27 | /// i.e. the color's hue is irrelevant **and** it is at or beyond the |
| 28 | /// `sRGB` maximum luminance. A color at or beyond maximum brightness isn't |
| 29 | /// necessarily white. It may also be a bright shining hue. |
| 30 | fn is_white(&self, epsilon: T::Epsilon) -> bool { |
| 31 | self.is_grey(epsilon.clone()) && self.lightness > T::one() |
| 32 | || self.lightness.abs_diff_eq(&T::one(), epsilon) |
| 33 | } |
| 34 | |
| 35 | /// Returns true if `lightness == 0` |
| 36 | fn is_black(&self, epsilon: T::Epsilon) -> bool { |
| 37 | debug_assert!(self.lightness >= -epsilon.clone()); |
| 38 | self.lightness <= epsilon |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl<S, O, T> VisuallyEqual<O, S, T> for Okhsl<T> |
| 43 | where |
| 44 | T: PartialOrd |
| 45 | + HasBoolMask<Mask = bool> |
| 46 | + RealAngle |
| 47 | + SignedAngle |
| 48 | + Zero |
| 49 | + One |
| 50 | + AngleEq<Mask = bool> |
| 51 | + Sub<Output = T> |
| 52 | + AbsDiffEq<Epsilon = T> |
| 53 | + Neg<Output = T> |
| 54 | + Clone, |
| 55 | T::Epsilon: Clone + HalfRotation + Mul<Output = T::Epsilon>, |
| 56 | S: Borrow<Self> + Copy, |
| 57 | O: Borrow<Self> + Copy, |
| 58 | { |
| 59 | fn visually_eq(s: S, o: O, epsilon: T::Epsilon) -> bool { |
| 60 | VisuallyEqual::both_black_or_both_white(s, o, epsilon.clone()) |
| 61 | || VisuallyEqual::both_greyscale(s, o, epsilon.clone()) |
| 62 | && s.borrow() |
| 63 | .lightness |
| 64 | .abs_diff_eq(&o.borrow().lightness, epsilon.clone()) |
| 65 | || s.borrow().hue.abs_diff_eq(&o.borrow().hue, epsilon.clone()) |
| 66 | && s.borrow() |
| 67 | .saturation |
| 68 | .abs_diff_eq(&o.borrow().saturation, epsilon.clone()) |
| 69 | && s.borrow() |
| 70 | .lightness |
| 71 | .abs_diff_eq(&o.borrow().lightness, epsilon) |
| 72 | } |
| 73 | } |