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

Improve the hue types a bit #89

Merged
merged 1 commit into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
105 changes: 88 additions & 17 deletions palette/src/hues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,94 @@ macro_rules! make_hues {
($($(#[$doc:meta])+ struct $name:ident;)+) => ($(
$(#[$doc])+
///
///The hue is a circular type, where `0` and `360` is the same, and
///it's normalized to `(-180, 180]` when it's converted to a linear
///number (like `f32`). This makes many calculations easier, but may
///also have some surprising effects if it's expected to act as a
///linear number.
/// The hue is a circular type, where `0` and `360` is the same, and
/// it's normalized to `(-180, 180]` when it's converted to a linear
/// number (like `f32`). This makes many calculations easier, but may
/// also have some surprising effects if it's expected to act as a
/// linear number.
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct $name<T: Float = f32>(T);

impl<T: Float> $name<T> {
///Create a new hue from radians, instead of degrees.
/// Create a new hue from degrees.
#[inline]
pub fn from_degrees(degrees: T) -> $name<T> {
$name(degrees)
}

/// Create a new hue from radians, instead of degrees.
#[inline]
pub fn from_radians(radians: T) -> $name<T> {
$name(radians * cast(180.0) / cast(PI))
}

///Get the hue as degrees, in the range `(-180, 180]`.
/// Get the hue as degrees, in the range `(-180, 180]`.
#[inline]
pub fn to_degrees(self) -> T {
normalize_angle(self.0)
}

///Convert the hue to radians, in the range `(-π, π]`.
/// Convert the hue to radians, in the range `(-π, π]`.
#[inline]
pub fn to_radians(self) -> T {
normalize_angle(self.0) * cast(PI) / cast(180.0)
}

///Convert the hue to positive degrees, in the range `[0, 360)`.
/// Convert the hue to positive degrees, in the range `[0, 360)`.
#[inline]
pub fn to_positive_degrees(self) -> T {
normalize_angle_positive(self.0)
}

///Convert the hue to positive radians, in the range `[0, 2π)`.
/// Convert the hue to positive radians, in the range `[0, 2π)`.
#[inline]
pub fn to_positive_radians(self) -> T {
normalize_angle_positive(self.0) * cast(PI) / cast(180.0)
}

/// Get the internal representation, without normalizing it.
#[inline]
pub fn to_raw_degrees(self) -> T {
self.0
}

/// Get the internal representation as radians, without normalizing it.
#[inline]
pub fn to_raw_radians(self) -> T {
self.0 * cast(PI) / cast(180.0)
}
}

impl<T: Float> From<T> for $name<T> {
#[inline]
fn from(degrees: T) -> $name<T> {
$name(degrees)
}
}

impl Into<f64> for $name<f64> {
#[inline]
fn into(self) -> f64 {
normalize_angle(self.0)
}
}

impl Into<f32> for $name<f32> {
#[inline]
fn into(self) -> f32 {
normalize_angle(self.0)
}
}
impl Into<f32> for $name<f64> {
#[inline]
fn into(self) -> f32 {
normalize_angle(self.0) as f32
}
}

impl<T: Float> PartialEq for $name<T> {
#[inline]
fn eq(&self, other: &$name<T>) -> bool {
let hue_s: T = (*self).to_degrees();
let hue_o: T = (*other).to_degrees();
Expand All @@ -78,6 +106,7 @@ macro_rules! make_hues {
}

impl<T: Float> PartialEq<T> for $name<T> {
#[inline]
fn eq(&self, other: &T) -> bool {
let hue: T = (*self).to_degrees();
hue.eq(&normalize_angle(*other))
Expand All @@ -87,6 +116,7 @@ macro_rules! make_hues {
impl<T: Float> Add<$name<T>> for $name<T> {
type Output = $name<T>;

#[inline]
fn add(self, other: $name<T>) -> $name<T> {
$name(self.0 + other.0)
}
Expand All @@ -95,14 +125,34 @@ macro_rules! make_hues {
impl<T: Float> Add<T> for $name<T> {
type Output = $name<T>;

#[inline]
fn add(self, other: T) -> $name<T> {
$name(self.0 + other)
}
}

impl Add<$name<f32>> for f32 {
type Output = $name<f32>;

#[inline]
fn add(self, other: $name<f32>) -> $name<f32> {
$name(self + other.0)
}
}

impl Add<$name<f64>> for f64 {
type Output = $name<f64>;

#[inline]
fn add(self, other: $name<f64>) -> $name<f64> {
$name(self + other.0)
}
}

impl<T: Float> Sub<$name<T>> for $name<T> {
type Output = $name<T>;

#[inline]
fn sub(self, other: $name<T>) -> $name<T> {
$name(self.0 - other.0)
}
Expand All @@ -111,34 +161,55 @@ macro_rules! make_hues {
impl<T: Float> Sub<T> for $name<T> {
type Output = $name<T>;

#[inline]
fn sub(self, other: T) -> $name<T> {
$name(self.0 - other)
}
}

impl Sub<$name<f32>> for f32 {
type Output = $name<f32>;

#[inline]
fn sub(self, other: $name<f32>) -> $name<f32> {
$name(self - other.0)
}
}

impl Sub<$name<f64>> for f64 {
type Output = $name<f64>;

#[inline]
fn sub(self, other: $name<f64>) -> $name<f64> {
$name(self - other.0)
}
}
)+)
}

make_hues! {
///A hue type for the CIE L\*a\*b\* family of color spaces.
/// A hue type for the CIE L\*a\*b\* family of color spaces.
///
///It's measured in degrees and it's based on the four physiological
///elementary colors _red_, _yellow_, _green_ and _blue_. This makes it
///different from the hue of RGB based color spaces.
/// It's measured in degrees and it's based on the four physiological
/// elementary colors _red_, _yellow_, _green_ and _blue_. This makes it
/// different from the hue of RGB based color spaces.
struct LabHue;

///A hue type for the RGB family of color spaces.
/// A hue type for the RGB family of color spaces.
///
///It's measured in degrees and uses the three additive primaries _red_,
///_green_ and _blue_.
/// It's measured in degrees and uses the three additive primaries _red_,
/// _green_ and _blue_.
struct RgbHue;
}

#[inline]
fn normalize_angle<T: Float>(deg: T) -> T {
let c360 = cast(360.0);
let c180 = cast(180.0);
deg - (((deg + c180) / c360) - T::one()).ceil() * c360
}

#[inline]
fn normalize_angle_positive<T: Float>(deg: T) -> T {
let c360 = cast(360.0);
deg - ((deg / c360).floor() * c360)
Expand Down
82 changes: 42 additions & 40 deletions palette/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
//!
//! ```rust
//! // An alias for Rgb<Srgb>, which is what most pictures store.
//! use palette::{Srgb, Pixel};
//! use palette::{Pixel, Srgb};
//!
//! let orangeish = Srgb::new(1.0, 0.6, 0.0).into_linear();
//! let blueish = Srgb::new(0.0, 0.2, 1.0).into_linear();
//! let whateve_it_becomes = orangeish + blueish;
//!
//! // Encode the result back into sRGB and create a byte array
//! let pixel: [u8;3] = Srgb::from_linear(whateve_it_becomes)
//! let pixel: [u8; 3] = Srgb::from_linear(whateve_it_becomes)
//! .into_format()
//! .into_raw();
//! ```
Expand All @@ -53,7 +53,7 @@
//! This approach comes with the extra benefit of allowing operations to
//! selectively affect the alpha component:
//!
//! ```
//! ```rust
//! use palette::{LinSrgb, LinSrgba};
//!
//! let mut c1 = LinSrgba::new(1.0, 0.5, 0.5, 0.8);
Expand All @@ -63,6 +63,7 @@
//! c1.blue += 0.2; //The color components can easily be accessed
//! c1 = c1 * 0.5; //Scale both the color and the alpha
//! ```
//!

#![doc(html_root_url = "https://docs.rs/palette/0.3.0/palette/")]
#![cfg_attr(feature = "strict", deny(missing_docs))]
Expand Down Expand Up @@ -583,18 +584,18 @@ pub trait Limited {
fn clamp_self(&mut self);
}

///A trait for linear color interpolation.
/// A trait for linear color interpolation.
///
///```
///use palette::{LinSrgb, Mix};
/// ```
/// use palette::{LinSrgb, Mix};
///
///let a = LinSrgb::new(0.0, 0.5, 1.0);
///let b = LinSrgb::new(1.0, 0.5, 0.0);
/// let a = LinSrgb::new(0.0, 0.5, 1.0);
/// let b = LinSrgb::new(1.0, 0.5, 0.0);
///
///assert_eq!(a.mix(&b, 0.0), a);
///assert_eq!(a.mix(&b, 0.5), LinSrgb::new(0.5, 0.5, 0.5));
///assert_eq!(a.mix(&b, 1.0), b);
///```
/// assert_eq!(a.mix(&b, 0.0), a);
/// assert_eq!(a.mix(&b, 0.5), LinSrgb::new(0.5, 0.5, 0.5));
/// assert_eq!(a.mix(&b, 1.0), b);
/// ```
pub trait Mix {
///The type of the mixing factor.
type Scalar: Float;
Expand All @@ -607,16 +608,16 @@ pub trait Mix {
fn mix(&self, other: &Self, factor: Self::Scalar) -> Self;
}

///The `Shade` trait allows a color to be lightened or darkened.
/// The `Shade` trait allows a color to be lightened or darkened.
///
///```
///use palette::{LinSrgb, Shade};
/// ```
/// use palette::{LinSrgb, Shade};
///
///let a = LinSrgb::new(0.4, 0.4, 0.4);
///let b = LinSrgb::new(0.6, 0.6, 0.6);
/// let a = LinSrgb::new(0.4, 0.4, 0.4);
/// let b = LinSrgb::new(0.6, 0.6, 0.6);
///
///assert_eq!(a.lighten(0.1), b.darken(0.1));
///```
/// assert_eq!(a.lighten(0.1), b.darken(0.1));
/// ```
pub trait Shade: Sized {
///The type of the lighten/darken amount.
type Scalar: Float;
Expand All @@ -630,21 +631,21 @@ pub trait Shade: Sized {
}
}

///A trait for colors where a hue may be calculated.
/// A trait for colors where a hue may be calculated.
///
///```
///use palette::{LinSrgb, GetHue};
/// ```
/// use palette::{GetHue, LinSrgb};
///
///let red = LinSrgb::new(1.0f32, 0.0, 0.0);
///let green = LinSrgb::new(0.0f32, 1.0, 0.0);
///let blue = LinSrgb::new(0.0f32, 0.0, 1.0);
///let gray = LinSrgb::new(0.5f32, 0.5, 0.5);
/// let red = LinSrgb::new(1.0f32, 0.0, 0.0);
/// let green = LinSrgb::new(0.0f32, 1.0, 0.0);
/// let blue = LinSrgb::new(0.0f32, 0.0, 1.0);
/// let gray = LinSrgb::new(0.5f32, 0.5, 0.5);
///
///assert_eq!(red.get_hue(), Some(0.0.into()));
///assert_eq!(green.get_hue(), Some(120.0.into()));
///assert_eq!(blue.get_hue(), Some(240.0.into()));
///assert_eq!(gray.get_hue(), None);
///```
/// assert_eq!(red.get_hue(), Some(0.0.into()));
/// assert_eq!(green.get_hue(), Some(120.0.into()));
/// assert_eq!(blue.get_hue(), Some(240.0.into()));
/// assert_eq!(gray.get_hue(), None);
/// ```
pub trait GetHue {
///The kind of hue unit this color space uses.
///
Expand All @@ -670,17 +671,17 @@ pub trait Hue: GetHue {
fn shift_hue<H: Into<Self::Hue>>(&self, amount: H) -> Self;
}

///A trait for colors where the saturation (or chroma) can be manipulated
///without conversion.
/// A trait for colors where the saturation (or chroma) can be manipulated
/// without conversion.
///
///```
///use palette::{Hsv, Saturate};
/// ```
/// use palette::{Hsv, Saturate};
///
///let a = Hsv::new(0.0, 0.25, 1.0);
///let b = Hsv::new(0.0, 1.0, 1.0);
/// let a = Hsv::new(0.0, 0.25, 1.0);
/// let b = Hsv::new(0.0, 1.0, 1.0);
///
///assert_eq!(a.saturate(1.0), b.desaturate(0.5));
///```
/// assert_eq!(a.saturate(1.0), b.desaturate(0.5));
/// ```
pub trait Saturate: Sized {
///The type of the (de)saturation factor.
type Scalar: Float;
Expand Down Expand Up @@ -836,7 +837,8 @@ impl Component for u64 {
}
}

///A convenience function to convert a constant number to Float Type
/// A convenience function to convert a constant number to Float Type
#[inline]
fn cast<T: NumCast, P: ToPrimitive>(prim: P) -> T {
NumCast::from(prim).unwrap()
}