pub trait FromColor<T> {
    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.

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:

  • FromColor and IntoColor are allowed to be lossy, meaning converting A -> B -> A may result in a different value than the original. This applies to A -> A as well.
  • From<Self> and Into<Self> are blanket implemented, while FromColor<Self> and IntoColor<Self> have to be manually implemented. This allows additional flexibility, such as allowing implementing FromColor<Rgb<S2, T>> for Rgb<S1, T>.
  • Implementing FromColorUnclamped and Clamp is enough to get all the other conversion traits, while From and Into would 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

Convert from T with values clamped to the color defined bounds.

use palette::{Clamp, FromColor, Lch, Srgb};

let rgb = Srgb::from_color(Lch::new(50.0, 100.0, -175.0));