#[cfg(feature = "std")]
extern crate std;
use core::fmt;
use core::ptr;
use crate::channel::{Channel, div255};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[repr(C)]
pub struct Rgba<C>
where
C: Copy,
{
pub r: C,
pub g: C,
pub b: C,
pub a: C,
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Premultiplied<T>(T);
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Zeroable for U8x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Pod for U8x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Zeroable for F32x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Pod for F32x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Zeroable for PremultipliedU8x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Pod for PremultipliedU8x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Zeroable for PremultipliedF32x4Rgba {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Pod for PremultipliedF32x4Rgba {}
impl<C: Copy> From<[C; 4]> for Rgba<C> {
fn from([r, g, b, a]: [C; 4]) -> Self {
Self::new(r, g, b, a)
}
}
impl<C: Copy> From<Rgba<C>> for [C; 4] {
fn from(c: Rgba<C>) -> Self {
[c.r, c.g, c.b, c.a]
}
}
impl<C: Copy> From<(C, C, C, C)> for Rgba<C> {
fn from((r, g, b, a): (C, C, C, C)) -> Self {
Self::new(r, g, b, a)
}
}
impl<C: Copy> From<Rgba<C>> for (C, C, C, C) {
fn from(c: Rgba<C>) -> Self {
(c.r, c.g, c.b, c.a)
}
}
impl<C: Copy> AsRef<[C]> for Rgba<C> {
fn as_ref(&self) -> &[C] {
unsafe { core::slice::from_raw_parts(ptr::from_ref(self).cast::<C>(), 4) }
}
}
impl<C: Copy> AsMut<[C]> for Rgba<C> {
fn as_mut(&mut self) -> &mut [C] {
unsafe { core::slice::from_raw_parts_mut(ptr::from_mut(self).cast::<C>(), 4) }
}
}
impl fmt::Display for Rgba<u8> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "rgba({}, {}, {}, {})", self.r, self.g, self.b, self.a)
}
}
impl fmt::Display for Rgba<f32> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "rgba({}, {}, {}, {})", self.r, self.g, self.b, self.a)
}
}
impl<T: fmt::Display> fmt::Display for Premultiplied<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "premultiplied {}", self.0)
}
}
impl Eq for Rgba<u8> {}
impl core::hash::Hash for Rgba<u8> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.r.hash(state);
self.g.hash(state);
self.b.hash(state);
self.a.hash(state);
}
}
impl Eq for Premultiplied<Rgba<u8>> {}
impl core::hash::Hash for Premultiplied<Rgba<u8>> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl<C> Rgba<C>
where
C: Copy,
{
pub const fn new(r: C, g: C, b: C, a: C) -> Self {
Self { r, g, b, a }
}
pub const fn red(&self) -> C {
self.r
}
pub const fn green(&self) -> C {
self.g
}
pub const fn blue(&self) -> C {
self.b
}
pub const fn alpha(&self) -> C {
self.a
}
}
impl<C> Rgba<C>
where
C: Channel,
{
#[must_use]
pub fn premultiply(self) -> Premultiplied<Self> {
Premultiplied(Self::new(
self.r.scale(self.a),
self.g.scale(self.a),
self.b.scale(self.a),
self.a,
))
}
#[must_use]
pub fn lerp(self, other: Self, t: C) -> Self {
let t = t.max_channel(C::ZERO).min_channel(C::ONE);
Self::new(
self.r.lerp(other.r, t),
self.g.lerp(other.g, t),
self.b.lerp(other.b, t),
self.a.lerp(other.a, t),
)
}
}
impl<T: Copy> Premultiplied<T> {
pub const fn from_premultiplied(value: T) -> Self {
Self(value)
}
pub const fn get(self) -> T {
self.0
}
}
impl<C: Channel> Premultiplied<Rgba<C>> {
#[must_use]
pub fn unpremultiply(self) -> Rgba<C> {
let c = self.0;
if c.a.is_one() {
return c;
}
if c.a.is_zero() {
return Rgba::new(C::ZERO, C::ZERO, C::ZERO, C::ZERO);
}
Rgba::new(
c.r.div_normalized(c.a),
c.g.div_normalized(c.a),
c.b.div_normalized(c.a),
c.a,
)
}
}
pub type U8x4Rgba = Rgba<u8>;
pub type F32x4Rgba = Rgba<f32>;
pub type PremultipliedU8x4Rgba = Premultiplied<U8x4Rgba>;
pub type PremultipliedF32x4Rgba = Premultiplied<F32x4Rgba>;
impl U8x4Rgba {
pub const TRANSPARENT: Self = Self::new(0, 0, 0, 0);
pub const BLACK: Self = Self::new(0, 0, 0, 255);
pub const WHITE: Self = Self::new(255, 255, 255, 255);
}
impl F32x4Rgba {
pub const TRANSPARENT: Self = Self::new(0.0, 0.0, 0.0, 0.0);
pub const BLACK: Self = Self::new(0.0, 0.0, 0.0, 1.0);
pub const WHITE: Self = Self::new(1.0, 1.0, 1.0, 1.0);
}
const MAX: f32 = 255.0;
#[allow(clippy::cast_possible_truncation)]
const fn source_over_channel(src: u32, src_a: u32, dst: u32, dst_a: u32, inv_a: u32) -> u8 {
let num = src * src_a * 255 + dst * dst_a * inv_a;
let den = src_a * 255 + dst_a * inv_a;
let q = (2 * num + den) / (2 * den);
if q > 255 { 255 } else { q as u8 }
}
impl U8x4Rgba {
#[must_use]
pub const fn zeroed() -> Self {
Self::new(0, 0, 0, 0)
}
#[must_use]
pub const fn from_rgb_u32(pixel: u32) -> Self {
Self::new(
((pixel >> 16) & 0xFF) as u8,
((pixel >> 8) & 0xFF) as u8,
(pixel & 0xFF) as u8,
255,
)
}
#[must_use]
pub const fn to_rgb_u32(self) -> u32 {
((self.r as u32) << 16) | ((self.g as u32) << 8) | (self.b as u32)
}
#[must_use]
pub const fn source_over(self, dst: Self) -> Self {
let src_a = self.a as u32;
if src_a == 255 {
return self;
}
if src_a == 0 {
return dst;
}
let inv_a = 255 - src_a;
let dst_a = dst.a as u32;
if dst_a == 255 {
return Self::new(
div255(self.r as u32 * src_a + dst.r as u32 * inv_a),
div255(self.g as u32 * src_a + dst.g as u32 * inv_a),
div255(self.b as u32 * src_a + dst.b as u32 * inv_a),
255,
);
}
Self::new(
source_over_channel(self.r as u32, src_a, dst.r as u32, dst_a, inv_a),
source_over_channel(self.g as u32, src_a, dst.g as u32, dst_a, inv_a),
source_over_channel(self.b as u32, src_a, dst.b as u32, dst_a, inv_a),
div255(src_a * 255 + dst_a * inv_a),
)
}
#[must_use]
pub const fn is_transparent(self) -> bool {
self.a == 0
}
#[must_use]
pub const fn is_opaque(self) -> bool {
self.a == 255
}
}
impl F32x4Rgba {
#[must_use]
pub const fn zeroed() -> Self {
Self::new(0.0, 0.0, 0.0, 0.0)
}
#[must_use]
pub const fn clamp(self) -> Self {
#[inline]
const fn clamp_f32(v: f32, lo: f32, hi: f32) -> f32 {
if v < lo {
lo
} else if v > hi {
hi
} else {
v
}
}
Self::new(
clamp_f32(self.r, 0.0, 1.0),
clamp_f32(self.g, 0.0, 1.0),
clamp_f32(self.b, 0.0, 1.0),
clamp_f32(self.a, 0.0, 1.0),
)
}
#[must_use]
#[allow(clippy::float_cmp)]
pub const fn is_transparent(self) -> bool {
self.a == 0.0
}
#[must_use]
#[allow(clippy::float_cmp)]
pub const fn is_opaque(self) -> bool {
self.a == 1.0
}
}
impl From<U8x4Rgba> for F32x4Rgba {
fn from(rgba: U8x4Rgba) -> Self {
Self::new(
f32::from(rgba.red()) / MAX,
f32::from(rgba.green()) / MAX,
f32::from(rgba.blue()) / MAX,
f32::from(rgba.alpha()) / MAX,
)
}
}
impl From<F32x4Rgba> for U8x4Rgba {
fn from(rgba: F32x4Rgba) -> Self {
Self::new(
u8::from_f32(rgba.red()),
u8::from_f32(rgba.green()),
u8::from_f32(rgba.blue()),
u8::from_f32(rgba.alpha()),
)
}
}
impl From<PremultipliedU8x4Rgba> for PremultipliedF32x4Rgba {
fn from(rgba: PremultipliedU8x4Rgba) -> Self {
Self(rgba.0.into())
}
}
impl From<PremultipliedF32x4Rgba> for PremultipliedU8x4Rgba {
fn from(rgba: PremultipliedF32x4Rgba) -> Self {
Self(rgba.0.into())
}
}
#[cfg(test)]
#[allow(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::float_cmp,
clippy::suboptimal_flops
)]
mod tests {
use super::*;
fn source_over_reference(src: U8x4Rgba, dst: U8x4Rgba) -> U8x4Rgba {
let (sa, da) = (f64::from(src.a) / 255.0, f64::from(dst.a) / 255.0);
let out_a = sa + da * (1.0 - sa);
if out_a == 0.0 {
return dst;
}
let ch = |s: u8, d: u8| {
let s = f64::from(s) / 255.0;
let d = f64::from(d) / 255.0;
let co = s * sa + d * da * (1.0 - sa);
(co / out_a * 255.0 + 0.5) as u8
};
U8x4Rgba::new(
ch(src.r, dst.r),
ch(src.g, dst.g),
ch(src.b, dst.b),
(out_a * 255.0 + 0.5) as u8,
)
}
#[test]
fn field_order_is_rgba() {
let c = U8x4Rgba::new(1, 2, 3, 4);
assert_eq!(c.r, 1);
assert_eq!(c.g, 2);
assert_eq!(c.b, 3);
assert_eq!(c.a, 4);
}
#[test]
fn u8_to_f32() {
let rgba_u8 = U8x4Rgba::new(255, 128, 64, 32);
let rgba_f32: F32x4Rgba = rgba_u8.into();
assert_eq!(rgba_f32.red(), 1.0);
assert_eq!(rgba_f32.green(), 0.501_960_8);
assert_eq!(rgba_f32.blue(), 0.250_980_4);
assert_eq!(rgba_f32.alpha(), 0.125_490_2);
}
#[test]
fn f32_to_u8() {
let rgba_f32 = F32x4Rgba::new(1.0, 0.501_960_8, 0.250_980_4, 0.0);
let rgba_u8: U8x4Rgba = rgba_f32.into();
assert_eq!(rgba_u8.red(), 255);
assert_eq!(rgba_u8.green(), 128);
assert_eq!(rgba_u8.blue(), 64);
assert_eq!(rgba_u8.alpha(), 0);
}
#[test]
fn f32_to_u8_clamps_out_of_range() {
let rgba: U8x4Rgba = F32x4Rgba::new(-1.0, 2.0, 0.5, 1.0).into();
assert_eq!(rgba, U8x4Rgba::new(0, 255, 128, 255));
}
#[test]
fn zeroed_is_transparent_black() {
assert_eq!(U8x4Rgba::zeroed(), U8x4Rgba::TRANSPARENT);
assert_eq!(F32x4Rgba::zeroed(), F32x4Rgba::TRANSPARENT);
}
#[test]
fn u8_named_constants() {
assert_eq!(U8x4Rgba::TRANSPARENT, U8x4Rgba::new(0, 0, 0, 0));
assert_eq!(U8x4Rgba::BLACK, U8x4Rgba::new(0, 0, 0, 255));
assert_eq!(U8x4Rgba::WHITE, U8x4Rgba::new(255, 255, 255, 255));
}
#[test]
fn f32_named_constants() {
assert_eq!(F32x4Rgba::TRANSPARENT, F32x4Rgba::new(0.0, 0.0, 0.0, 0.0));
assert_eq!(F32x4Rgba::BLACK, F32x4Rgba::new(0.0, 0.0, 0.0, 1.0));
assert_eq!(F32x4Rgba::WHITE, F32x4Rgba::new(1.0, 1.0, 1.0, 1.0));
}
#[test]
fn from_rgb_u32_round_trips() {
let pixel = 0x00FF_8040u32;
let rgba = U8x4Rgba::from_rgb_u32(pixel);
assert_eq!(rgba.r, 0xFF);
assert_eq!(rgba.g, 0x80);
assert_eq!(rgba.b, 0x40);
assert_eq!(rgba.a, 255);
assert_eq!(rgba.to_rgb_u32(), pixel);
}
#[test]
fn from_rgb_u32_ignores_top_byte() {
assert_eq!(
U8x4Rgba::from_rgb_u32(0xDEAD_BEEF).to_rgb_u32(),
U8x4Rgba::from_rgb_u32(0x00AD_BEEF).to_rgb_u32(),
);
}
#[test]
fn to_rgb_u32_discards_alpha() {
let rgba = U8x4Rgba::new(0x10, 0x20, 0x30, 0xAB);
assert_eq!(rgba.to_rgb_u32(), 0x0010_2030);
}
#[test]
fn source_over_is_const() {
const OUT: U8x4Rgba =
U8x4Rgba::new(0, 255, 0, 128).source_over(U8x4Rgba::new(255, 0, 0, 255));
assert_eq!(OUT, U8x4Rgba::new(127, 128, 0, 255));
}
#[test]
fn source_over_opaque_src_returns_src() {
let src = U8x4Rgba::new(255, 0, 0, 255);
let dst = U8x4Rgba::new(0, 0, 255, 255);
assert_eq!(src.source_over(dst), src);
}
#[test]
fn source_over_transparent_src_returns_dst() {
let src = U8x4Rgba::new(255, 0, 0, 0);
let dst = U8x4Rgba::new(0, 0, 255, 255);
assert_eq!(src.source_over(dst), dst);
}
#[test]
fn source_over_both_transparent() {
let out = U8x4Rgba::TRANSPARENT.source_over(U8x4Rgba::TRANSPARENT);
assert_eq!(out, U8x4Rgba::TRANSPARENT);
}
#[test]
fn source_over_opaque_dst_is_exactly_round_to_nearest() {
for src_a in 0..=255u8 {
for s in 0..=255u8 {
for d in (0..=255u8).step_by(5) {
let out =
U8x4Rgba::new(s, s, s, src_a).source_over(U8x4Rgba::new(d, d, d, 255));
let v =
u32::from(s) * u32::from(src_a) + u32::from(d) * (255 - u32::from(src_a));
let expected = ((2 * v + 255) / 510) as u8;
assert_eq!(out.r, expected, "src={s}@{src_a} dst={d}");
assert_eq!(out.a, 255, "opaque destination stays opaque");
}
}
}
}
#[test]
fn source_over_translucent_dst_matches_reference() {
for src_a in (0..=255u8).step_by(7) {
for dst_a in (0..=255u8).step_by(7) {
for s in (0..=255u8).step_by(17) {
for d in (0..=255u8).step_by(17) {
let src = U8x4Rgba::new(s, s, s, src_a);
let dst = U8x4Rgba::new(d, d, d, dst_a);
let out = src.source_over(dst);
let want = source_over_reference(src, dst);
assert!(
out.r.abs_diff(want.r) <= 1 && out.a.abs_diff(want.a) <= 1,
"src={src} dst={dst}: got {out}, reference {want}",
);
assert!(
out.a >= src.a && out.a >= dst.a,
"source-over never reduces coverage",
);
}
}
}
}
}
#[test]
fn source_over_opaque_is_idempotent() {
let src = U8x4Rgba::new(200, 100, 50, 255);
let mut acc = U8x4Rgba::new(10, 20, 30, 255);
for _ in 0..1000 {
acc = src.source_over(acc);
}
assert_eq!(acc, src);
}
#[test]
fn source_over_repeated_does_not_drift_darker() {
let src = U8x4Rgba::new(200, 100, 50, 128);
let mut acc = U8x4Rgba::new(200, 100, 50, 255);
for i in 0..1000 {
acc = src.source_over(acc);
assert_eq!(
acc,
U8x4Rgba::new(200, 100, 50, 255),
"drifted after {i} blends",
);
}
}
#[test]
fn source_over_self_is_a_fixed_point() {
for a in 0..=255u8 {
for v in (0..=255u8).step_by(3) {
let px = U8x4Rgba::new(v, v, v, 255);
let src = U8x4Rgba::new(v, v, v, a);
assert_eq!(src.source_over(px), px, "v={v} a={a}");
}
}
}
#[test]
fn source_over_expected_values() {
let cases: &[(U8x4Rgba, U8x4Rgba, U8x4Rgba)] = &[
(
U8x4Rgba::new(200, 100, 50, 255),
U8x4Rgba::new(10, 20, 30, 255),
U8x4Rgba::new(200, 100, 50, 255),
),
(U8x4Rgba::TRANSPARENT, U8x4Rgba::WHITE, U8x4Rgba::WHITE),
(
U8x4Rgba::new(0, 255, 0, 128),
U8x4Rgba::new(255, 0, 0, 255),
U8x4Rgba::new(127, 128, 0, 255),
),
(U8x4Rgba::WHITE, U8x4Rgba::TRANSPARENT, U8x4Rgba::WHITE),
(
U8x4Rgba::new(255, 255, 255, 128),
U8x4Rgba::TRANSPARENT,
U8x4Rgba::new(255, 255, 255, 128),
),
];
for &(src, dst, expected) in cases {
assert_eq!(src.source_over(dst), expected, "src={src} dst={dst}");
}
}
#[test]
fn is_transparent_opaque() {
assert!(U8x4Rgba::TRANSPARENT.is_transparent());
assert!(!U8x4Rgba::BLACK.is_transparent());
assert!(!U8x4Rgba::new(0, 0, 0, 1).is_transparent());
assert!(U8x4Rgba::new(255, 0, 0, 0).is_transparent());
assert!(F32x4Rgba::TRANSPARENT.is_transparent());
assert!(!F32x4Rgba::BLACK.is_transparent());
}
#[test]
fn is_opaque() {
assert!(U8x4Rgba::BLACK.is_opaque());
assert!(U8x4Rgba::WHITE.is_opaque());
assert!(!U8x4Rgba::TRANSPARENT.is_opaque());
assert!(!U8x4Rgba::new(0, 0, 0, 254).is_opaque());
assert!(F32x4Rgba::WHITE.is_opaque());
assert!(!F32x4Rgba::TRANSPARENT.is_opaque());
}
#[test]
fn from_array() {
let rgba = Rgba::from([1u8, 2, 3, 4]);
assert_eq!(rgba, U8x4Rgba::new(1, 2, 3, 4));
}
#[test]
fn into_array() {
let arr: [u8; 4] = U8x4Rgba::new(1, 2, 3, 4).into();
assert_eq!(arr, [1, 2, 3, 4]);
}
#[test]
fn from_tuple() {
let rgba = Rgba::from((1u8, 2, 3, 4));
assert_eq!(rgba, U8x4Rgba::new(1, 2, 3, 4));
}
#[test]
fn into_tuple() {
let tup: (u8, u8, u8, u8) = U8x4Rgba::new(1, 2, 3, 4).into();
assert_eq!(tup, (1, 2, 3, 4));
}
#[test]
fn as_ref_slice() {
let rgba = U8x4Rgba::new(1, 2, 3, 4);
assert_eq!(rgba.as_ref(), &[1u8, 2, 3, 4]);
}
#[test]
fn as_mut_slice() {
let mut rgba = U8x4Rgba::new(1, 2, 3, 4);
rgba.as_mut()[0] = 10;
assert_eq!(rgba.r, 10);
}
#[test]
fn display_u8() {
let s = format!("{}", U8x4Rgba::new(10, 20, 30, 40));
assert_eq!(s, "rgba(10, 20, 30, 40)");
}
#[test]
fn display_f32() {
let s = format!("{}", F32x4Rgba::new(0.1, 0.2, 0.3, 1.0));
assert_eq!(s, "rgba(0.1, 0.2, 0.3, 1)");
}
#[test]
fn display_premultiplied() {
let s = format!("{}", U8x4Rgba::new(10, 20, 30, 40).premultiply());
assert_eq!(s, "premultiplied rgba(2, 3, 5, 40)");
}
#[test]
fn eq_hash_set() {
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(U8x4Rgba::new(1, 2, 3, 4));
set.insert(U8x4Rgba::new(1, 2, 3, 4));
assert_eq!(set.len(), 1);
set.insert(U8x4Rgba::new(5, 6, 7, 8));
assert_eq!(set.len(), 2);
}
#[test]
fn premultiplied_eq_hash_set() {
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(U8x4Rgba::new(1, 2, 3, 4).premultiply());
set.insert(U8x4Rgba::new(1, 2, 3, 4).premultiply());
assert_eq!(set.len(), 1);
}
#[test]
fn premultiply_u8_matches_tiny_skia() {
assert_eq!(
U8x4Rgba::new(10, 20, 30, 40).premultiply().get(),
U8x4Rgba::new(2, 3, 5, 40),
);
assert_eq!(
U8x4Rgba::new(10, 20, 30, 255).premultiply().get(),
U8x4Rgba::new(10, 20, 30, 255),
);
}
#[test]
fn premultiply_identity_when_opaque() {
for v in (0..=255u8).step_by(5) {
let c = U8x4Rgba::new(v, v, v, 255);
assert_eq!(c.premultiply().get(), c);
assert_eq!(c.premultiply().unpremultiply(), c);
}
}
#[test]
fn premultiply_half_alpha_f32() {
let c = F32x4Rgba::new(0.5, 1.0, 0.0, 0.5).premultiply().get();
assert_eq!(c.r, 0.25);
assert_eq!(c.g, 0.5);
assert_eq!(c.b, 0.0);
assert_eq!(c.a, 0.5);
}
#[test]
fn unpremultiply_round_trips_f32() {
let orig = F32x4Rgba::new(0.5, 0.5, 0.5, 0.5);
let back = orig.premultiply().unpremultiply();
assert!((back.r - orig.r).abs() < 1e-6);
assert!((back.a - orig.a).abs() < 1e-6);
}
#[test]
fn unpremultiply_transparent_returns_transparent() {
assert_eq!(
F32x4Rgba::new(0.5, 0.5, 0.5, 0.0)
.premultiply()
.unpremultiply(),
F32x4Rgba::TRANSPARENT,
);
assert_eq!(
U8x4Rgba::new(255, 255, 255, 0)
.premultiply()
.unpremultiply(),
U8x4Rgba::TRANSPARENT,
);
}
#[test]
fn unpremultiply_u8_is_lossy_and_bounded() {
let mut worst = 0u8;
for a in 1..=255u8 {
for v in (0..=255u8).step_by(3) {
let orig = U8x4Rgba::new(v, v, v, a);
let back = orig.premultiply().unpremultiply();
worst = worst.max(back.r.abs_diff(v));
assert_eq!(back.a, a, "alpha survives the round trip exactly");
}
}
assert!(worst > 0, "if this is 0 the docs are wrong and can relax");
assert!(worst <= 128, "worst case is at alpha = 1; got {worst}");
}
#[test]
fn from_premultiplied_wraps_without_converting() {
let raw = U8x4Rgba::new(2, 3, 5, 40);
assert_eq!(Premultiplied::from_premultiplied(raw).get(), raw);
}
#[test]
fn premultiplied_u8_f32_conversion() {
let p = U8x4Rgba::new(10, 20, 30, 255).premultiply();
let f: PremultipliedF32x4Rgba = p.into();
let back: PremultipliedU8x4Rgba = f.into();
assert_eq!(back, p);
}
#[test]
fn clamp_values() {
let c = F32x4Rgba::new(1.5, -0.5, 2.0, -1.0).clamp();
assert_eq!(c, F32x4Rgba::new(1.0, 0.0, 1.0, 0.0));
}
#[test]
fn clamp_noop_for_valid() {
let c = F32x4Rgba::new(0.5, 0.5, 0.5, 1.0).clamp();
assert_eq!(c, F32x4Rgba::new(0.5, 0.5, 0.5, 1.0));
}
#[test]
fn lerp_identity() {
let a = F32x4Rgba::new(0.2, 0.4, 0.6, 0.8);
assert_eq!(a.lerp(F32x4Rgba::WHITE, 0.0), a);
}
#[test]
fn lerp_full() {
let a = F32x4Rgba::new(0.2, 0.4, 0.6, 0.8);
assert_eq!(a.lerp(F32x4Rgba::WHITE, 1.0), F32x4Rgba::WHITE);
}
#[test]
fn lerp_midpoint() {
let a = F32x4Rgba::new(0.0, 0.0, 0.0, 1.0);
let mid = a.lerp(F32x4Rgba::WHITE, 0.5);
assert!((mid.r - 0.5).abs() < 1e-6);
assert!((mid.a - 1.0).abs() < 1e-6);
}
#[test]
fn lerp_clamps_t() {
let a = F32x4Rgba::BLACK;
assert_eq!(a.lerp(F32x4Rgba::WHITE, 2.0), F32x4Rgba::WHITE);
assert_eq!(a.lerp(F32x4Rgba::WHITE, -1.0), a);
}
#[test]
fn lerp_u8_endpoints_are_exact() {
let a = U8x4Rgba::new(10, 20, 30, 40);
let b = U8x4Rgba::new(200, 100, 50, 255);
assert_eq!(a.lerp(b, 0), a);
assert_eq!(a.lerp(b, 255), b);
}
#[test]
fn lerp_u8_midpoint() {
let out = U8x4Rgba::new(0, 100, 0, 255).lerp(U8x4Rgba::new(255, 200, 0, 255), 128);
assert_eq!(out, U8x4Rgba::new(128, 150, 0, 255));
}
}