Struct AlphaColor
#[repr(transparent)]pub struct AlphaColor<CS> {
pub components: [f32; 4],
pub cs: PhantomData<CS>,
}Expand description
A color with an alpha channel.
A color in a color space known at compile time, with an alpha channel.
The color channels are straight, i.e., they are not premultiplied by
the alpha channel. See PremulColor for a color type with color
channels premultiplied by the alpha channel.
See OpaqueColor for a discussion of arithmetic traits and interpolation.
Fields§
§components: [f32; 4]The components, which may be manipulated directly.
The interpretation of the first three components depends on the color space. The fourth component is separate alpha.
cs: PhantomData<CS>The color space.
Implementations§
§impl<CS> AlphaColor<CS>where
CS: ColorSpace,
impl<CS> AlphaColor<CS>where
CS: ColorSpace,
pub const BLACK: AlphaColor<CS>
pub const BLACK: AlphaColor<CS>
A black color.
More comprehensive pre-defined colors are available
in the color::palette module.
pub const TRANSPARENT: AlphaColor<CS>
pub const TRANSPARENT: AlphaColor<CS>
A transparent color.
This is a black color with full alpha.
More comprehensive pre-defined colors are available
in the color::palette module.
pub const WHITE: AlphaColor<CS>
pub const WHITE: AlphaColor<CS>
A white color.
This value is specific to the color space.
More comprehensive pre-defined colors are available
in the color::palette module.
pub const fn new(components: [f32; 4]) -> AlphaColor<CS>
pub const fn new(components: [f32; 4]) -> AlphaColor<CS>
Create a new color from the given components.
pub const fn split(self) -> (OpaqueColor<CS>, f32)
pub const fn split(self) -> (OpaqueColor<CS>, f32)
Split into opaque and alpha components.
This function is the inverse of OpaqueColor::with_alpha.
pub const fn with_alpha(self, alpha: f32) -> AlphaColor<CS>
pub const fn with_alpha(self, alpha: f32) -> AlphaColor<CS>
Set the alpha channel.
This replaces the existing alpha channel. To scale or
or otherwise modify the existing alpha channel, use
AlphaColor::multiply_alpha or AlphaColor::map.
let c = color::palette::css::GOLDENROD.with_alpha(0.5);
assert_eq!(0.5, c.split().1);pub const fn discard_alpha(self) -> OpaqueColor<CS>
pub const fn discard_alpha(self) -> OpaqueColor<CS>
Split out the opaque components, discarding the alpha.
This is a shorthand for calling split.
pub fn convert<TargetCs>(self) -> AlphaColor<TargetCs>where
TargetCs: ColorSpace,
pub fn convert<TargetCs>(self) -> AlphaColor<TargetCs>where
TargetCs: ColorSpace,
Convert a color into a different color space.
pub const fn premultiply(self) -> PremulColor<CS>
pub const fn premultiply(self) -> PremulColor<CS>
Convert a color to the corresponding premultiplied form.
pub fn lerp_rect(self, other: AlphaColor<CS>, t: f32) -> AlphaColor<CS>
pub fn lerp_rect(self, other: AlphaColor<CS>, t: f32) -> AlphaColor<CS>
Linearly interpolate colors, without hue fixup.
This method produces meaningful results in rectangular color spaces, or if hue fixup has been applied.
pub fn lerp(
self,
other: AlphaColor<CS>,
t: f32,
direction: HueDirection,
) -> AlphaColor<CS>
pub fn lerp( self, other: AlphaColor<CS>, t: f32, direction: HueDirection, ) -> AlphaColor<CS>
Linearly interpolate colors, with hue fixup if needed.
pub const fn multiply_alpha(self, rhs: f32) -> AlphaColor<CS>
pub const fn multiply_alpha(self, rhs: f32) -> AlphaColor<CS>
Multiply alpha by the given factor.
pub fn scale_chroma(self, scale: f32) -> AlphaColor<CS>
pub fn scale_chroma(self, scale: f32) -> AlphaColor<CS>
Scale the chroma by the given amount.
See ColorSpace::scale_chroma for more details.
pub fn map_in<TargetCS>(
self,
f: impl Fn(f32, f32, f32, f32) -> [f32; 4],
) -> AlphaColor<CS>where
TargetCS: ColorSpace,
pub fn map_in<TargetCS>(
self,
f: impl Fn(f32, f32, f32, f32) -> [f32; 4],
) -> AlphaColor<CS>where
TargetCS: ColorSpace,
Map components in a given color space.
pub fn map_lightness(self, f: impl Fn(f32) -> f32) -> AlphaColor<CS>
pub fn map_lightness(self, f: impl Fn(f32) -> f32) -> AlphaColor<CS>
Map the lightness of the color.
In a color space that naturally has a lightness component, map that value. Otherwise, do the mapping in Oklab. The lightness range is normalized so that 1.0 is white. That is the normal range for Oklab but differs from the range in Lab, Lch, and Hsl.
§Examples
use color::{AlphaColor, Lab};
let color = AlphaColor::<Lab>::new([40., 4., -17., 1.]);
let lighter = color.map_lightness(|l| l + 0.2);
let expected = AlphaColor::<Lab>::new([60., 4., -17., 1.]);
assert!(lighter.premultiply().difference(expected.premultiply()) < 1e-4);pub fn map_hue(self, f: impl Fn(f32) -> f32) -> AlphaColor<CS>
pub fn map_hue(self, f: impl Fn(f32) -> f32) -> AlphaColor<CS>
Map the hue of the color.
In a color space that naturally has a hue component, map that value. Otherwise, do the mapping in Oklch. The hue is in degrees.
§Examples
use color::{AlphaColor, Oklab};
let color = AlphaColor::<Oklab>::new([0.5, 0.2, -0.1, 1.]);
let complementary = color.map_hue(|h| (h + 180.) % 360.);
let expected = AlphaColor::<Oklab>::new([0.5, -0.2, 0.1, 1.]);
assert!(complementary.premultiply().difference(expected.premultiply()) < 1e-4);pub fn to_rgba8(self) -> Rgba8
pub fn to_rgba8(self) -> Rgba8
Convert the color to sRGB if not already in sRGB, and pack into 8 bit per component integer encoding.
The RGBA components are mapped from the floating point range of 0.0-1.0 to the integer
range of 0-255. Component values outside of this range are saturated to 0 or 255.
§Implementation note
This performs almost-correct rounding to be fast on both x86 and AArch64 hardware. Within the
saturated output range of this method, 0-255, there is a single color component value
where results differ: 0.0019607842. This method maps that component to integer value 1;
it would more precisely be mapped to 0.
§impl AlphaColor<Srgb>
impl AlphaColor<Srgb>
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> AlphaColor<Srgb>
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> AlphaColor<Srgb>
Create a color from 8-bit rgba values.
Note: for conversion from the Rgba8 type, just use the From trait.
Trait Implementations§
§impl<CS> Add for AlphaColor<CS>where
CS: ColorSpace,
Component-wise addition of components.
impl<CS> Add for AlphaColor<CS>where
CS: ColorSpace,
Component-wise addition of components.
§type Output = AlphaColor<CS>
type Output = AlphaColor<CS>
+ operator.§fn add(self, rhs: AlphaColor<CS>) -> AlphaColor<CS>
fn add(self, rhs: AlphaColor<CS>) -> AlphaColor<CS>
+ operation. Read more§impl<CS> BitEq for AlphaColor<CS>
impl<CS> BitEq for AlphaColor<CS>
§fn bit_eq(&self, other: &AlphaColor<CS>) -> bool
fn bit_eq(&self, other: &AlphaColor<CS>) -> bool
§impl<CS> BitHash for AlphaColor<CS>
impl<CS> BitHash for AlphaColor<CS>
§impl<CS> Clone for AlphaColor<CS>where
CS: Clone,
impl<CS> Clone for AlphaColor<CS>where
CS: Clone,
§fn clone(&self) -> AlphaColor<CS>
fn clone(&self) -> AlphaColor<CS>
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<CS> Debug for AlphaColor<CS>where
CS: Debug,
impl<CS> Debug for AlphaColor<CS>where
CS: Debug,
§impl<CS> Div<f32> for AlphaColor<CS>where
CS: ColorSpace,
Divide components by a scalar.
impl<CS> Div<f32> for AlphaColor<CS>where
CS: ColorSpace,
Divide components by a scalar.
§type Output = AlphaColor<CS>
type Output = AlphaColor<CS>
/ operator.§fn div(self, rhs: f32) -> AlphaColor<CS>
fn div(self, rhs: f32) -> AlphaColor<CS>
/ operation. Read more§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<CS> From<OpaqueColor<CS>> for AlphaColor<CS>where
CS: ColorSpace,
impl<CS> From<OpaqueColor<CS>> for AlphaColor<CS>where
CS: ColorSpace,
§fn from(value: OpaqueColor<CS>) -> AlphaColor<CS>
fn from(value: OpaqueColor<CS>) -> AlphaColor<CS>
§impl From<Rgba8> for AlphaColor<Srgb>
impl From<Rgba8> for AlphaColor<Srgb>
§fn from(value: Rgba8) -> AlphaColor<Srgb>
fn from(value: Rgba8) -> AlphaColor<Srgb>
§impl<CS> FromStr for AlphaColor<CS>where
CS: ColorSpace,
impl<CS> FromStr for AlphaColor<CS>where
CS: ColorSpace,
§type Err = ParseError
type Err = ParseError
§fn from_str(s: &str) -> Result<AlphaColor<CS>, <AlphaColor<CS> as FromStr>::Err>
fn from_str(s: &str) -> Result<AlphaColor<CS>, <AlphaColor<CS> as FromStr>::Err>
s to return a value of this type. Read moreSource§impl<CS> Interpolatable for AlphaColor<CS>where
CS: ColorSpace,
impl<CS> Interpolatable for AlphaColor<CS>where
CS: ColorSpace,
Source§fn lerp(&self, target: &AlphaColor<CS>, t: f64) -> AlphaColor<CS>
fn lerp(&self, target: &AlphaColor<CS>, t: f64) -> AlphaColor<CS>
§impl<CS> Mul<f32> for AlphaColor<CS>where
CS: ColorSpace,
Multiply components by a scalar.
impl<CS> Mul<f32> for AlphaColor<CS>where
CS: ColorSpace,
Multiply components by a scalar.
§type Output = AlphaColor<CS>
type Output = AlphaColor<CS>
* operator.§fn mul(self, rhs: f32) -> AlphaColor<CS>
fn mul(self, rhs: f32) -> AlphaColor<CS>
* operation. Read more§impl<CS> PartialEq for AlphaColor<CS>where
CS: ColorSpace,
impl<CS> PartialEq for AlphaColor<CS>where
CS: ColorSpace,
§impl<CS> Sub for AlphaColor<CS>where
CS: ColorSpace,
Component-wise subtraction of components.
impl<CS> Sub for AlphaColor<CS>where
CS: ColorSpace,
Component-wise subtraction of components.
§type Output = AlphaColor<CS>
type Output = AlphaColor<CS>
- operator.§fn sub(self, rhs: AlphaColor<CS>) -> AlphaColor<CS>
fn sub(self, rhs: AlphaColor<CS>) -> AlphaColor<CS>
- operation. Read moreimpl<CS> Copy for AlphaColor<CS>where
CS: Copy,
Auto Trait Implementations§
impl<CS> Freeze for AlphaColor<CS>
impl<CS> RefUnwindSafe for AlphaColor<CS>where
CS: RefUnwindSafe,
impl<CS> Send for AlphaColor<CS>where
CS: Send,
impl<CS> Sync for AlphaColor<CS>where
CS: Sync,
impl<CS> Unpin for AlphaColor<CS>where
CS: Unpin,
impl<CS> UnwindSafe for AlphaColor<CS>where
CS: UnwindSafe,
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
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