pub trait FromColor<T>: Sized {
// Required method
fn from_color(t: T) -> Self;
}Expand description
A trait for converting one color from another, in a possibly lossy way.
U: FromColor<T> is implemented for every type U: FromColorUnclamped<T> + Clamp, as well as for Vec<T> and Box<[T]> where T and U have the
same memory layout.
See FromColorUnclamped for a
lossless version of this trait. See
TryFromColor for a trait that gives an
error when the result is out of bounds.
§The Difference Between FromColor and From
The conversion traits, including FromColor, were added to gain even more
flexibility than what From and the other standard library traits can give.
There are a few subtle, but important, differences in their semantics:
FromColorandIntoColorare allowed to be lossy, meaning convertingA -> B -> Amay result in a different value than the original. This applies toA -> Aas well.From<Self>andInto<Self>are blanket implemented, whileFromColor<Self>andIntoColor<Self>have to be manually implemented. This allows additional flexibility, such as allowing implementingFromColor<Rgb<S2, T>> for Rgb<S1, T>.- Implementing
FromColorUnclamped,IsWithinBoundsandClampis enough to get all the other conversion traits, whileFromandIntowould not be possible to blanket implement in the same way. This also reduces the work that needs to be done by macros.
See the convert module for how to implement
FromColorUnclamped for custom colors.
Required Methods§
Sourcefn from_color(t: T) -> Self
fn from_color(t: T) -> Self
Convert from T with values clamped to the color defined bounds.
use palette::{IsWithinBounds, FromColor, Lch, Srgb};
let rgb = Srgb::from_color(Lch::new(50.0f32, 100.0, -175.0));
assert!(rgb.is_within_bounds());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.
Implementations on Foreign Types§
Source§impl<T, U> FromColor<Box<[T]>> for Box<[U]>
impl<T, U> FromColor<Box<[T]>> for Box<[U]>
Source§fn from_color(color: Box<[T]>) -> Self
fn from_color(color: Box<[T]>) -> Self
Convert all colors in place, without reallocating.
use palette::{convert::FromColor, SaturateAssign, Srgb, Lch};
let srgb = vec![Srgb::new(0.8f32, 1.0, 0.2), Srgb::new(0.9, 0.1, 0.3)].into_boxed_slice();
let mut lch = Box::<[Lch]>::from_color(srgb);
lch.saturate_assign(0.1);
let srgb = Box::<[Srgb]>::from_color(lch);Source§impl<T, U> FromColor<Vec<T>> for Vec<U>
impl<T, U> FromColor<Vec<T>> for Vec<U>
Source§fn from_color(color: Vec<T>) -> Self
fn from_color(color: Vec<T>) -> Self
Convert all colors in place, without reallocating.
use palette::{convert::FromColor, SaturateAssign, Srgb, Lch};
let srgb = vec![Srgb::new(0.8f32, 1.0, 0.2), Srgb::new(0.9, 0.1, 0.3)];
let mut lch = Vec::<Lch>::from_color(srgb);
lch.saturate_assign(0.1);
let srgb = Vec::<Srgb>::from_color(lch);