Skip to content

Commit

Permalink
Restore the proper scale of Lab and Lch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogeon committed Mar 6, 2016
1 parent 1fc6d76 commit 0ab1c5f
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 204 deletions.
40 changes: 20 additions & 20 deletions src/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ pub type Laba<Wp, T = f32> = Alpha<Lab<Wp, T>, T>;
///numerical difference.
///
///The parameters of L*a*b* are quite different, compared to many other color
///spaces, so manipulating them manually can be unintuitive.
///spaces, so manipulating them manually may be unintuitive.
#[derive(Debug, PartialEq)]
pub struct Lab<Wp = D65, T = f32>
where T: Float,
Wp: WhitePoint<T>
{
///L* is the lightness of the color. 0.0 gives absolute black and 1.0
///L* is the lightness of the color. 0.0 gives absolute black and 100.0
///give the brightest white.
pub l: T,

///a* goes from red at -1.0 to green at 1.0.
///a* goes from red at -128.0 to green at 127.0.
pub a: T,

///b* goes from yellow at -1.0 to blue at 1.0.
///b* goes from yellow at -128.0 to blue at 127.0.
pub b: T,

///The white point associated with the color's illuminant and observer.
Expand Down Expand Up @@ -136,9 +136,9 @@ impl<Wp, T> FromColor<Wp, T> for Lab<Wp, T>
z = convert(z);

Lab {
l: ( (y * flt(116.0)) - flt(16.0) ) / flt(100.0),
a: ( (x - y) * flt(500.0) ) / flt(128.0),
b: ( (y - z) * flt(200.0) ) / flt(128.0),
l: (y * flt(116.0)) - flt(16.0),
a: (x - y) * flt(500.0),
b: (y - z) * flt(200.0),
white_point: PhantomData,
}
}
Expand All @@ -164,9 +164,9 @@ impl<Wp, T> Limited for Lab<Wp, T>
Wp: WhitePoint<T>
{
fn is_valid(&self) -> bool {
self.l >= T::zero() && self.l <= T::one() &&
self.a >= -T::one() && self.a <= T::one() &&
self.b >= -T::one() && self.b <= T::one()
self.l >= T::zero() && self.l <= flt(100.0) &&
self.a >= flt(-128.0) && self.a <= flt(127.0) &&
self.b >= flt(-128.0) && self.b <= flt(127.0)
}

fn clamp(&self) -> Lab<Wp, T> {
Expand All @@ -176,9 +176,9 @@ impl<Wp, T> Limited for Lab<Wp, T>
}

fn clamp_self(&mut self) {
self.l = clamp(self.l, T::zero(), T::one());
self.a = clamp(self.a, -T::one(), T::one());
self.b = clamp(self.b, -T::one(), T::one());
self.l = clamp(self.l, T::zero(), flt(100.0));
self.a = clamp(self.a, flt(-128.0), flt(127.0));
self.b = clamp(self.b, flt(-128.0), flt(127.0));
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ impl<Wp, T> Shade for Lab<Wp, T>

fn lighten(&self, amount: T) -> Lab<Wp, T> {
Lab {
l: self.l + amount,
l: self.l + amount * flt(100.0),
a: self.a,
b: self.b,
white_point: PhantomData,
Expand Down Expand Up @@ -401,21 +401,21 @@ mod test {
#[test]
fn red() {
let a = Lab::from(Rgb::new(1.0, 0.0, 0.0));
let b = Lab::new(53.23288 / 100.0, 80.09246 / 128.0, 67.2031 / 128.0);
let b = Lab::new(53.23288, 80.09246, 67.2031);
assert_relative_eq!(a, b, epsilon = 0.0001);
}

#[test]
fn green() {
let a = Lab::from(Rgb::new(0.0, 1.0, 0.0));
let b = Lab::new(87.73704 / 100.0, -86.184654 / 128.0, 83.18117 / 128.0);
let b = Lab::new(87.73704, -86.184654, 83.18117);
assert_relative_eq!(a, b, epsilon = 0.0001);
}

#[test]
fn blue() {
let a = Lab::from(Rgb::new(0.0, 0.0, 1.0));
let b = Lab::new(32.302586 / 100.0, 79.19668 / 128.0, -107.863686 / 128.0);
let b = Lab::new(32.302586, 79.19668, -107.863686);
assert_relative_eq!(a, b, epsilon = 0.0001);
}

Expand All @@ -424,9 +424,9 @@ mod test {
assert_ranges!{
Lab;
limited {
l: 0.0 => 1.0,
a: -1.0 => 1.0,
b: -1.0 => 1.0
l: 0.0 => 100.0,
a: -128.0 => 127.0,
b: -128.0 => 127.0
}
limited_min {}
unlimited {}
Expand Down
16 changes: 8 additions & 8 deletions src/lch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::marker::PhantomData;

use {Alpha, Xyz, Lab, LabHue, Hue};
use {Limited, Mix, Shade, GetHue, FromColor, IntoColor, Saturate};
use {clamp};
use {clamp, flt};
use white_point::{WhitePoint, D65};

///CIE L*C*h° with an alpha component. See the [`Lcha` implementation in `Alpha`](struct.Alpha.html#Lcha).
Expand All @@ -24,14 +24,14 @@ pub struct Lch<Wp = D65, T: Float = f32>
where T: Float,
Wp: WhitePoint<T>
{
///L* is the lightness of the color. 0.0 gives absolute black and 1.0
///L* is the lightness of the color. 0.0 gives absolute black and 100.0
///give the brightest white.
pub l: T,

///C* is the colorfulness of the color. It's similar to saturation. 0.0
///gives gray scale colors, and numbers around 1.0-1.41421356 gives fully
///saturated colors. The upper limit of 1.41421356 (or `sqrt(2.0)`) should
///include the whole L*a*b* space and some more.
///gives gray scale colors, and numbers around 128-181 gives fully
///saturated colors. The upper limit of 181 should include the whole
///L*a*b* space and some more.
pub chroma: T,

///The hue of the color, in degrees. Decides if it's red, blue, purple,
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<Wp, T> Shade for Lch<Wp, T>

fn lighten(&self, amount: T) -> Lch<Wp, T> {
Lch {
l: self.l + amount,
l: self.l + amount * flt(100.0),
chroma: self.chroma,
hue: self.hue,
white_point: PhantomData,
Expand Down Expand Up @@ -326,10 +326,10 @@ mod test {
assert_ranges!{
Lch;
limited {
l: 0.0 => 1.0
l: 0.0 => 100.0
}
limited_min {
chroma: 0.0 => 2.0
chroma: 0.0 => 200.0
}
unlimited {
hue: -360.0 => 360.0
Expand Down
6 changes: 1 addition & 5 deletions src/xyz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ impl<Wp, T> FromColor<Wp, T> for Xyz<Wp, T>
xyz
}

fn from_lab(input_lab: Lab<Wp, T>) -> Self {
let mut lab = input_lab.clone();
lab.l = lab.l * flt(100.0);
lab.a = lab.a * flt(128.0);
lab.b = lab.b * flt(128.0);
fn from_lab(lab: Lab<Wp, T>) -> Self {
let y = (lab.l + flt(16.0)) / flt(116.0);
let x = y + (lab.a / flt(500.0));
let z = y - (lab.b / flt(200.0));
Expand Down
Loading

0 comments on commit 0ab1c5f

Please sign in to comment.