Files
ccvl/src/image/srgb.rs
T

65 lines
1.1 KiB
Rust

use ccvl_proc_macros::Pixel;
#[derive(Clone, Copy, Debug, PartialEq, Pixel)]
pub struct SRGBA {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Pixel)]
pub struct SRGB {
pub r: f32,
pub g: f32,
pub b: f32,
}
impl SRGBA {
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
}
impl Into<SRGB> for SRGBA {
fn into(self) -> SRGB {
SRGB::new(self.r, self.g, self.b)
}
}
impl SRGB {
pub fn new(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b }
}
}
impl Into<SRGBA> for SRGB {
fn into(self) -> SRGBA {
SRGBA::new(self.r, self.g, self.b, 1.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn srgb_from_srgba_drops_alpha()
{
let srgb: SRGB = SRGBA::new(0.1, 0.2, 0.3, 0.4).into();
assert_eq!(srgb.r, 0.1);
assert_eq!(srgb.g, 0.2);
assert_eq!(srgb.b, 0.3);
}
#[test]
fn srgba_from_srgb_adds_alpha_1()
{
let srgba: SRGBA = SRGB::new(0.1, 0.2, 0.3).into();
assert_eq!(srgba.r, 0.1);
assert_eq!(srgba.g, 0.2);
assert_eq!(srgba.b, 0.3);
assert_eq!(srgba.a, 1.0);
}
}