Add luminance pixel format
This commit is contained in:
1 parent
e3c1bf5347
commit
92b521ea9a
2 files changed
+81
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
pub mod linear_rgb;
|
pub mod linear_rgb;
|
||||||
|
pub mod luminance;
|
||||||
pub mod srgb;
|
pub mod srgb;
|
||||||
pub use linear_rgb::*;
|
pub use linear_rgb::*;
|
||||||
|
pub use luminance::*;
|
||||||
pub use srgb::*;
|
pub use srgb::*;
|
||||||
|
|
||||||
pub mod farbfeld;
|
pub mod farbfeld;
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
use ccvl_proc_macros::Pixel;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Pixel)]
|
||||||
|
pub struct Luminance {
|
||||||
|
y: f32
|
||||||
|
}
|
||||||
|
|
||||||
|
const R_COEFF: f32 = 0.2126;
|
||||||
|
const G_COEFF: f32 = 0.7152;
|
||||||
|
const B_COEFF: f32 = 0.0722;
|
||||||
|
|
||||||
|
impl Luminance {
|
||||||
|
pub fn from<T: Into<LinearRGB>>(pixel: T) -> Self {
|
||||||
|
Into::<Luminance>::into(Into::<LinearRGB>::into(pixel))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into<T: From<LinearRGB>>(self) -> T {
|
||||||
|
T::from(Into::<LinearRGB>::into(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LinearRGB> for Luminance {
|
||||||
|
fn from(rgb: LinearRGB) -> Self {
|
||||||
|
Self {
|
||||||
|
y: rgb.r * R_COEFF + rgb.g * G_COEFF + rgb.b * B_COEFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<LinearRGB> for Luminance {
|
||||||
|
fn into(self) -> LinearRGB {
|
||||||
|
LinearRGB::new(self.y, self.y, self.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::Deref for Luminance {
|
||||||
|
type Target = f32;
|
||||||
|
|
||||||
|
fn deref(&self) -> &f32 {
|
||||||
|
&self.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::DerefMut for Luminance {
|
||||||
|
fn deref_mut(&mut self) -> &mut f32 {
|
||||||
|
&mut self.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use proptest::prelude::*;
|
||||||
|
|
||||||
|
macro_rules! prop_assert_approx {
|
||||||
|
( $act:expr, $exp:expr ) => {
|
||||||
|
prop_assert!(($act - $exp).abs() < 1e-6)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
proptest! {
|
||||||
|
#[test]
|
||||||
|
fn to_rgb_copies_y_to_each_channel(y in 0.0f32..1.0) {
|
||||||
|
let res: LinearRGB = Luminance { y }.into();
|
||||||
|
prop_assert_eq!(res.r, y);
|
||||||
|
prop_assert_eq!(res.g, y);
|
||||||
|
prop_assert_eq!(res.b, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn approx_unchanged_from_rgb_round_trip(y in 0.0f32..1.0) {
|
||||||
|
let rgb: LinearRGB = Luminance { y }.into();
|
||||||
|
let res: Luminance = rgb.into();
|
||||||
|
prop_assert_approx!(res.y, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user