colorsys/common/
approx.rs

1use crate::common::f64_abs;
2use crate::units::GetColorUnits;
3
4/// Default precision used for color comparison.
5/// It is `0.000_000_001`
6pub static DEFAULT_APPROX_EQ_PRECISION: f64 = 1e-9;
7
8/// Methods to compare two colors
9pub trait ApproxEq<T> {
10  fn approx_eq(&self, other: &T) -> bool;
11  fn approx_eq_clarify(&self, other: &T, precision: f64) -> bool;
12}
13
14pub fn approx(x: f64, y: f64, precision: f64) -> bool {
15  f64_abs(x - y) < precision
16}
17
18impl<T> ApproxEq<T> for T where T: GetColorUnits {
19  fn approx_eq(&self, other: &T) -> bool {
20    self.get_units().approx_eq(other.get_units())
21  }
22  fn approx_eq_clarify(&self, other: &T, precision: f64) -> bool {
23    self.get_units().approx_eq_clarify(other.get_units(), precision)
24  }
25}