Trait scarlet::colorpoint::ColorPoint
source · pub trait ColorPoint: Color + Into<Coord> + From<Coord> + Clone + Copy {
// Provided methods
fn euclidean_distance(self, other: Self) -> f64 { ... }
fn weighted_midpoint(self, other: Self, weight: f64) -> Self { ... }
fn midpoint(self, other: Self) -> Self { ... }
fn weighted_average(
self,
others: Vec<Self>,
weights: Vec<f64>
) -> Result<Self, ColorCalcError> { ... }
fn average(self, others: Vec<Self>) -> Coord { ... }
fn is_imaginary(&self) -> bool { ... }
fn closest_real_color(&self) -> Self { ... }
fn gradient_scale(&self, other: &Self, n: usize) -> Vec<Self> { ... }
fn gradient(&self, other: &Self) -> Box<dyn Fn(f64) -> Self> { ... }
fn cbrt_gradient(&self, other: &Self) -> Box<dyn Fn(f64) -> Self> { ... }
fn padded_gradient(
&self,
other: &Self,
lower_pad: f64,
upper_pad: f64
) -> Box<dyn Fn(f64) -> Self> { ... }
}
Expand description
A trait that indicates that the current Color can be embedded in 3D space. This also requires
Clone
and Copy
: there shouldn’t be any necessary information outside of the coordinate data.
Provided Methods§
sourcefn euclidean_distance(self, other: Self) -> f64
fn euclidean_distance(self, other: Self) -> f64
Gets the Euclidean distance between these two points when embedded in 3D space. This should
not be used as an analog of color similarity: use the distance()
function for
that.
sourcefn weighted_midpoint(self, other: Self, weight: f64) -> Self
fn weighted_midpoint(self, other: Self, weight: f64) -> Self
Gets the weighted midpoint of two colors in a space as a new
Color
. This is defined as the color corresponding to the point
along the line segment connecting the two points such that the distance to the second point
is the weight, which for most applications needs to be between 0 and 1. For example, a
weight of 0.9 would make the midpoint one-tenth as much affected by the second points as the
first.
sourcefn midpoint(self, other: Self) -> Self
fn midpoint(self, other: Self) -> Self
Like weighted_midpoint
, but with weight = 0.5
: essentially, the
Color
representing the midpoint of the two inputs in 3D space.
sourcefn weighted_average(
self,
others: Vec<Self>,
weights: Vec<f64>
) -> Result<Self, ColorCalcError>
fn weighted_average( self, others: Vec<Self>, weights: Vec<f64> ) -> Result<Self, ColorCalcError>
Returns the weighted average of a given set of colors. Weights will be normalized so that they sum to 1. Each component of the final value will be calculated by summing the components of each of the input colors multiplied by their given weight.
Errors
Returns ColorCalcError::MismatchedWeights
if the number of colors (self
and anything in
others
) and the number of weights mismatch.
sourcefn average(self, others: Vec<Self>) -> Coord
fn average(self, others: Vec<Self>) -> Coord
Returns the arithmetic mean of a given set of colors. Equivalent to weighted_average
in the
case where each weight is the same.
sourcefn is_imaginary(&self) -> bool
fn is_imaginary(&self) -> bool
Returns true
if the color is outside the range of human vision. Uses the CIE 1931 standard
observer spectral data.
sourcefn closest_real_color(&self) -> Self
fn closest_real_color(&self) -> Self
Returns the closest color that can be seen by the human eye. If the color is not imaginary, returns itself.
sourcefn gradient_scale(&self, other: &Self, n: usize) -> Vec<Self>
fn gradient_scale(&self, other: &Self, n: usize) -> Vec<Self>
Returns a Vector of colors that starts with this color, ends with the given other color, and
evenly transitions between colors. The given n
is the number of additional colors to add.
sourcefn gradient(&self, other: &Self) -> Box<dyn Fn(f64) -> Self>
fn gradient(&self, other: &Self) -> Box<dyn Fn(f64) -> Self>
Returns a pointer to a function that maps floating-point values from 0 to 1 to colors, such
that 0 returns self
, 1 returns other
, and anything in between returns a mix (calculated
linearly). Although it is possible to extrapolate outside of the range [0, 1], this is not
a guarantee and may change without warning. For more fine-grained control of gradients, see
the GradientColorMap
struct.
Examples
use scarlet::color::RGBColor;
use scarlet::colorpoint::ColorPoint;
let start = RGBColor::from_hex_code("#11457c").unwrap();
let end = RGBColor::from_hex_code("#774bdc").unwrap();
let grad = start.gradient(&end);
let color_at_start = grad(0.).to_string(); // #11457C
let color_at_end = grad(1.).to_string(); // #774BDC
let color_at_third = grad(2./6.).to_string(); // #33479C
sourcefn cbrt_gradient(&self, other: &Self) -> Box<dyn Fn(f64) -> Self>
fn cbrt_gradient(&self, other: &Self) -> Box<dyn Fn(f64) -> Self>
Returns a pointer to a function that maps floating-point values from 0 to 1 to colors, such
that 0 returns self
, 1 returns other
, and anything in between returns a mix (calculated
by the cube root of the given value). Although it is possible to extrapolate outside of the
range [0, 1], this is not a guarantee and may change without warning. For more fine-grained
control of gradients, see the GradientColorMap
struct.
Examples
use scarlet::color::RGBColor;
use scarlet::colorpoint::ColorPoint;
let start = RGBColor::from_hex_code("#11457c").unwrap();
let end = RGBColor::from_hex_code("#774bdc").unwrap();
let grad = start.cbrt_gradient(&end);
let color_at_start = grad(0.).to_string(); // #11457C
let color_at_end = grad(1.).to_string(); // #774BDC
let color_at_third = grad(2./6.).to_string(); // #5849BF
sourcefn padded_gradient(
&self,
other: &Self,
lower_pad: f64,
upper_pad: f64
) -> Box<dyn Fn(f64) -> Self>
fn padded_gradient( &self, other: &Self, lower_pad: f64, upper_pad: f64 ) -> Box<dyn Fn(f64) -> Self>
Returns a pointer to a function that maps floating-point values from 0 to 1 to colors with
padding lower_pad
and upper_pad
such that an input of 0 returns the gradient at
lower_pad
, an input of 1 returns the gradient at upper_pad
, and values in-between are
mapped linearly inside that range. For more fine-grained control over gradients, see the
GradientColorMap
struct.
Examples
use scarlet::color::RGBColor;
use scarlet::colorpoint::ColorPoint;
let start = RGBColor::from_hex_code("#11457c").unwrap();
let end = RGBColor::from_hex_code("#774bdc").unwrap();
// the following would be equivalent to start.gradient(&end);
let normal_grad = start.padded_gradient(&end, 0., 1.);
let padded_grad = start.padded_gradient(&end, 1. / 6., 5. / 6.);
// 0.25 is 1/4 of the way between 1/6 and 5/6, so it's equivalent to a 2/6 call
assert_eq!(padded_grad(0.25).to_string(), normal_grad(1./3.).to_string());