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

Make color constructors const and remove or replace all with_wp #239

Merged
merged 1 commit into from
Jul 15, 2021
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
4 changes: 2 additions & 2 deletions palette/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ where
color.mix(&new_color, amount)
}

let new_hsl = transform_color(&Hsl::new(0.00, 0.70, 0.20), 0.8);
let new_hsv = transform_color(&Hsv::new(0.00, 0.82, 0.34), 0.8);
let new_hsl = transform_color(&Hsl::new_srgb(0.00, 0.70, 0.20), 0.8);
let new_hsv = transform_color(&Hsv::new_srgb(0.00, 0.82, 0.34), 0.8);
```

This image shows the transition from the color to `new_color` in HSL and HSV:
Expand Down
21 changes: 19 additions & 2 deletions palette/build/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ pub fn build_colors(writer: &mut File) {
.unwrap_or_else(|| panic!("couldn't get blue for {}", name));

writeln!(writer, "\n///<div style=\"display: inline-block; width: 3em; height: 1em; border: 1px solid black; background: {0};\"></div>", name).unwrap();
writeln!(writer, "pub const {}: crate::rgb::Srgb<u8> = crate::rgb::Srgb {{ red: {}, green: {}, blue: {}, standard: ::core::marker::PhantomData }};", name.to_uppercase(), red, green, blue).unwrap();
writeln!(
writer,
"pub const {}: crate::rgb::Srgb<u8> = crate::rgb::Srgb::new({}, {}, {});",
name.to_uppercase(),
red,
green,
blue
)
.unwrap();

entries.push((name.to_owned(), name.to_uppercase()));
}
Expand Down Expand Up @@ -104,7 +112,16 @@ pub fn build_gradients(writer: &mut File) {
.next()
.and_then(|r| r.trim().parse().ok())
.unwrap_or_else(|| panic!("couldn't get the {}th blue-value for {}", i, name));
write!(writer, "({:.10},{}{{red: {}, green: {}, blue: {}, standard: ::core::marker::PhantomData}}),", (i as f32/number_of_colors as f32), color_type, red, green, blue).unwrap();
write!(
writer,
"({:.10},{}::new({}, {}, {})),",
(i as f32 / number_of_colors as f32),
color_type,
red,
green,
blue
)
.unwrap();
}
writeln!(writer, "], ::core::marker::PhantomData);").unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions palette/examples/readme_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ fn color_operations_1() {
}

// Write example image
let hsl_color = Hsl::new(0.00, 0.70, 0.20);
let hsl_color = Hsl::new_srgb(0.00, 0.70, 0.20);
let hsl_color_at = |amount| {
use palette::FromColor;

let color = transform_color(&hsl_color, amount);
palette::Srgb::from_color(color).into_format()
};

let hsv_color = Hsv::new(0.00, 0.82, 0.34);
let hsv_color = Hsv::new_srgb(0.00, 0.82, 0.34);
let hsv_color_at = |amount| {
use palette::FromColor;

Expand Down
18 changes: 9 additions & 9 deletions palette/src/chromatic_adaptation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! use palette::chromatic_adaptation::AdaptInto;
//!
//!
//! let a = Xyz::<A, f32>::with_wp(0.315756, 0.162732, 0.015905);
//! let a = Xyz::<A, f32>::new(0.315756, 0.162732, 0.015905);
//!
//! //Will convert Xyz<A, f32> to Xyz<C, f32> using Bradford chromatic adaptation
//! let c: Xyz<C, f32> = a.adapt_into();
Expand Down Expand Up @@ -254,11 +254,11 @@ mod test {

#[test]
fn chromatic_adaptation_from_a_to_c() {
let input_a = Xyz::<A, f32>::with_wp(0.315756, 0.162732, 0.015905);
let input_a = Xyz::<A, f32>::new(0.315756, 0.162732, 0.015905);

let expected_bradford = Xyz::<C, f32>::with_wp(0.257963, 0.139776, 0.058825);
let expected_vonkries = Xyz::<C, f32>::with_wp(0.268446, 0.159139, 0.052843);
let expected_xyz_scaling = Xyz::<C, f32>::with_wp(0.281868, 0.162732, 0.052844);
let expected_bradford = Xyz::<C, f32>::new(0.257963, 0.139776, 0.058825);
let expected_vonkries = Xyz::<C, f32>::new(0.268446, 0.159139, 0.052843);
let expected_xyz_scaling = Xyz::<C, f32>::new(0.281868, 0.162732, 0.052844);

let computed_bradford: Xyz<C, f32> = Xyz::adapt_from(input_a);
assert_relative_eq!(expected_bradford, computed_bradford, epsilon = 0.0001);
Expand All @@ -272,11 +272,11 @@ mod test {

#[test]
fn chromatic_adaptation_into_a_to_c() {
let input_a = Xyz::<A, f32>::with_wp(0.315756, 0.162732, 0.015905);
let input_a = Xyz::<A, f32>::new(0.315756, 0.162732, 0.015905);

let expected_bradford = Xyz::<C, f32>::with_wp(0.257963, 0.139776, 0.058825);
let expected_vonkries = Xyz::<C, f32>::with_wp(0.268446, 0.159139, 0.052843);
let expected_xyz_scaling = Xyz::<C, f32>::with_wp(0.281868, 0.162732, 0.052844);
let expected_bradford = Xyz::<C, f32>::new(0.257963, 0.139776, 0.058825);
let expected_vonkries = Xyz::<C, f32>::new(0.268446, 0.159139, 0.052843);
let expected_xyz_scaling = Xyz::<C, f32>::new(0.281868, 0.162732, 0.052844);

let computed_bradford: Xyz<C, f32> = input_a.adapt_into();
assert_relative_eq!(expected_bradford, computed_bradford, epsilon = 0.0001);
Expand Down
4 changes: 2 additions & 2 deletions palette/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ mod tests {

impl<S: RgbSpace> FromColorUnclamped<WithXyz<S>> for Xyz<S::WhitePoint, f64> {
fn from_color_unclamped(_color: WithXyz<S>) -> Xyz<S::WhitePoint, f64> {
Xyz::with_wp(0.0, 1.0, 0.0)
Xyz::new(0.0, 1.0, 0.0)
}
}

Expand Down Expand Up @@ -640,7 +640,7 @@ mod tests {

impl<T: FloatComponent> FromColorUnclamped<WithoutXyz<T>> for Lch<crate::white_point::E, T> {
fn from_color_unclamped(_color: WithoutXyz<T>) -> Lch<crate::white_point::E, T> {
Lch::with_wp(T::one(), T::zero(), T::zero())
Lch::new(T::one(), T::zero(), T::zero())
}
}

Expand Down
6 changes: 3 additions & 3 deletions palette/src/encoding/srgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub struct Srgb;

impl Primaries for Srgb {
fn red<Wp: WhitePoint, T: FloatComponent>() -> Yxy<Wp, T> {
Yxy::with_wp(from_f64(0.6400), from_f64(0.3300), from_f64(0.212656))
Yxy::new(from_f64(0.6400), from_f64(0.3300), from_f64(0.212656))
}
fn green<Wp: WhitePoint, T: FloatComponent>() -> Yxy<Wp, T> {
Yxy::with_wp(from_f64(0.3000), from_f64(0.6000), from_f64(0.715158))
Yxy::new(from_f64(0.3000), from_f64(0.6000), from_f64(0.715158))
}
fn blue<Wp: WhitePoint, T: FloatComponent>() -> Yxy<Wp, T> {
Yxy::with_wp(from_f64(0.1500), from_f64(0.0600), from_f64(0.072186))
Yxy::new(from_f64(0.1500), from_f64(0.0600), from_f64(0.072186))
}
}

Expand Down
88 changes: 54 additions & 34 deletions palette/src/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,30 @@ impl<S, T: Clone> Clone for Hsl<S, T> {
}

impl<T> Hsl<Srgb, T> {
/// HSL for linear sRGB.
pub fn new<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T) -> Hsl<Srgb, T> {
Hsl {
hue: hue.into(),
saturation,
lightness,
standard: PhantomData,
}
/// Create an sRGB HSL color. This method can be used instead of `Hsl::new`
/// to help type inference.
pub fn new_srgb<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T) -> Self {
Self::new_const(hue.into(), saturation, lightness)
}

/// Create an sRGB HSL color. This is the same as `Hsl::new_srgb` without
/// the generic hue type. It's temporary until `const fn` supports traits.
pub const fn new_srgb_const(hue: RgbHue<T>, saturation: T, lightness: T) -> Self {
Self::new_const(hue, saturation, lightness)
}
}

impl<S, T> Hsl<S, T> {
/// Linear HSL.
pub fn with_wp<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T) -> Hsl<S, T> {
/// Create an HSL color.
pub fn new<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T) -> Self {
Self::new_const(hue.into(), saturation, lightness)
}

/// Create an HSL color. This is the same as `Hsl::new` without the generic
/// hue type. It's temporary until `const fn` supports traits.
pub const fn new_const(hue: RgbHue<T>, saturation: T, lightness: T) -> Self {
Hsl {
hue: hue.into(),
hue,
saturation,
lightness,
standard: PhantomData,
Expand All @@ -110,7 +118,7 @@ impl<S, T> Hsl<S, T> {

/// Convert from a `(hue, saturation, lightness)` tuple.
pub fn from_components<H: Into<RgbHue<T>>>((hue, saturation, lightness): (H, T, T)) -> Self {
Self::with_wp(hue, saturation, lightness)
Self::new(hue, saturation, lightness)
}

#[inline]
Expand Down Expand Up @@ -170,21 +178,33 @@ where

///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
impl<T, A> Alpha<Hsl<Srgb, T>, A> {
/// HSL and transparency for linear sRGB.
pub fn new<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T, alpha: A) -> Self {
Alpha {
color: Hsl::new(hue, saturation, lightness),
alpha,
}
/// Create an sRGB HSL color with transparency. This method can be used
/// instead of `Hsla::new` to help type inference.
pub fn new_srgb<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T, alpha: A) -> Self {
Self::new_const(hue.into(), saturation, lightness, alpha)
}

/// Create an sRGB HSL color with transparency. This is the same as
/// `Hsla::new_srgb` without the generic hue type. It's temporary until
/// `const fn` supports traits.
pub const fn new_srgb_const(hue: RgbHue<T>, saturation: T, lightness: T, alpha: A) -> Self {
Self::new_const(hue, saturation, lightness, alpha)
}
}

///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
impl<S, T, A> Alpha<Hsl<S, T>, A> {
/// Linear HSL and transparency.
pub fn with_wp<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T, alpha: A) -> Self {
/// Create an HSL color with transparency.
pub fn new<H: Into<RgbHue<T>>>(hue: H, saturation: T, lightness: T, alpha: A) -> Self {
Self::new_const(hue.into(), saturation, lightness, alpha)
}

/// Create an HSL color with transparency. This is the same as `Hsla::new`
/// without the generic hue type. It's temporary until `const fn` supports
/// traits.
pub const fn new_const(hue: RgbHue<T>, saturation: T, lightness: T, alpha: A) -> Self {
Alpha {
color: Hsl::with_wp(hue, saturation, lightness),
color: Hsl::new_const(hue, saturation, lightness),
alpha,
}
}
Expand All @@ -203,7 +223,7 @@ impl<S, T, A> Alpha<Hsl<S, T>, A> {
pub fn from_components<H: Into<RgbHue<T>>>(
(hue, saturation, lightness, alpha): (H, T, T, A),
) -> Self {
Self::with_wp(hue, saturation, lightness, alpha)
Self::new(hue, saturation, lightness, alpha)
}
}

Expand Down Expand Up @@ -479,7 +499,7 @@ where
T: Zero,
{
fn default() -> Hsl<S, T> {
Hsl::with_wp(RgbHue::from(T::zero()), T::zero(), T::zero())
Hsl::new(RgbHue::from(T::zero()), T::zero(), T::zero())
}
}

Expand Down Expand Up @@ -684,8 +704,8 @@ mod test {
#[test]
fn red() {
let a = Hsl::from_color(Srgb::new(1.0, 0.0, 0.0));
let b = Hsl::new(0.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new(0.0, 1.0, 1.0));
let b = Hsl::new_srgb(0.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new_srgb(0.0, 1.0, 1.0));

assert_relative_eq!(a, b);
assert_relative_eq!(a, c);
Expand All @@ -694,8 +714,8 @@ mod test {
#[test]
fn orange() {
let a = Hsl::from_color(Srgb::new(1.0, 0.5, 0.0));
let b = Hsl::new(30.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new(30.0, 1.0, 1.0));
let b = Hsl::new_srgb(30.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new_srgb(30.0, 1.0, 1.0));

assert_relative_eq!(a, b);
assert_relative_eq!(a, c);
Expand All @@ -704,8 +724,8 @@ mod test {
#[test]
fn green() {
let a = Hsl::from_color(Srgb::new(0.0, 1.0, 0.0));
let b = Hsl::new(120.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new(120.0, 1.0, 1.0));
let b = Hsl::new_srgb(120.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new_srgb(120.0, 1.0, 1.0));

assert_relative_eq!(a, b);
assert_relative_eq!(a, c);
Expand All @@ -714,8 +734,8 @@ mod test {
#[test]
fn blue() {
let a = Hsl::from_color(Srgb::new(0.0, 0.0, 1.0));
let b = Hsl::new(240.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new(240.0, 1.0, 1.0));
let b = Hsl::new_srgb(240.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new_srgb(240.0, 1.0, 1.0));

assert_relative_eq!(a, b);
assert_relative_eq!(a, c);
Expand All @@ -724,8 +744,8 @@ mod test {
#[test]
fn purple() {
let a = Hsl::from_color(Srgb::new(0.5, 0.0, 1.0));
let b = Hsl::new(270.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new(270.0, 1.0, 1.0));
let b = Hsl::new_srgb(270.0, 1.0, 0.5);
let c = Hsl::from_color(Hsv::new_srgb(270.0, 1.0, 1.0));

assert_relative_eq!(a, b);
assert_relative_eq!(a, c);
Expand Down Expand Up @@ -762,7 +782,7 @@ mod test {
#[cfg(feature = "serializing")]
#[test]
fn serialize() {
let serialized = ::serde_json::to_string(&Hsl::new(0.3, 0.8, 0.1)).unwrap();
let serialized = ::serde_json::to_string(&Hsl::new_srgb(0.3, 0.8, 0.1)).unwrap();

assert_eq!(
serialized,
Expand Down
Loading