Skip to content

Commit

Permalink
Add conversion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Aug 19, 2024
1 parent 59669da commit 355b199
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
18 changes: 18 additions & 0 deletions palette/src/encoding/adobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,23 @@ mod test {
let half_to_linear = AdobeRgb::into_linear(0.5);
assert_relative_eq!(half_to_linear, 0.21775552, epsilon = 0.0000001);
}

#[test]
fn u8_to_f32_to_u8() {
for expected in 0u8..=255u8 {
let linear: f32 = AdobeRgb::into_linear(expected);
let result: u8 = AdobeRgb::from_linear(linear);
assert_eq!(result, expected);
}
}

#[test]
fn u8_to_f64_to_u8() {
for expected in 0u8..=255u8 {
let linear: f64 = AdobeRgb::into_linear(expected);
let result: u8 = AdobeRgb::from_linear(linear);
assert_eq!(result, expected);
}
}
}
}
34 changes: 32 additions & 2 deletions palette/src/encoding/p3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ pub struct DciP3Plus<F>(PhantomData<F>);

impl<T: Real, F> Primaries<T> for DciP3Plus<F> {
fn red() -> Yxy<Any, T> {
Yxy::new(T::from_f64(0.740), T::from_f64(0.270), T::from_f64(0.203986))
Yxy::new(
T::from_f64(0.740),
T::from_f64(0.270),
T::from_f64(0.203986),
)
}
fn green() -> Yxy<Any, T> {
Yxy::new(T::from_f64(0.220), T::from_f64(0.780), T::from_f64(0.882591))
Yxy::new(
T::from_f64(0.220),
T::from_f64(0.780),
T::from_f64(0.882591),
)
}
fn blue() -> Yxy<Any, T> {
Yxy::new(
Expand Down Expand Up @@ -311,4 +319,26 @@ mod test {
assert_relative_eq!(red + green + blue, DciP3::get_xyz(), epsilon = 0.00001)
}
}

mod transfer {
use crate::encoding::{FromLinear, IntoLinear, P3Gamma};

#[test]
fn u8_to_f32_to_u8() {
for expected in 0u8..=255u8 {
let linear: f32 = P3Gamma::into_linear(expected);
let result: u8 = P3Gamma::from_linear(linear);
assert_eq!(result, expected);
}
}

#[test]
fn u8_to_f64_to_u8() {
for expected in 0u8..=255u8 {
let linear: f64 = P3Gamma::into_linear(expected);
let result: u8 = P3Gamma::from_linear(linear);
assert_eq!(result, expected);
}
}
}
}
22 changes: 22 additions & 0 deletions palette/src/encoding/prophoto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,26 @@ mod test {
assert_relative_eq!(red + green + blue, D50::get_xyz(), epsilon = 0.0001);
}
}

mod transfer {
use crate::encoding::{FromLinear, IntoLinear, ProPhotoRgb};

#[test]
fn u16_to_f32_to_u16() {
for expected in 0u16..=65535u16 {
let linear: f32 = ProPhotoRgb::into_linear(expected as f32 / 65535.0);
let result: u16 = ProPhotoRgb::from_linear(linear);
assert_eq!(result, expected);
}
}

#[test]
fn u16_to_f64_to_u16() {
for expected in 0u16..=65535u16 {
let linear: f64 = ProPhotoRgb::into_linear(expected as f64 / 65535.0);
let result: u16 = ProPhotoRgb::from_linear(linear);
assert_eq!(result, expected);
}
}
}
}

0 comments on commit 355b199

Please sign in to comment.