use crate::alpha::{AlphaFirst, AlphaLast};
pub trait WithAlpha: Sized + Copy {
#[must_use]
fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self> {
AlphaFirst::with_color(alpha, self)
}
#[must_use]
fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self> {
AlphaLast::with_color(alpha, self)
}
}
impl<C> WithAlpha for C where C: Copy {}
#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
use super::*;
use crate::{
gray::Gray8,
rgb::{HasBlue, HasGreen, HasRed, Rgb888},
};
#[test]
fn rgb888_with_alpha_last() {
let color = Rgb888::from_rgb(1, 2, 3).with_alpha_last(200u8);
assert_eq!((color.red(), color.green(), color.blue()), (1, 2, 3));
assert_eq!(color.alpha(), 200);
}
#[test]
fn rgb888_with_alpha_first() {
let color = Rgb888::from_rgb(1, 2, 3).with_alpha_first(200u8);
assert_eq!((color.red(), color.green(), color.blue()), (1, 2, 3));
assert_eq!(color.alpha(), 200);
}
#[test]
fn works_for_non_rgb_colors_too() {
let color = Gray8::new(128).with_alpha_last(64u8);
assert_eq!(color.alpha(), 64);
}
#[test]
fn alpha_type_need_not_match_color_component_type() {
let color = Rgb888::from_rgb(1, 2, 3).with_alpha_last(0.5f32);
assert_eq!(color.alpha(), 0.5);
}
}