Struct DynamicColor
pub struct DynamicColor {
pub cs: ColorSpaceTag,
pub flags: Flags,
pub components: [f32; 4],
}Expand description
A color with a color space tag decided at runtime.
This type is roughly equivalent to AlphaColor except with a tag
for color space as opposed being determined at compile time. It can
also represent missing components, which are a feature of the CSS
Color 4 spec.
Missing components are mostly useful for interpolation, and in that
context take the value of the other color being interpolated. For
example, interpolating a color in Oklch with oklch(none 0 none)
fades the color saturation, ending in a gray with the same lightness.
In other contexts, missing colors are interpreted as a zero value. When manipulating components directly, setting them nonzero when the corresponding missing flag is set may yield unexpected results.
Fields§
§cs: ColorSpaceTagThe color space.
flags: FlagsThe state of this color, tracking whether it has missing components and how it was
constructed. See the documentation of Flags for more information.
components: [f32; 4]The components.
The first three components are interpreted according to the color space tag. The fourth component is alpha, interpreted as separate alpha.
Implementations§
§impl DynamicColor
impl DynamicColor
pub fn to_alpha_color<CS>(self) -> AlphaColor<CS>where
CS: ColorSpace,
pub fn to_alpha_color<CS>(self) -> AlphaColor<CS>where
CS: ColorSpace,
Convert to AlphaColor with a static color space.
Missing components are interpreted as 0.
pub fn from_alpha_color<CS>(color: AlphaColor<CS>) -> DynamicColorwhere
CS: ColorSpace,
pub fn from_alpha_color<CS>(color: AlphaColor<CS>) -> DynamicColorwhere
CS: ColorSpace,
Convert from AlphaColor.
pub fn convert(self, cs: ColorSpaceTag) -> DynamicColor
pub fn convert(self, cs: ColorSpaceTag) -> DynamicColor
Convert to a different color space.
pub fn convert_absolute(self, cs: ColorSpaceTag) -> DynamicColor
pub fn convert_absolute(self, cs: ColorSpaceTag) -> DynamicColor
Convert to a different color space, without chromatic adaptation.
For most use-cases you should consider using the chromatically-adapting
DynamicColor::convert instead. See the documentation on
ColorSpace::convert_absolute for more information.
pub fn chromatically_adapt(
self,
from: Chromaticity,
to: Chromaticity,
) -> DynamicColor
pub fn chromatically_adapt( self, from: Chromaticity, to: Chromaticity, ) -> DynamicColor
Chromatically adapt the color between the given white point chromaticities.
The color is assumed to be under a reference white point of from and is chromatically
adapted to the given white point to. The linear Bradford transform is used to perform the
chromatic adaptation.
pub const fn multiply_alpha(self, rhs: f32) -> DynamicColor
pub const fn multiply_alpha(self, rhs: f32) -> DynamicColor
Multiply alpha by the given factor.
If the alpha channel is missing, then the new alpha channel will be ignored and the color returned unchanged.
pub const fn with_alpha(self, alpha: f32) -> DynamicColor
pub const fn with_alpha(self, alpha: f32) -> DynamicColor
Set the alpha channel.
This replaces the existing alpha channel. To scale or
or otherwise modify the existing alpha channel, use
DynamicColor::multiply_alpha or DynamicColor::map.
If the alpha channel is missing, then the new alpha channel will be ignored and the color returned unchanged.
let c = parse_color("lavenderblush").unwrap().with_alpha(0.7);
assert_eq!(0.7, c.to_alpha_color::<Srgb>().split().1);pub fn scale_chroma(self, scale: f32) -> DynamicColor
pub fn scale_chroma(self, scale: f32) -> DynamicColor
Scale the chroma by the given amount.
See ColorSpace::scale_chroma for more details.
pub fn clip(self) -> DynamicColor
pub fn clip(self) -> DynamicColor
Clip the color’s components to fit within the natural gamut of the color space, and clamp
the color’s alpha to be in the range [0, 1].
See ColorSpace::clip for more details.
pub fn interpolate(
self,
other: DynamicColor,
cs: ColorSpaceTag,
direction: HueDirection,
) -> Interpolator
pub fn interpolate( self, other: DynamicColor, cs: ColorSpaceTag, direction: HueDirection, ) -> Interpolator
Interpolate two colors.
The colors are interpolated linearly from self to other in the color space given by
cs. When interpolating in a cylindrical color space, the hue can be interpolated in
multiple ways. The direction parameter controls the way in which the
hue is interpolated.
The interpolation proceeds according to CSS Color Module Level 4 § 12.
This method does a bunch of precomputation, resulting in an Interpolator object that
can be evaluated at various t values.
§Example
use color::{AlphaColor, ColorSpaceTag, DynamicColor, HueDirection, Srgb};
let start = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([1., 0., 0., 1.]));
let end = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([0., 1., 0., 1.]));
let interp = start.interpolate(end, ColorSpaceTag::Hsl, HueDirection::Increasing);
let mid = interp.eval(0.5);
assert_eq!(mid.cs, ColorSpaceTag::Hsl);
assert!((mid.components[0] - 60.).abs() < 0.01);pub fn interpolate_unpremultiplied(
self,
other: DynamicColor,
cs: ColorSpaceTag,
direction: HueDirection,
) -> UnpremultipliedInterpolator
pub fn interpolate_unpremultiplied( self, other: DynamicColor, cs: ColorSpaceTag, direction: HueDirection, ) -> UnpremultipliedInterpolator
Interpolate two colors without alpha premultiplication.
Similar to DynamicColor::interpolate, but colors are interpolated without premultiplying
their color channels by the alpha channel. This is almost never what you want.
This causes color information to leak out of transparent colors. For example, when interpolating from a fully transparent red to a fully opaque blue in sRGB, this method will go through an intermediate purple.
This matches behavior of gradients in the HTML canvas element.
See The 2D rendering context § Fill and stroke styles of the
HTML 2D Canvas specification.
The colors are interpolated linearly from self to other in the color space given by
cs. When interpolating in a cylindrical color space, the hue can be interpolated in
multiple ways. The direction parameter controls the way in which the
hue is interpolated.
The interpolation proceeds according to CSS Color Module Level 4 § 12.
This method does a bunch of precomputation, resulting in an UnpremultipliedInterpolator object that
can be evaluated at various t values.
§Example
use color::{AlphaColor, ColorSpaceTag, DynamicColor, HueDirection, Srgb};
let start = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([1., 0., 0., 1.]));
let end = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([0., 1., 0., 1.]));
let interp = start.interpolate_unpremultiplied(end, ColorSpaceTag::Hsl, HueDirection::Increasing);
let mid = interp.eval(0.5);
assert_eq!(mid.cs, ColorSpaceTag::Hsl);
assert!((mid.components[0] - 60.).abs() < 0.01);pub fn relative_luminance(self) -> f32
pub fn relative_luminance(self) -> f32
Compute the relative luminance of the color.
This can be useful for choosing contrasting colors, and follows the WCAG 2.1 spec.
Note that this method only considers the opaque color, not the alpha. Blending semi-transparent colors will reduce contrast, and that should also be taken into account.
pub fn map_in(
self,
cs: ColorSpaceTag,
f: impl Fn(f32, f32, f32, f32) -> [f32; 4],
) -> DynamicColor
pub fn map_in( self, cs: ColorSpaceTag, f: impl Fn(f32, f32, f32, f32) -> [f32; 4], ) -> DynamicColor
Map components in a given color space.
pub fn map_lightness(self, f: impl Fn(f32) -> f32) -> DynamicColor
pub fn map_lightness(self, f: impl Fn(f32) -> f32) -> DynamicColor
Trait Implementations§
§impl BitEq for DynamicColor
impl BitEq for DynamicColor
§fn bit_eq(&self, other: &DynamicColor) -> bool
fn bit_eq(&self, other: &DynamicColor) -> bool
§impl BitHash for DynamicColor
impl BitHash for DynamicColor
§impl Clone for DynamicColor
impl Clone for DynamicColor
§fn clone(&self) -> DynamicColor
fn clone(&self) -> DynamicColor
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for DynamicColor
impl Debug for DynamicColor
§impl Display for DynamicColor
impl Display for DynamicColor
§impl<CS> From<AlphaColor<CS>> for DynamicColor
Note that the conversion is only lossless for color spaces that have a corresponding tag.
This is why we have this additional trait bound. See also
https://github.com/linebender/color/pull/155 for more discussion.
impl<CS> From<AlphaColor<CS>> for DynamicColor
Note that the conversion is only lossless for color spaces that have a corresponding tag. This is why we have this additional trait bound. See also https://github.com/linebender/color/pull/155 for more discussion.
§fn from(value: AlphaColor<CS>) -> DynamicColor
fn from(value: AlphaColor<CS>) -> DynamicColor
§impl FromStr for DynamicColor
impl FromStr for DynamicColor
§type Err = ParseError
type Err = ParseError
§fn from_str(s: &str) -> Result<DynamicColor, <DynamicColor as FromStr>::Err>
fn from_str(s: &str) -> Result<DynamicColor, <DynamicColor as FromStr>::Err>
s to return a value of this type. Read more§impl PartialEq for DynamicColor
impl PartialEq for DynamicColor
§fn eq(&self, other: &DynamicColor) -> bool
fn eq(&self, other: &DynamicColor) -> bool
Equality is not perceptual, but requires the component values to be equal.
See also CacheKey.
impl Copy for DynamicColor
Auto Trait Implementations§
impl Freeze for DynamicColor
impl RefUnwindSafe for DynamicColor
impl Send for DynamicColor
impl Sync for DynamicColor
impl Unpin for DynamicColor
impl UnwindSafe for DynamicColor
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Filterable for T
impl<T> Filterable for T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> FuncAnim for Twhere
T: FuncRequirement + 'static,
impl<T> FuncAnim for Twhere
T: FuncRequirement + 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<T> StaticAnim for Twhere
T: StaticAnimRequirement + 'static,
impl<T> StaticAnim for Twhere
T: StaticAnimRequirement + 'static,
Source§fn show(&self) -> AnimationCell<T>
fn show(&self) -> AnimationCell<T>
Source§fn hide(&self) -> AnimationCell<T>
fn hide(&self) -> AnimationCell<T>
Source§impl<T> StrictAs for T
impl<T> StrictAs for T
Source§fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
Source§impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
Source§fn strict_cast_from(src: Src) -> Dst
fn strict_cast_from(src: Src) -> Dst
§impl<T> ToSmolStr for T
impl<T> ToSmolStr for T
fn to_smolstr(&self) -> SmolStr
§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more