pub trait ColorDistance<T>: Sized + Copy{
// Provided methods
fn close_to(&self, other: T, tolerance: f64) -> bool { ... }
fn far_from(&self, other: T, tolerance: f64) -> bool { ... }
fn delta_e(&self, other: T) -> f64 { ... }
}
Expand description
Trait for calculating perceptual color distance between colors.
This trait provides methods to determine if two colors are perceptually close using the CIE Delta E distance calculation in Lab color space.
Provided Methods§
Sourcefn close_to(&self, other: T, tolerance: f64) -> bool
fn close_to(&self, other: T, tolerance: f64) -> bool
Compares self
to other
via delta_e, checking that the result is less than or equal
to tolerance
.
other
must implement Into<Lab>
Tolerance values:
- 0.0-1.0: Not perceptible by human eye
- 1.0-2.0: Perceptible through close observation
- 2.0-10.0: Perceptible at a glance
- 11.0-49.0: Colors are more similar than opposite
- 100.0: Colors are exact opposites
§Example
use chromashift::{Srgb, ColorDistance};
let red1 = Srgb::new(255, 0, 0, 100.0);
let red2 = Srgb::new(254, 1, 1, 100.0);
assert!(red1.close_to(red2, 2.0));
Sourcefn far_from(&self, other: T, tolerance: f64) -> bool
fn far_from(&self, other: T, tolerance: f64) -> bool
Compares self
to other
via delta_e, checking that the result is greater than or
equal to tolerance
.
other
must implement Into<Lab>
Tolerance values:
- 0.0-1.0: Not perceptible by human eye
- 1.0-2.0: Perceptible through close observation
- 2.0-10.0: Perceptible at a glance
- 11.0-49.0: Colors are more similar than opposite
- 100.0: Colors are exact opposites
§Example
use chromashift::{Srgb, ColorDistance};
let red = Srgb::new(255, 0, 0, 100.0);
let blue = Srgb::new(0, 0, 255, 100.0);
assert!(red.far_from(blue, 10.0));
Sourcefn delta_e(&self, other: T) -> f64
fn delta_e(&self, other: T) -> f64
This uses the CIEDE2000 Delta E formula difference between self
and other
.
other
must implement Into<Lab>
Tolerance values:
- 0.0-1.0: Not perceptible by human eye
- 1.0-2.0: Perceptible through close observation
- 2.0-10.0: Perceptible at a glance
- 11.0-49.0: Colors are more similar than opposite
- 100.0: Colors are exact opposites
§Example
use chromashift::{Srgb, Named, ColorDistance};
let red = Srgb::new(255, 0, 0, 100.0);
let green = Srgb::new(0, 255, 0, 100.0);
assert_eq!(red.delta_e(green).round(), 84.0);
assert_eq!(Named::Black.delta_e(Named::White).round(), 100.0);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.