Skip to content

Commit

Permalink
Use RGB space instead of white point in Hwb
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogeon committed Mar 13, 2016
1 parent 269e10d commit 50b813a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 110 deletions.
43 changes: 21 additions & 22 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub trait FromColor<Wp = D65, T = f32>: Sized
}

///Convert from HWB color space
fn from_hwb(inp: Hwb<Wp, T>) -> Self {
Self::from_hsv(Hsv::<(::rgb::standards::Srgb, Wp), T>::from_hwb(inp))
fn from_hwb<S: RgbSpace<WhitePoint=Wp>>(inp: Hwb<S, T>) -> Self {
Self::from_hsv(Hsv::<S, T>::from_hwb(inp))
}

///Convert from Luma
Expand Down Expand Up @@ -107,8 +107,8 @@ pub trait IntoColor<Wp = D65, T = f32>: Sized
}

///Convert into HWB color space
fn into_hwb(self) -> Hwb<Wp, T> {
let hsv: Hsv<(::rgb::standards::Srgb, Wp), T> = self.into_hsv();
fn into_hwb<S: RgbSpace<WhitePoint=Wp>>(self) -> Hwb<S, T> {
let hsv: Hsv<S, T> = self.into_hsv();
Hwb::from_hsv(hsv)
}

Expand Down Expand Up @@ -372,30 +372,29 @@ impl_into_color!(Xyz, from_xyz);
impl_into_color!(Yxy, from_yxy);
impl_into_color!(Lab, from_lab);
impl_into_color!(Lch, from_lch);
impl_into_color!(Hwb, from_hwb);
impl_into_color!(Luma ,from_luma);
impl_into_color_rgb!(LinRgb, from_rgb);
impl_into_color_rgb!(Hsl, from_hsl);
impl_into_color_rgb!(Hsv, from_hsv);
impl_into_color_rgb!(Hwb, from_hwb);


impl_from_trait!(<S, T> (Xyz<S::WhitePoint, T>, into_xyz) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>});
impl_from_trait!(<S, T> (Yxy<S::WhitePoint, T>, into_yxy) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>});
impl_from_trait!(<S, T> (Lab<S::WhitePoint, T>, into_lab) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>});
impl_from_trait!(<S, T> (Lch<S::WhitePoint, T>, into_lch) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>});
impl_from_trait!(<S, T> (Hwb<S::WhitePoint, T>, into_hwb) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>});
impl_from_trait!(<S, T> (Luma<S::WhitePoint, T>, into_luma) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>});
impl_from_trait!(<S, T> (Xyz<S::WhitePoint, T>, into_xyz) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Yxy<S::WhitePoint, T>, into_yxy) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Lab<S::WhitePoint, T>, into_lab) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Lch<S::WhitePoint, T>, into_lch) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Luma<S::WhitePoint, T>, into_luma) => {LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});

impl_from_trait!(<S, T> (LinRgb<S, T>, into_rgb) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S::WhitePoint, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hsl<S, T>, into_hsl) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, LinRgb<S, T>, Hsv<S, T>, Hwb<S::WhitePoint, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hsv<S, T>, into_hsv) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, LinRgb<S, T>, Hsl<S, T>, Hwb<S::WhitePoint, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (LinRgb<S, T>, into_rgb) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hsl<S, T>, into_hsl) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, LinRgb<S, T>, Hsv<S, T>, Hwb<S, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hsv<S, T>, into_hsv) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, LinRgb<S, T>, Hsl<S, T>, Hwb<S, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hwb<S, T>, into_hwb) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, LinRgb<S, T>, Hsl<S, T>, Hsv<S, T>, Luma<S::WhitePoint, T>});

impl_from_trait_other!(<Wp: WhitePoint, T> (Xyz<Wp, T>, into_xyz) => {Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Hwb<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Yxy<Wp, T>, into_yxy) => {Xyz<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Hwb<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Lab<Wp, T>, into_lab) => {Xyz<Wp, T>, Yxy<Wp, T>, Lch<Wp, T>, Hwb<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Lch<Wp, T>, into_lch) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Hwb<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Hwb<Wp, T>, into_hwb) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Luma<Wp, T>, into_luma) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Hwb<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Xyz<Wp, T>, into_xyz) => {Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Yxy<Wp, T>, into_yxy) => {Xyz<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Lab<Wp, T>, into_lab) => {Xyz<Wp, T>, Yxy<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Lch<Wp, T>, into_lch) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Luma<Wp, T>, into_luma) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>});

impl_from_trait_other!(<S: RgbStandard, T> (Xyz<<S::Space as RgbSpace>::WhitePoint, T>, into_xyz) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Yxy<<S::Space as RgbSpace>::WhitePoint, T>, into_yxy) => {Rgb<S, T>});
Expand All @@ -404,16 +403,16 @@ impl_from_trait_other!(<S: RgbStandard, T> (Lch<<S::Space as RgbSpace>::WhitePoi
impl_from_trait_other!(<S: RgbStandard, T> (LinRgb<S::Space, T>, into_rgb) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hsl<S::Space, T>, into_hsl) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hsv<S::Space, T>, into_hsv) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hwb<<S::Space as RgbSpace>::WhitePoint, T>, into_hwb) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hwb<S::Space, T>, into_hwb) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Luma<<S::Space as RgbSpace>::WhitePoint, T>, into_luma) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Rgb<S, T>, |c| Rgb::from_linear(c.into_rgb())) => {
LinRgb<S::Space, T>,
Hsl<S::Space, T>,
Hsv<S::Space, T>,
Hwb<S::Space, T>,
Xyz<<S::Space as RgbSpace>::WhitePoint, T>,
Yxy<<S::Space as RgbSpace>::WhitePoint, T>,
Lab<<S::Space as RgbSpace>::WhitePoint, T>,
Lch<<S::Space as RgbSpace>::WhitePoint, T>,
Hwb<<S::Space as RgbSpace>::WhitePoint, T>,
Luma<<S::Space as RgbSpace>::WhitePoint, T>
});
3 changes: 1 addition & 2 deletions src/equality.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use num::Float;
use approx::ApproxEq;

use {Xyz, Yxy, Lab, Lch, Hwb, Luma, LabHue, RgbHue, flt};
use {Xyz, Yxy, Lab, Lch, Luma, LabHue, RgbHue, flt};
use pixel::GammaRgb;
use white_point::WhitePoint;

Expand Down Expand Up @@ -46,7 +46,6 @@ impl_eq!( Yxy, [y, x, luma] );
impl_eq!( Lab, [l, a, b] );
impl_eq!( Luma, [luma] );
impl_eq!( Lch, [l, chroma, hue] );
impl_eq!( Hwb, [hue, whiteness, blackness] );
impl_eq!( GammaRgb, [red, blue, green, alpha, gamma] );

// For hues diffence is calculated and compared to zero. However due to the way floating point's
Expand Down
4 changes: 3 additions & 1 deletion src/hsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ impl<S, Wp, T> FromColor<Wp, T> for Hsv<S, T>
}
}

fn from_hwb(hwb: Hwb<Wp, T>) -> Self {
fn from_hwb<Sp: RgbSpace<WhitePoint=Wp>>(hwb: Hwb<Sp, T>) -> Self {
let hwb = Hwb::<S, T>::from_hwb(hwb);

let inv = T::one() - hwb.blackness;
// avoid divide by zero
let s = if inv.is_normal() {
Expand Down
Loading

0 comments on commit 50b813a

Please sign in to comment.