use core::{ fmt, iter::FromIterator, ops::{ Add, AddAssign, BitAnd, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Sub, SubAssign, }, }; #[cfg(feature = "approx")] use approx::{AbsDiffEq, RelativeEq, UlpsEq}; #[cfg(feature = "random")] use rand::{ distributions::{ uniform::{SampleBorrow, SampleUniform, Uniform, UniformSampler}, Distribution, Standard, }, Rng, }; use crate::{ blend::{PreAlpha, Premultiply}, bool_mask::HasBoolMask, cast::ArrayCast, clamp, clamp_assign, convert::{FromColorUnclamped, IntoColorUnclamped}, num::{self, Arithmetics, One, PartialCmp, SaturatingAdd, SaturatingSub, Zero}, stimulus::Stimulus, ArrayExt, Clamp, ClampAssign, GetHue, IsWithinBounds, Lighten, LightenAssign, Mix, MixAssign, NextArray, Saturate, SaturateAssign, SetHue, ShiftHue, ShiftHueAssign, WithAlpha, WithHue, }; /// An alpha component wrapper for colors, for adding transparency. /// /// Instead of having separate types for "RGB with alpha", "HSV with alpha", and /// so on, Palette uses this wrapper type to attach the alpha component. The /// memory representation is the same as if `alpha` was the last member/property /// of `color`, which is just as space efficient. The perk of having a wrapper /// is that the alpha can easily be added to or separated form any color. /// /// # Creating Transparent Values /// /// The color types in Palette have transparent type aliases, such as /// [`Srgba`](crate::Srgba) for [`Srgb`][crate::Srgb] or [`Hsla`](crate::Hsla) /// for [`Hsl`](crate::Hsl). These aliases implement `new` and other useful /// methods. Here's the same example as for [`Rgb`](crate::rgb::Rgb), but with /// transparency: /// /// ``` /// use palette::Srgba; /// /// let rgba_u8 = Srgba::new(171u8, 193, 35, 128); /// let rgab_f32 = Srgba::new(0.3f32, 0.8, 0.1, 0.5); /// /// // `new` is also `const`: /// const RGBA_U8: Srgba = Srgba::new(171, 193, 35, 128); /// /// // Conversion methods from the color type are usually available for transparent /// // values too. For example `into_format` for changing the number format: /// let rgb_u8_from_f32 = Srgba::new(0.3f32, 0.8, 0.1, 0.5).into_format::(); /// /// // Hexadecimal is also supported for RGBA, with or without the #: /// let rgb_from_hex1: Srgba = "#f034e65a".parse().unwrap(); /// let rgb_from_hex2: Srgba = "f034e65a".parse().unwrap(); /// assert_eq!(rgb_from_hex1, rgb_from_hex2); /// /// // This includes the shorthand format: /// let rgb_from_short_hex: Srgba = "f3ea".parse().unwrap(); /// let rgb_from_long_hex: Srgba = "ff33eeaa".parse().unwrap(); /// assert_eq!(rgb_from_short_hex, rgb_from_long_hex); /// /// // It's also possible to convert from (and to) arrays, tuples and `u32` values: /// let rgb_from_array = Srgba::from([171u8, 193, 35, 128]); /// let rgb_from_tuple = Srgba::from((171u8, 193, 35, 128)); /// let rgb_from_u32 = Srgba::from(0x607F005A); /// ``` /// /// Opaque values can be made transparent using the [`WithAlpha`] trait, in /// addition to simply wrapping them in `Alpha`. [`WithAlpha`] is also useful in /// generic code, since it's implemented for both opaque and transparent types. /// /// ``` /// use palette::{WithAlpha, Srgb}; /// /// let rgb = Srgb::new(171u8, 193, 35); /// let rgba = rgb.with_alpha(128u8); /// assert_eq!(rgba.alpha, 128); /// ``` /// /// You may have noticed the `u8` in `rgb.with_alpha(128u8)`. That's because /// `Alpha` allows the transparency component to have a different type than the /// color components. It would be just as valid to write /// `rgb.with_alpha(0.5f32)`, for example. /// /// # Accessing Color Components /// /// To help with the nesting, `Alpha` implements [`Deref`] and [`DerefMut`]. /// This use of the traits is a bit unconventional, since `Alpha` isn't a smart /// pointer. It turned out to be a quite successful experiment that stuck /// around. /// /// ``` /// use palette::Srgba; /// /// let rgba = Srgba::new(171u8, 193, 35, 128); /// let red = rgba.red; // Accesses `rgba.color.red`. /// let alpha = rgba.alpha; // Accesses `rgba.alpha`. /// let rgb = rgba.color; // Accesses `rgba.color`; /// ``` /// /// The main drawback is in generic code: /// /// ```compile_fail /// use palette::Srgba; /// /// fn get_red(rgba: Srgba) -> T { /// rgba.red // Error: cannot move out of dereference of `Alpha, T>` /// } /// ``` /// /// `red` has to be accessed through `color`: /// /// ``` /// use palette::Srgba; /// /// fn get_red(rgba: Srgba) -> T { /// rgba.color.red /// } /// ``` #[derive(Clone, Copy, Debug)] #[repr(C)] pub struct Alpha { /// The color. pub color: C, /// The transparency component. 0.0 (or 0u8) is fully transparent and 1.0 /// (or 255u8) is fully opaque. pub alpha: T, } impl Alpha { /// Return an iterator over the colors in the wrapped collections. pub fn iter<'a>(&'a self) -> <&'a Self as IntoIterator>::IntoIter where &'a Self: IntoIterator, { self.into_iter() } /// Return an iterator that allows modifying the colors in the wrapped collections. pub fn iter_mut<'a>(&'a mut self) -> <&'a mut Self as IntoIterator>::IntoIter where &'a mut Self: IntoIterator, { self.into_iter() } } impl Alpha { /// Alpha mask the color by its transparency. pub fn premultiply(self) -> PreAlpha { PreAlpha::new(self.color, self.alpha) } } impl Alpha { /// Return the `alpha` value minimum. pub fn min_alpha() -> T { T::zero() } /// Return the `alpha` value maximum. pub fn max_alpha() -> T { T::max_intensity() } } impl PartialEq for Alpha where T: PartialEq, C: PartialEq, { fn eq(&self, other: &Self) -> bool { self.color == other.color && self.alpha == other.alpha } } impl Eq for Alpha where T: Eq, C: Eq, { } impl, C2, T> FromColorUnclamped for Alpha where C1::Color: IntoColorUnclamped, { fn from_color_unclamped(other: C1) -> Self { let (color, alpha) = other.split(); Alpha { color: color.into_color_unclamped(), alpha, } } } impl WithAlpha for Alpha { type Color = C; type WithAlpha = Self; fn with_alpha(mut self, alpha: A) -> Self::WithAlpha { self.alpha = alpha; self } fn without_alpha(self) -> Self::Color { self.color } fn split(self) -> (Self::Color, A) { (self.color, self.alpha) } } impl Deref for Alpha { type Target = C; fn deref(&self) -> &C { &self.color } } impl DerefMut for Alpha { fn deref_mut(&mut self) -> &mut C { &mut self.color } } impl Mix for Alpha where C: Mix, C::Scalar: Zero + One + num::Clamp + Arithmetics + Clone, { type Scalar = C::Scalar; #[inline] fn mix(mut self, other: Self, factor: C::Scalar) -> Self { let factor = clamp(factor, C::Scalar::zero(), C::Scalar::one()); self.color = self.color.mix(other.color, factor.clone()); self.alpha = self.alpha.clone() + factor * (other.alpha - self.alpha); self } } impl MixAssign for Alpha where C: MixAssign, C::Scalar: Zero + One + num::Clamp + Arithmetics + AddAssign + Clone, { type Scalar = C::Scalar; #[inline] fn mix_assign(&mut self, other: Self, factor: C::Scalar) { let factor = clamp(factor, C::Scalar::zero(), C::Scalar::one()); self.color.mix_assign(other.color, factor.clone()); self.alpha += factor * (other.alpha - self.alpha.clone()); } } impl Lighten for Alpha { type Scalar = C::Scalar; #[inline] fn lighten(self, factor: C::Scalar) -> Self { Alpha { color: self.color.lighten(factor), alpha: self.alpha, } } #[inline] fn lighten_fixed(self, amount: C::Scalar) -> Self { Alpha { color: self.color.lighten_fixed(amount), alpha: self.alpha, } } } impl LightenAssign for Alpha { type Scalar = C::Scalar; #[inline] fn lighten_assign(&mut self, factor: C::Scalar) { self.color.lighten_assign(factor); } #[inline] fn lighten_fixed_assign(&mut self, amount: C::Scalar) { self.color.lighten_fixed_assign(amount); } } impl GetHue for Alpha { type Hue = C::Hue; #[inline] fn get_hue(&self) -> C::Hue { self.color.get_hue() } } impl WithHue for Alpha where C: WithHue, { #[inline] fn with_hue(mut self, hue: H) -> Self { self.color = self.color.with_hue(hue); self } } impl SetHue for Alpha where C: SetHue, { #[inline] fn set_hue(&mut self, hue: H) { self.color.set_hue(hue); } } impl ShiftHue for Alpha where C: ShiftHue, { type Scalar = C::Scalar; #[inline] fn shift_hue(mut self, amount: Self::Scalar) -> Self { self.color = self.color.shift_hue(amount); self } } impl ShiftHueAssign for Alpha where C: ShiftHueAssign, { type Scalar = C::Scalar; #[inline] fn shift_hue_assign(&mut self, amount: Self::Scalar) { self.color.shift_hue_assign(amount); } } impl Saturate for Alpha { type Scalar = C::Scalar; #[inline] fn saturate(self, factor: C::Scalar) -> Self { Alpha { color: self.color.saturate(factor), alpha: self.alpha, } } #[inline] fn saturate_fixed(self, amount: C::Scalar) -> Self { Alpha { color: self.color.saturate_fixed(amount), alpha: self.alpha, } } } impl SaturateAssign for Alpha { type Scalar = C::Scalar; #[inline] fn saturate_assign(&mut self, factor: C::Scalar) { self.color.saturate_assign(factor); } #[inline] fn saturate_fixed_assign(&mut self, amount: C::Scalar) { self.color.saturate_fixed_assign(amount); } } impl IsWithinBounds for Alpha where C: IsWithinBounds, T: Stimulus + PartialCmp + IsWithinBounds, C::Mask: BitAnd, { #[inline] fn is_within_bounds(&self) -> C::Mask { self.color.is_within_bounds() & self.alpha.gt_eq(&Self::min_alpha()) & self.alpha.lt_eq(&Self::max_alpha()) } } impl Clamp for Alpha where C: Clamp, T: Stimulus + num::Clamp, { #[inline] fn clamp(self) -> Self { Alpha { color: self.color.clamp(), alpha: clamp(self.alpha, Self::min_alpha(), Self::max_alpha()), } } } impl ClampAssign for Alpha where C: ClampAssign, T: Stimulus + num::ClampAssign, { #[inline] fn clamp_assign(&mut self) { self.color.clamp_assign(); clamp_assign(&mut self.alpha, Self::min_alpha(), Self::max_alpha()); } } unsafe impl ArrayCast for Alpha::Array as ArrayExt>::Item> where C: ArrayCast, C::Array: NextArray, { type Array = ::Next; } impl HasBoolMask for Alpha where C: HasBoolMask, T: HasBoolMask, { type Mask = C::Mask; } impl Default for Alpha { fn default() -> Alpha { Alpha { color: C::default(), alpha: Self::max_alpha(), } } } #[cfg(feature = "approx")] impl AbsDiffEq for Alpha where C: AbsDiffEq, T: AbsDiffEq, T::Epsilon: Clone, { type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { self.color.abs_diff_eq(&other.color, epsilon.clone()) && self.alpha.abs_diff_eq(&other.alpha, epsilon) } } #[cfg(feature = "approx")] impl RelativeEq for Alpha where C: RelativeEq, T: RelativeEq, T::Epsilon: Clone, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } fn relative_eq( &self, other: &Alpha, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool { self.color .relative_eq(&other.color, epsilon.clone(), max_relative.clone()) && self.alpha.relative_eq(&other.alpha, epsilon, max_relative) } } #[cfg(feature = "approx")] impl UlpsEq for Alpha where C: UlpsEq, T: UlpsEq, T::Epsilon: Clone, { fn default_max_ulps() -> u32 { T::default_max_ulps() } fn ulps_eq(&self, other: &Alpha, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.color.ulps_eq(&other.color, epsilon.clone(), max_ulps) && self.alpha.ulps_eq(&other.alpha, epsilon, max_ulps) } } impl Add for Alpha where C: Add, T: Add, { type Output = Alpha::Output>; fn add(self, other: Alpha) -> Self::Output { Alpha { color: self.color + other.color, alpha: self.alpha + other.alpha, } } } impl Add for Alpha where T: Add + Clone, C: Add, { type Output = Alpha::Output>; fn add(self, c: T) -> Self::Output { Alpha { color: self.color + c.clone(), alpha: self.alpha + c, } } } impl AddAssign for Alpha where C: AddAssign, T: AddAssign, { fn add_assign(&mut self, other: Alpha) { self.color += other.color; self.alpha += other.alpha; } } impl AddAssign for Alpha where T: AddAssign + Clone, C: AddAssign, { fn add_assign(&mut self, c: T) { self.color += c.clone(); self.alpha += c; } } impl SaturatingAdd for Alpha where C: SaturatingAdd, T: SaturatingAdd, { type Output = Alpha::Output>; fn saturating_add(self, other: Alpha) -> Self::Output { Alpha { color: self.color.saturating_add(other.color), alpha: self.alpha.saturating_add(other.alpha), } } } impl SaturatingAdd for Alpha where T: SaturatingAdd + Clone, C: SaturatingAdd, { type Output = Alpha::Output>; fn saturating_add(self, c: T) -> Self::Output { Alpha { color: self.color.saturating_add(c.clone()), alpha: self.alpha.saturating_add(c), } } } impl Sub for Alpha where C: Sub, T: Sub, { type Output = Alpha::Output>; fn sub(self, other: Alpha) -> Self::Output { Alpha { color: self.color - other.color, alpha: self.alpha - other.alpha, } } } impl Sub for Alpha where T: Sub + Clone, C: Sub, { type Output = Alpha::Output>; fn sub(self, c: T) -> Self::Output { Alpha { color: self.color - c.clone(), alpha: self.alpha - c, } } } impl SubAssign for Alpha where C: SubAssign, T: SubAssign, { fn sub_assign(&mut self, other: Alpha) { self.color -= other.color; self.alpha -= other.alpha; } } impl SubAssign for Alpha where T: SubAssign + Clone, C: SubAssign, { fn sub_assign(&mut self, c: T) { self.color -= c.clone(); self.alpha -= c; } } impl SaturatingSub for Alpha where C: SaturatingSub, T: SaturatingSub, { type Output = Alpha::Output>; fn saturating_sub(self, other: Alpha) -> Self::Output { Alpha { color: self.color.saturating_sub(other.color), alpha: self.alpha.saturating_sub(other.alpha), } } } impl SaturatingSub for Alpha where T: SaturatingSub + Clone, C: SaturatingSub, { type Output = Alpha::Output>; fn saturating_sub(self, c: T) -> Self::Output { Alpha { color: self.color.saturating_sub(c.clone()), alpha: self.alpha.saturating_sub(c), } } } impl Mul for Alpha where C: Mul, T: Mul, { type Output = Alpha::Output>; fn mul(self, other: Alpha) -> Self::Output { Alpha { color: self.color * other.color, alpha: self.alpha * other.alpha, } } } impl Mul for Alpha where T: Mul + Clone, C: Mul, { type Output = Alpha::Output>; fn mul(self, c: T) -> Self::Output { Alpha { color: self.color * c.clone(), alpha: self.alpha * c, } } } impl MulAssign for Alpha where C: MulAssign, T: MulAssign, { fn mul_assign(&mut self, other: Alpha) { self.color *= other.color; self.alpha *= other.alpha; } } impl MulAssign for Alpha where T: MulAssign + Clone, C: MulAssign, { fn mul_assign(&mut self, c: T) { self.color *= c.clone(); self.alpha *= c; } } impl Div for Alpha where C: Div, T: Div, { type Output = Alpha::Output>; fn div(self, other: Alpha) -> Self::Output { Alpha { color: self.color / other.color, alpha: self.alpha / other.alpha, } } } impl Div for Alpha where T: Div + Clone, C: Div, { type Output = Alpha::Output>; fn div(self, c: T) -> Self::Output { Alpha { color: self.color / c.clone(), alpha: self.alpha / c, } } } impl DivAssign for Alpha where C: DivAssign, T: DivAssign, { fn div_assign(&mut self, other: Alpha) { self.color /= other.color; self.alpha /= other.alpha; } } impl DivAssign for Alpha where T: DivAssign + Clone, C: DivAssign, { fn div_assign(&mut self, c: T) { self.color /= c.clone(); self.alpha /= c; } } impl_array_casts!([C, T, const N: usize] Alpha, [T; N], where Alpha: ArrayCast); impl From for Alpha { fn from(color: C) -> Alpha { Alpha { color, alpha: Self::max_alpha(), } } } impl fmt::LowerHex for Alpha where T: fmt::LowerHex, C: fmt::LowerHex, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let size = f.width().unwrap_or(::core::mem::size_of::() * 2); write!( f, "{:0width$x}{:0width$x}", self.color, self.alpha, width = size ) } } impl fmt::UpperHex for Alpha where T: fmt::UpperHex, C: fmt::UpperHex, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let size = f.width().unwrap_or(::core::mem::size_of::() * 2); write!( f, "{:0width$X}{:0width$X}", self.color, self.alpha, width = size ) } } impl Extend> for Alpha where C: Extend, A: Extend, { fn extend>>(&mut self, iter: T) { for color in iter { self.color.extend(core::iter::once(color.color)); self.alpha.extend(core::iter::once(color.alpha)); } } } impl FromIterator> for Alpha where C: Extend + FromIterator, A: Extend + Default, { fn from_iter>>(iter: T) -> Self { let mut result = Self { color: C::from_iter(None), // Default is currently a used for black, meaning it's not always what we want here. alpha: A::default(), }; for color in iter { result.color.extend(core::iter::once(color.color)); result.alpha.extend(core::iter::once(color.alpha)); } result } } /// An iterator for transparent colors. pub struct Iter { pub(crate) color: C, pub(crate) alpha: A, } impl Iterator for Iter where C: Iterator, A: Iterator, { type Item = Alpha; fn next(&mut self) -> Option { let color = self.color.next(); let alpha = self.alpha.next(); if let (Some(color), Some(alpha)) = (color, alpha) { Some(Alpha { color, alpha }) } else { None } } fn size_hint(&self) -> (usize, Option) { let hint = self.color.size_hint(); debug_assert_eq!( self.alpha.size_hint(), hint, "the color and alpha iterators have different size hints" ); hint } fn count(self) -> usize { let count = self.color.count(); debug_assert_eq!( self.alpha.count(), count, "the color and alpha iterators have different counts" ); count } } impl DoubleEndedIterator for Iter where C: DoubleEndedIterator, A: DoubleEndedIterator, { fn next_back(&mut self) -> Option { let color = self.color.next_back(); let alpha = self.alpha.next_back(); if let (Some(color), Some(alpha)) = (color, alpha) { Some(Alpha { color, alpha }) } else { None } } } impl ExactSizeIterator for Iter where C: ExactSizeIterator, A: ExactSizeIterator, { fn len(&self) -> usize { let len = self.color.len(); debug_assert_eq!( self.alpha.len(), len, "the color and alpha iterators have different lengths" ); len } } #[cfg(feature = "serializing")] impl serde::Serialize for Alpha where C: serde::Serialize, T: serde::Serialize, { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { self.color.serialize(crate::serde::AlphaSerializer { inner: serializer, alpha: &self.alpha, }) } } #[cfg(feature = "serializing")] impl<'de, C, T> serde::Deserialize<'de> for Alpha where C: serde::Deserialize<'de>, T: serde::Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { let mut alpha: Option = None; let color = C::deserialize(crate::serde::AlphaDeserializer { inner: deserializer, alpha: &mut alpha, })?; if let Some(alpha) = alpha { Ok(Self { color, alpha }) } else { Err(serde::de::Error::missing_field("alpha")) } } } #[cfg(feature = "random")] impl Distribution> for Standard where Standard: Distribution + Distribution, { fn sample(&self, rng: &mut R) -> Alpha { Alpha { color: rng.gen(), alpha: rng.gen(), } } } /// Sample transparent colors uniformly. #[cfg(feature = "random")] pub struct UniformAlpha where T: SampleUniform, C: SampleUniform, { color: Uniform, alpha: Uniform, } #[cfg(feature = "random")] impl SampleUniform for Alpha where T: Clone + SampleUniform, C: Clone + SampleUniform, { type Sampler = UniformAlpha; } #[cfg(feature = "random")] impl UniformSampler for UniformAlpha where T: Clone + SampleUniform, C: Clone + SampleUniform, { type X = Alpha; fn new(low_b: B1, high_b: B2) -> Self where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow().clone(); let high = high_b.borrow().clone(); UniformAlpha { color: Uniform::new::(low.color, high.color), alpha: Uniform::new::<_, T>(low.alpha, high.alpha), } } fn new_inclusive(low_b: B1, high_b: B2) -> Self where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow().clone(); let high = high_b.borrow().clone(); UniformAlpha { color: Uniform::new_inclusive::(low.color, high.color), alpha: Uniform::new_inclusive::<_, T>(low.alpha, high.alpha), } } fn sample(&self, rng: &mut R) -> Alpha { Alpha { color: self.color.sample(rng), alpha: self.alpha.sample(rng), } } } #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Zeroable for Alpha where C: bytemuck::Zeroable, T: bytemuck::Zeroable, { } // Safety: // // It is a requirement of `ArrayCast` that the in-memory representation of `C` // is made of `T`s. Because `T` is `Pod`, `Alpha` is `Pod` as well because // no internal padding can be introduced during monomorphization. #[cfg(feature = "bytemuck")] unsafe impl bytemuck::Pod for Alpha where T: bytemuck::Pod, C: bytemuck::Pod + ArrayCast, { } #[cfg(test)] mod test { use crate::encoding::Srgb; use crate::rgb::Rgba; #[test] fn lower_hex() { assert_eq!( format!("{:x}", Rgba::::new(171, 193, 35, 161)), "abc123a1" ); } #[test] fn lower_hex_small_numbers() { assert_eq!( format!("{:x}", Rgba::::new(1, 2, 3, 4)), "01020304" ); assert_eq!( format!("{:x}", Rgba::::new(1, 2, 3, 4)), "0001000200030004" ); assert_eq!( format!("{:x}", Rgba::::new(1, 2, 3, 4)), "00000001000000020000000300000004" ); assert_eq!( format!("{:x}", Rgba::::new(1, 2, 3, 4)), "0000000000000001000000000000000200000000000000030000000000000004" ); } #[test] fn lower_hex_custom_width() { assert_eq!( format!("{:03x}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); assert_eq!( format!("{:03x}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); assert_eq!( format!("{:03x}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); assert_eq!( format!("{:03x}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); } #[test] fn upper_hex() { assert_eq!( format!("{:X}", Rgba::::new(171, 193, 35, 161)), "ABC123A1" ); } #[test] fn upper_hex_small_numbers() { assert_eq!( format!("{:X}", Rgba::::new(1, 2, 3, 4)), "01020304" ); assert_eq!( format!("{:X}", Rgba::::new(1, 2, 3, 4)), "0001000200030004" ); assert_eq!( format!("{:X}", Rgba::::new(1, 2, 3, 4)), "00000001000000020000000300000004" ); assert_eq!( format!("{:X}", Rgba::::new(1, 2, 3, 4)), "0000000000000001000000000000000200000000000000030000000000000004" ); } #[test] fn upper_hex_custom_width() { assert_eq!( format!("{:03X}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); assert_eq!( format!("{:03X}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); assert_eq!( format!("{:03X}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); assert_eq!( format!("{:03X}", Rgba::::new(1, 2, 3, 4)), "001002003004" ); } #[test] fn check_min_max_components() { assert_eq!(Rgba::::min_alpha(), 0.0); assert_eq!(Rgba::::max_alpha(), 1.0); } #[cfg(feature = "serializing")] #[test] fn serialize() { let color = Rgba::::new(0.3, 0.8, 0.1, 0.5); assert_eq!( serde_json::to_string(&color).unwrap(), r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"# ); assert_eq!( ron::to_string(&color).unwrap(), r#"(red:0.3,green:0.8,blue:0.1,alpha:0.5)"# ); } #[cfg(feature = "serializing")] #[test] fn deserialize() { let color = Rgba::::new(0.3, 0.8, 0.1, 0.5); assert_eq!( serde_json::from_str::>(r#"{"alpha":0.5,"red":0.3,"green":0.8,"blue":0.1}"#) .unwrap(), color ); assert_eq!( ron::from_str::>(r#"(alpha:0.5,red:0.3,green:0.8,blue:0.1)"#).unwrap(), color ); assert_eq!( ron::from_str::>(r#"Rgb(alpha:0.5,red:0.3,green:0.8,blue:0.1)"#).unwrap(), color ); } #[cfg(feature = "serializing")] #[test] fn serde_round_trips() { let color = Rgba::::new(0.3, 0.8, 0.1, 0.5); assert_eq!( serde_json::from_str::>(&serde_json::to_string(&color).unwrap()).unwrap(), color ); assert_eq!( ron::from_str::>(&ron::to_string(&color).unwrap()).unwrap(), color ); } #[cfg(feature = "serializing")] #[test] fn serde_various_types() { macro_rules! test_roundtrip { ($value:expr $(, $ron_name:expr)?) => { let value = super::Alpha { color: $value, alpha: 0.5, }; assert_eq!( serde_json::from_str::>( &serde_json::to_string(&value).expect("json serialization") ) .expect("json deserialization"), value ); let ron_string = ron::to_string(&value).expect("ron serialization"); assert_eq!( ron::from_str::>(&ron_string) .expect("ron deserialization"), value ); $( assert_eq!( ron::from_str::>(&format!("{}{ron_string}", $ron_name)) .expect("ron deserialization"), value ); )? }; } #[derive(Serialize, Deserialize, PartialEq, Debug)] struct Empty; #[derive(Serialize, Deserialize, PartialEq, Debug)] struct UnitTuple(); #[derive(Serialize, Deserialize, PartialEq, Debug)] struct Newtype(f32); #[derive(Serialize, Deserialize, PartialEq, Debug)] struct Tuple(f32, f32); #[derive(Serialize, Deserialize, PartialEq, Debug)] struct Struct { value: f32, } test_roundtrip!(()); test_roundtrip!(Empty, "Empty"); test_roundtrip!(UnitTuple(), "UnitTuple"); test_roundtrip!(Newtype(0.1), "Newtype"); test_roundtrip!(Tuple(0.1, 0.2), "Tuple"); test_roundtrip!(Struct { value: 0.1 }, "Struct"); } test_uniform_distribution! { Rgba { red: (0.0, 1.0), green: (0.0, 1.0), blue: (0.0, 1.0), alpha: (0.0, 1.0) }, min: Rgba::new(0.0f32, 0.0, 0.0, 0.0), max: Rgba::new(1.0, 1.0, 1.0, 1.0) } }