| 1 | use crate::angle::{AngleEq, HalfRotation, RealAngle, SignedAngle}; |
| 2 | use crate::num::{Arithmetics, One, Zero}; |
| 3 | use crate::visual::{VisualColor, VisuallyEqual}; |
| 4 | use crate::{HasBoolMask, Okhwb, OklabHue}; |
| 5 | use approx::AbsDiffEq; |
| 6 | use std::borrow::Borrow; |
| 7 | |
| 8 | impl<T> VisualColor<T> for Okhwb<T> |
| 9 | where |
| 10 | T: PartialOrd + HasBoolMask<Mask = bool> + AbsDiffEq<Epsilon = T> + One + Zero + Arithmetics, |
| 11 | T::Epsilon: Clone, |
| 12 | OklabHue<T>: AbsDiffEq<Epsilon = T::Epsilon>, |
| 13 | { |
| 14 | /// Returns `true`, if `self.blackness + self.whiteness >= 1`, |
| 15 | /// assuming (but not asserting) that neither |
| 16 | /// `blackness` nor `whiteness` can be negative. |
| 17 | fn is_grey(&self, epsilon: T::Epsilon) -> bool { |
| 18 | let wb_sum = self.blackness.clone() + self.whiteness.clone(); |
| 19 | wb_sum > T::one() || wb_sum.abs_diff_eq(&T::one(), epsilon) |
| 20 | } |
| 21 | |
| 22 | /// Returns `true`, if `Self::is_grey && blackness == 0`, |
| 23 | /// i.e. the color's hue is irrelevant **and** the color contains |
| 24 | /// no black component it must be white. |
| 25 | fn is_white(&self, epsilon: T::Epsilon) -> bool { |
| 26 | self.is_grey(epsilon.clone()) && self.blackness < epsilon |
| 27 | } |
| 28 | |
| 29 | /// Returns `true` if `Self::is_grey && whiteness == 0` |
| 30 | fn is_black(&self, epsilon: T::Epsilon) -> bool { |
| 31 | self.is_grey(epsilon.clone()) && self.whiteness < epsilon |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<S, O, T> VisuallyEqual<O, S, T> for Okhwb<T> |
| 36 | where |
| 37 | T: PartialOrd |
| 38 | + HasBoolMask<Mask = bool> |
| 39 | + RealAngle |
| 40 | + SignedAngle |
| 41 | + Zero |
| 42 | + One |
| 43 | + AngleEq<Mask = bool> |
| 44 | + Arithmetics |
| 45 | + AbsDiffEq<Epsilon = T> |
| 46 | + Clone, |
| 47 | T::Epsilon: Clone + HalfRotation, |
| 48 | S: Borrow<Self> + Copy, |
| 49 | O: Borrow<Self> + Copy, |
| 50 | { |
| 51 | fn visually_eq(s: S, o: O, epsilon: T::Epsilon) -> bool { |
| 52 | VisuallyEqual::both_black_or_both_white(s, o, epsilon.clone()) |
| 53 | || VisuallyEqual::both_greyscale(s, o, epsilon.clone()) |
| 54 | && s.borrow() |
| 55 | .whiteness |
| 56 | .abs_diff_eq(&o.borrow().whiteness, epsilon.clone()) |
| 57 | && s.borrow() |
| 58 | .blackness |
| 59 | .abs_diff_eq(&o.borrow().blackness, epsilon.clone()) |
| 60 | || s.borrow().hue.abs_diff_eq(&o.borrow().hue, epsilon.clone()) |
| 61 | && s.borrow() |
| 62 | .blackness |
| 63 | .abs_diff_eq(&o.borrow().blackness, epsilon.clone()) |
| 64 | && s.borrow() |
| 65 | .whiteness |
| 66 | .abs_diff_eq(&o.borrow().whiteness, epsilon) |
| 67 | } |
| 68 | } |