#[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 a premultiplied 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 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§
Source§impl<CS: ColorSpace> PremulColor<CS>
impl<CS: ColorSpace> PremulColor<CS>
Sourcepub const BLACK: Self
pub const BLACK: Self
A black color.
More comprehensive pre-defined colors are available
in the color::palette
module.
Sourcepub const TRANSPARENT: Self
pub const TRANSPARENT: Self
A transparent color.
This is a black color with full alpha.
More comprehensive pre-defined colors are available
in the color::palette
module.
Sourcepub const WHITE: Self
pub const WHITE: Self
A white color.
This value is specific to the color space.
More comprehensive pre-defined colors are available
in the color::palette
module.
Sourcepub 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.
Sourcepub fn convert<TargetCS: ColorSpace>(self) -> PremulColor<TargetCS>
pub fn convert<TargetCS: ColorSpace>(self) -> PremulColor<TargetCS>
Convert a color into a different color space.
Sourcepub const fn un_premultiply(self) -> AlphaColor<CS>
pub const fn un_premultiply(self) -> AlphaColor<CS>
Convert a color to the corresponding separate alpha form.
Sourcepub fn lerp_rect(self, other: Self, t: f32) -> Self
pub fn lerp_rect(self, other: Self, t: f32) -> Self
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.
Sourcepub fn fixup_hues(self, other: &mut Self, direction: HueDirection)
pub fn fixup_hues(self, other: &mut Self, direction: HueDirection)
Apply hue fixup for interpolation.
Adjust the hue angle of other
so that linear interpolation results in
the expected hue direction.
Sourcepub fn lerp(self, other: Self, t: f32, direction: HueDirection) -> Self
pub fn lerp(self, other: Self, t: f32, direction: HueDirection) -> Self
Linearly interpolate colors, with hue fixup if needed.
Sourcepub const fn multiply_alpha(self, rhs: f32) -> Self
pub const fn multiply_alpha(self, rhs: f32) -> Self
Multiply alpha by the given factor.
Sourcepub fn difference(self, other: Self) -> f32
pub fn difference(self, other: Self) -> f32
Difference between two colors by Euclidean metric.
Sourcepub 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
.
Source§impl PremulColor<Srgb>
impl PremulColor<Srgb>
Sourcepub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self
pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self
Create a color from pre-multiplied 8-bit rgba values.
Note: for conversion from the PremulRgba8
type, just use the From
trait.
Trait Implementations§
Source§impl<CS: ColorSpace> Add for PremulColor<CS>
Component-wise addition of components.
impl<CS: ColorSpace> Add for PremulColor<CS>
Component-wise addition of components.
Source§impl<CS> BitEq for PremulColor<CS>
impl<CS> BitEq for PremulColor<CS>
Source§impl<CS> BitHash for PremulColor<CS>
impl<CS> BitHash for PremulColor<CS>
Source§impl<CS: Clone> Clone for PremulColor<CS>
impl<CS: Clone> Clone for PremulColor<CS>
Source§fn clone(&self) -> PremulColor<CS>
fn clone(&self) -> PremulColor<CS>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<CS: Debug> Debug for PremulColor<CS>
impl<CS: Debug> Debug for PremulColor<CS>
Source§impl<CS: ColorSpace> Div<f32> for PremulColor<CS>
Divide components by a scalar.
impl<CS: ColorSpace> Div<f32> for PremulColor<CS>
Divide components by a scalar.
Source§impl<CS: ColorSpace> From<OpaqueColor<CS>> for PremulColor<CS>
impl<CS: ColorSpace> From<OpaqueColor<CS>> for PremulColor<CS>
Source§fn from(value: OpaqueColor<CS>) -> Self
fn from(value: OpaqueColor<CS>) -> Self
Source§impl From<PremulRgba8> for PremulColor<Srgb>
impl From<PremulRgba8> for PremulColor<Srgb>
Source§fn from(value: PremulRgba8) -> Self
fn from(value: PremulRgba8) -> Self
Source§impl<CS: ColorSpace> FromStr for PremulColor<CS>
impl<CS: ColorSpace> FromStr for PremulColor<CS>
Source§impl<CS: ColorSpace> Mul<PremulColor<CS>> for f32
Multiply components by a scalar.
impl<CS: ColorSpace> Mul<PremulColor<CS>> for f32
Multiply components by a scalar.
Source§type Output = PremulColor<CS>
type Output = PremulColor<CS>
*
operator.Source§impl<CS: ColorSpace> Mul<f32> for PremulColor<CS>
Multiply components by a scalar.
impl<CS: ColorSpace> Mul<f32> for PremulColor<CS>
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.
Source§impl<CS: ColorSpace> PartialEq for PremulColor<CS>
impl<CS: ColorSpace> PartialEq for PremulColor<CS>
Source§impl<CS: ColorSpace> Sub for PremulColor<CS>
Component-wise subtraction of components.
impl<CS: ColorSpace> Sub for PremulColor<CS>
Component-wise subtraction of components.
Source§impl<CS: ColorSpace> TransparentWrapper<[f32; 4]> for PremulColor<CS>
impl<CS: ColorSpace> TransparentWrapper<[f32; 4]> for PremulColor<CS>
Source§fn wrap_ref(s: &Inner) -> &Self
fn wrap_ref(s: &Inner) -> &Self
Source§fn wrap_mut(s: &mut Inner) -> &mut Self
fn wrap_mut(s: &mut Inner) -> &mut Self
Source§fn wrap_slice(s: &[Inner]) -> &[Self]where
Self: Sized,
fn wrap_slice(s: &[Inner]) -> &[Self]where
Self: Sized,
Source§fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]where
Self: Sized,
fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]where
Self: Sized,
Source§fn peel_ref(s: &Self) -> &Inner
fn peel_ref(s: &Self) -> &Inner
Source§fn peel_mut(s: &mut Self) -> &mut Inner
fn peel_mut(s: &mut Self) -> &mut Inner
Source§fn peel_slice(s: &[Self]) -> &[Inner]where
Self: Sized,
fn peel_slice(s: &[Self]) -> &[Inner]where
Self: Sized,
Source§fn peel_slice_mut(s: &mut [Self]) -> &mut [Inner]where
Self: Sized,
fn peel_slice_mut(s: &mut [Self]) -> &mut [Inner]where
Self: Sized,
Source§impl<CS: ColorSpace> Zeroable for PremulColor<CS>
impl<CS: ColorSpace> Zeroable for PremulColor<CS>
impl<CS: Copy> Copy for PremulColor<CS>
impl<CS: ColorSpace> Pod for PremulColor<CS>
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§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
type Bits = T
Self
must have the same layout as the specified Bits
except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern
.Source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<I, T> TransparentWrapperAlloc<I> for T
impl<I, T> TransparentWrapperAlloc<I> for T
Source§fn wrap_vec(s: Vec<Inner>) -> Vec<Self>where
Self: Sized,
fn wrap_vec(s: Vec<Inner>) -> Vec<Self>where
Self: Sized,
Source§fn wrap_box(s: Box<Inner>) -> Box<Self>
fn wrap_box(s: Box<Inner>) -> Box<Self>
Source§fn wrap_rc(s: Rc<Inner>) -> Rc<Self>
fn wrap_rc(s: Rc<Inner>) -> Rc<Self>
Rc
to the inner type into an Rc
to the wrapper type.Source§fn wrap_arc(s: Arc<Inner>) -> Arc<Self>
fn wrap_arc(s: Arc<Inner>) -> Arc<Self>
Arc
to the inner type into an Arc
to the wrapper type.