| 1 | use approx::AbsDiffEq; |
| 2 | use core::borrow::Borrow; |
| 3 | |
| 4 | /// Methods to tell the tell about the visual characteristics of the color |
| 5 | pub trait VisualColor<T> |
| 6 | where |
| 7 | T: AbsDiffEq<Epsilon = T>, |
| 8 | { |
| 9 | /// Returns `true`, if the color is grey within the bounds of `epsilon`-tolerance, |
| 10 | /// i.e. visually only the luminance value matters, not the color's hue. |
| 11 | fn is_grey(&self, epsilon: T::Epsilon) -> bool; |
| 12 | |
| 13 | /// Returns `true`, if the color is white within the bounds of `epsilon`-tolerance, |
| 14 | /// i.e. the color's hue is irrelevant **and** it is at or beyond the |
| 15 | /// `sRGB` maximum brightness. |
| 16 | /// A color that is *only* at or beyond maximum brightness isn't |
| 17 | /// necessarily white. It may also be a bright shining hue. |
| 18 | fn is_white(&self, epsilon: T::Epsilon) -> bool; |
| 19 | |
| 20 | /// Returns `true`, if the color is black within the bounds of `epsilon`-tolerance. |
| 21 | fn is_black(&self, epsilon: T::Epsilon) -> bool; |
| 22 | } |
| 23 | |
| 24 | /// Methods to compare visual characteristics of two colors |
| 25 | pub trait VisuallyEqual<O, S, T>: VisualColor<T> |
| 26 | where |
| 27 | T: AbsDiffEq<Epsilon = T> + Clone, |
| 28 | S: Borrow<Self>, |
| 29 | O: Borrow<Self>, |
| 30 | { |
| 31 | /// Returns true, if `self` and `other` are either both white or both black |
| 32 | fn both_black_or_both_white(s: S, o: O, epsilon: T::Epsilon) -> bool { |
| 33 | s.borrow().is_white(epsilon.clone()) && o.borrow().is_white(epsilon.clone()) |
| 34 | || s.borrow().is_black(epsilon.clone()) && o.borrow().is_black(epsilon) |
| 35 | } |
| 36 | |
| 37 | /// Returns true, if `self` and `other` are both fully desaturated |
| 38 | fn both_greyscale(s: S, o: O, epsilon: T::Epsilon) -> bool { |
| 39 | s.borrow().is_grey(epsilon.clone()) && o.borrow().is_grey(epsilon) |
| 40 | } |
| 41 | fn visually_eq(s: S, o: O, epsilon: T::Epsilon) -> bool; |
| 42 | } |