diff --git a/ext/canvas/image_ops.rs b/ext/canvas/image_ops.rs index bbd6fcc55c..2709df97aa 100644 --- a/ext/canvas/image_ops.rs +++ b/ext/canvas/image_ops.rs @@ -595,3 +595,51 @@ pub(crate) fn to_srgb_from_icc_profile( }, } } + +#[cfg(test)] +mod tests { + use super::*; + use image::Rgba; + + #[test] + fn test_premultiply_alpha() { + let rgba = Rgba::([255, 128, 0, 128]); + let rgba = rgba.premultiply_alpha(); + assert_eq!(rgba, Rgba::([128, 64, 0, 128])); + + let rgba = Rgba::([255, 255, 255, 255]); + let rgba = rgba.premultiply_alpha(); + assert_eq!(rgba, Rgba::([255, 255, 255, 255])); + } + + #[test] + fn test_unpremultiply_alpha() { + let rgba = Rgba::([127, 0, 0, 127]); + let rgba = rgba.unpremultiply_alpha(); + assert_eq!(rgba, Rgba::([255, 0, 0, 127])); + } + + #[test] + fn test_apply_conversion_matrix_srgb_to_display_p3() { + let (r, g, b) = apply_conversion_matrix_srgb_to_display_p3(255_u8, 0, 0); + assert_eq!(r, 234); + assert_eq!(g, 51); + assert_eq!(b, 35); + + let (r, g, b) = apply_conversion_matrix_srgb_to_display_p3(0_u8, 255, 0); + assert_eq!(r, 117); + assert_eq!(g, 251); + assert_eq!(b, 76); + + let (r, g, b) = apply_conversion_matrix_srgb_to_display_p3(0_u8, 0, 255); + assert_eq!(r, 0); + assert_eq!(g, 0); + assert_eq!(b, 245); + + let (r, g, b) = + apply_conversion_matrix_srgb_to_display_p3(255_u8, 255, 255); + assert_eq!(r, 255); + assert_eq!(g, 255); + assert_eq!(b, 255); + } +}