Struct PremulColor
#[repr(transparent)]pub struct PremulColor<CS> {
pub components: [f32; 4],
pub cs: PhantomData<CS>,
}Expand description
A color with premultiplied alpha.
A color in a color space known at compile time, with color channels premultiplied by the alpha channel.
Following the convention of CSS Color 4, in cylindrical color spaces the hue channel is not premultiplied. If it were, interpolation would give undesirable results.
See AlphaColor for a color type without alpha premultiplication.
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, and are premultiplied with the alpha value. The fourth component is alpha.
Note that in cylindrical color spaces, the hue component is not premultiplied, as specified in the CSS Color 4 spec. The methods on this type take care of that for you, but if you’re manipulating the components yourself, be aware.
cs: PhantomData<CS>The color space.
Implementations§
§impl<CS> PremulColor<CS>where
CS: ColorSpace,
impl<CS> PremulColor<CS>where
CS: ColorSpace,
pub const BLACK: PremulColor<CS>
pub const BLACK: PremulColor<CS>
A black color.
More comprehensive pre-defined colors are available
in the color::palette module.
pub const TRANSPARENT: PremulColor<CS>
pub const TRANSPARENT: PremulColor<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: PremulColor<CS>
pub const WHITE: PremulColor<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]) -> PremulColor<CS>
pub const fn new(components: [f32; 4]) -> PremulColor<CS>
Create a new color from the given components.
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 un-premultiplying the alpha and
calling AlphaColor::discard_alpha.
The result of calling this on a fully transparent color will be the color black.
pub fn convert<TargetCS>(self) -> PremulColor<TargetCS>where
TargetCS: ColorSpace,
pub fn convert<TargetCS>(self) -> PremulColor<TargetCS>where
TargetCS: ColorSpace,
Convert a color into a different color space.
pub const fn un_premultiply(self) -> AlphaColor<CS>
pub const fn un_premultiply(self) -> AlphaColor<CS>
Convert a color to the corresponding separate alpha form.
pub fn lerp_rect(self, other: PremulColor<CS>, t: f32) -> PremulColor<CS>
pub fn lerp_rect(self, other: PremulColor<CS>, t: f32) -> PremulColor<CS>
Interpolate colors.
Note: this function doesn’t fix up hue in cylindrical spaces. It is still useful if the hue angles are compatible, particularly if the fixup has been applied.
pub fn fixup_hues(self, other: &mut PremulColor<CS>, direction: HueDirection)
pub fn fixup_hues(self, other: &mut PremulColor<CS>, direction: HueDirection)
Apply hue fixup for interpolation.
Adjust the hue angle of other so that linear interpolation results in
the expected hue direction.
pub fn lerp(
self,
other: PremulColor<CS>,
t: f32,
direction: HueDirection,
) -> PremulColor<CS>
pub fn lerp( self, other: PremulColor<CS>, t: f32, direction: HueDirection, ) -> PremulColor<CS>
Linearly interpolate colors, with hue fixup if needed.
pub const fn multiply_alpha(self, rhs: f32) -> PremulColor<CS>
pub const fn multiply_alpha(self, rhs: f32) -> PremulColor<CS>
Multiply alpha by the given factor.
pub fn difference(self, other: PremulColor<CS>) -> f32
pub fn difference(self, other: PremulColor<CS>) -> f32
Difference between two colors by Euclidean metric.
pub fn to_rgba8(self) -> PremulRgba8
pub fn to_rgba8(self) -> PremulRgba8
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, see the note on AlphaColor::to_rgba8.
§impl PremulColor<Srgb>
impl PremulColor<Srgb>
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> PremulColor<Srgb>
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> PremulColor<Srgb>
Create a color from pre-multiplied 8-bit rgba values.
Note: for conversion from the PremulRgba8 type, just use the From trait.
Trait Implementations§
§impl<CS> Add for PremulColor<CS>where
CS: ColorSpace,
Component-wise addition of components.
impl<CS> Add for PremulColor<CS>where
CS: ColorSpace,
Component-wise addition of components.
§type Output = PremulColor<CS>
type Output = PremulColor<CS>
+ operator.§fn add(self, rhs: PremulColor<CS>) -> PremulColor<CS>
fn add(self, rhs: PremulColor<CS>) -> PremulColor<CS>
+ operation. Read more§impl<CS> BitEq for PremulColor<CS>
impl<CS> BitEq for PremulColor<CS>
§fn bit_eq(&self, other: &PremulColor<CS>) -> bool
fn bit_eq(&self, other: &PremulColor<CS>) -> bool
§impl<CS> BitHash for PremulColor<CS>
impl<CS> BitHash for PremulColor<CS>
§impl<CS> Clone for PremulColor<CS>where
CS: Clone,
impl<CS> Clone for PremulColor<CS>where
CS: Clone,
§fn clone(&self) -> PremulColor<CS>
fn clone(&self) -> PremulColor<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 PremulColor<CS>where
CS: Debug,
impl<CS> Debug for PremulColor<CS>where
CS: Debug,
§impl<CS> Div<f32> for PremulColor<CS>where
CS: ColorSpace,
Divide components by a scalar.
impl<CS> Div<f32> for PremulColor<CS>where
CS: ColorSpace,
Divide components by a scalar.
§type Output = PremulColor<CS>
type Output = PremulColor<CS>
/ operator.§fn div(self, rhs: f32) -> PremulColor<CS>
fn div(self, rhs: f32) -> PremulColor<CS>
/ operation. Read more§impl<CS> From<OpaqueColor<CS>> for PremulColor<CS>where
CS: ColorSpace,
impl<CS> From<OpaqueColor<CS>> for PremulColor<CS>where
CS: ColorSpace,
§fn from(value: OpaqueColor<CS>) -> PremulColor<CS>
fn from(value: OpaqueColor<CS>) -> PremulColor<CS>
§impl From<PremulRgba8> for PremulColor<Srgb>
impl From<PremulRgba8> for PremulColor<Srgb>
§fn from(value: PremulRgba8) -> PremulColor<Srgb>
fn from(value: PremulRgba8) -> PremulColor<Srgb>
§impl<CS> FromStr for PremulColor<CS>where
CS: ColorSpace,
impl<CS> FromStr for PremulColor<CS>where
CS: ColorSpace,
§type Err = ParseError
type Err = ParseError
§fn from_str(
s: &str,
) -> Result<PremulColor<CS>, <PremulColor<CS> as FromStr>::Err>
fn from_str( s: &str, ) -> Result<PremulColor<CS>, <PremulColor<CS> as FromStr>::Err>
s to return a value of this type. Read more§impl<CS> Mul<f32> for PremulColor<CS>where
CS: ColorSpace,
Multiply components by a scalar.
impl<CS> Mul<f32> for PremulColor<CS>where
CS: ColorSpace,
Multiply components by a scalar.
For rectangular color spaces, this is equivalent to multiplying
alpha, but for cylindrical color spaces, PremulColor::multiply_alpha
is the preferred method.
§type Output = PremulColor<CS>
type Output = PremulColor<CS>
* operator.§fn mul(self, rhs: f32) -> PremulColor<CS>
fn mul(self, rhs: f32) -> PremulColor<CS>
* operation. Read more§impl<CS> PartialEq for PremulColor<CS>where
CS: ColorSpace,
impl<CS> PartialEq for PremulColor<CS>where
CS: ColorSpace,
§impl<CS> Sub for PremulColor<CS>where
CS: ColorSpace,
Component-wise subtraction of components.
impl<CS> Sub for PremulColor<CS>where
CS: ColorSpace,
Component-wise subtraction of components.
§type Output = PremulColor<CS>
type Output = PremulColor<CS>
- operator.§fn sub(self, rhs: PremulColor<CS>) -> PremulColor<CS>
fn sub(self, rhs: PremulColor<CS>) -> PremulColor<CS>
- operation. Read moreimpl<CS> Copy for PremulColor<CS>where
CS: Copy,
Auto Trait Implementations§
impl<CS> Freeze for PremulColor<CS>
impl<CS> RefUnwindSafe for PremulColor<CS>where
CS: RefUnwindSafe,
impl<CS> Send for PremulColor<CS>where
CS: Send,
impl<CS> Sync for PremulColor<CS>where
CS: Sync,
impl<CS> Unpin for PremulColor<CS>where
CS: Unpin,
impl<CS> UnwindSafe for PremulColor<CS>where
CS: UnwindSafe,
Blanket Implementations§
§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
§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)§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> 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 more