pub trait Bound: Color + ColorPoint {
// Required method
fn bounds() -> [(f64, f64); 3];
// Provided methods
fn clamp_coord(point: Coord) -> Coord { ... }
fn clamp<T: ColorPoint>(color: T) -> T { ... }
}
Expand description
Describes a color space in which the total space of representable colors has explicit bounds
besides those imposed by human vision. For example, an sRGB color can’t have negative values for
any of its components, whereas the CIELAB space can feasibly describe even those colors that
cannot be viewed by humans. This only applies to colors that can be embedded in 3D space, hence
the use of the ColorPoint
trait as a dependency.
For more, check out this guide.
Example
Bound a clearly-problematic color within sRGB.
let out_of_bounds = CIELABColor{l: 1., a: 150., b: -150.};
let in_bounds: RGBColor = RGBColor::clamp(out_of_bounds).convert();
let still_in_bounds: RGBColor = RGBColor::clamp(in_bounds).convert();
assert_eq!(in_bounds.to_string(), still_in_bounds.to_string());
let in_bounds_lab: CIELABColor = in_bounds.convert();
println!("{} {} {}", in_bounds_lab.l, in_bounds_lab.a, in_bounds_lab.b);
// prints 27.024908432754984 64.48329922444846 -105.76675512389784
// notice difference from before: also, note how every component changes to find the closest match
Required Methods§
sourcefn bounds() -> [(f64, f64); 3]
fn bounds() -> [(f64, f64); 3]
Returns an array [(min1, max1), (min2, max2), (min3, max3)]
that represents the bounds on each
component of the color space, in the order that they appear in the Coord representation. If
some parts of the bounds don’t exist, using infinity or negative infinity works.
Provided Methods§
sourcefn clamp_coord(point: Coord) -> Coord
fn clamp_coord(point: Coord) -> Coord
Given a Coord, returns a Coord such that each component has been clamped to the correct bounds. See trait documentation for example usage.
sourcefn clamp<T: ColorPoint>(color: T) -> T
fn clamp<T: ColorPoint>(color: T) -> T
Given a Color that can be embedded in 3D space, returns a new version of that color that is in the bounds of this color space, even if the coordinate systems of the two spaces differ. If the color is already in the gamut, it simply returns a copy. See trait documentation for example usage.