Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CIE Luv support #221

Merged
merged 8 commits into from
May 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address PR #221 review comments
Remove debugging statements
Fix use statement formatting
Use `Luv::min_l`, `Luv::max_l` consistently in clamp functions
Comment formatting and accuracy for `Luv::u`, `Luv::v` fields
masonium committed May 9, 2021
commit 0a1fed71d1ef297edc145015467efc906c06a327
2 changes: 1 addition & 1 deletion palette/src/lch.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ use rand::distributions::{Distribution, Standard};
#[cfg(feature = "random")]
use rand::Rng;

use crate::{color_difference::ColorDifference};
use crate::color_difference::ColorDifference;
use crate::color_difference::{get_ciede_difference, LabColorDiff};
use crate::convert::{FromColorUnclamped, IntoColorUnclamped};
use crate::encoding::pixel::RawPixel;
14 changes: 8 additions & 6 deletions palette/src/luv.rs
Original file line number Diff line number Diff line change
@@ -48,10 +48,14 @@ where
/// give the brightest white.
pub l: T,

/// u* goes from -100 to 100.
/// The range of valid u\* varies depending on the values of L\*
/// and v\*, but at its limits u\* is within the interval (-84.0,
/// 176.0).
pub u: T,

/// v* goes from -100 to 100.
/// The range of valid v\* varies depending on the values of L\*
/// and u\*, but at its limits v\* is within the interval (-135.0,
/// 108.0).
pub v: T,

/// The white point associated with the color's illuminant and observer.
@@ -238,11 +242,9 @@ where

let u_prime: T = from_f64(4.0) * color.x * prime_denom_recip;
let u_ref_prime = from_f64(4.0) * w.x * prime_ref_denom_recip;
dbg!(u_ref_prime.to_f64().unwrap());

let v_prime: T = from_f64(9.0) * color.y * prime_denom_recip;
let v_ref_prime = from_f64(9.0) * w.y * prime_ref_denom_recip;
dbg!(v_ref_prime.to_f64().unwrap());

let y_r = color.y / w.y;
let l = if y_r > epsilon {
@@ -291,7 +293,7 @@ where
{
#[rustfmt::skip]
fn is_within_bounds(&self) -> bool {
self.l >= T::zero() && self.l <= Self::max_l() &&
self.l >= Self::min_l() && self.l <= Self::max_l() &&
self.u >= Self::min_u() && self.u <= Self::max_u() &&
self.v >= Self::min_v() && self.v <= Self::max_v()
}
@@ -303,7 +305,7 @@ where
}

fn clamp_self(&mut self) {
self.l = clamp(self.l, T::zero(), from_f64(100.0));
self.l = clamp(self.l, Self::min_l(), Self::max_l());
self.u = clamp(self.u, Self::min_u(), Self::max_u());
self.v = clamp(self.v, Self::min_v(), Self::max_v());
}
6 changes: 1 addition & 5 deletions palette/tests/hsluv_dataset/hsluv_dataset.rs
Original file line number Diff line number Diff line change
@@ -7,13 +7,12 @@ use serde_json;

use palette::convert::IntoColorUnclamped;
use palette::white_point::D65;
use palette::{Lch, Luv, Xyz};
use palette::{Luv, Xyz};
use std::collections::HashMap;

#[derive(Clone, Debug)]
struct HsluvExample {
name: String,
lch: Lch<D65, f64>,
luv: Luv<D65, f64>,
xyz: Xyz<D65, f64>,
}
@@ -29,13 +28,11 @@ fn load_data() -> Examples {
let m = raw_data.as_object().expect("failed to parse dataset");
m.iter().map(|(k, v)| {
let colors = v.as_object().unwrap();
let lch_data: Vec<f64> = colors["lch"].as_array().unwrap().iter().flat_map(|x| x.as_f64()).collect();
let luv_data: Vec<f64> = colors["luv"].as_array().unwrap().iter().flat_map(|x| x.as_f64()).collect();
let xyz_data: Vec<f64> = colors["xyz"].as_array().unwrap().iter().flat_map(|x| x.as_f64()).collect();

(k.clone(), HsluvExample {
name: k.clone(),
lch: Lch::new(lch_data[0], lch_data[1], lch_data[2]),
luv: Luv::new(luv_data[0], luv_data[1], luv_data[2]),
xyz: Xyz::new(xyz_data[0], xyz_data[1], xyz_data[2]),
})
@@ -50,7 +47,6 @@ lazy_static! {
#[test]
pub fn run_xyz_to_luv_tests() {
for (_, v) in TEST_DATA.iter() {
println!("{:?}", v.xyz);
let to_luv: Luv<D65, f64> = v.xyz.into_color_unclamped();
assert_relative_eq!(to_luv, v.luv, epsilon = 0.1);
}