Skip to content

Commit

Permalink
Merge #190
Browse files Browse the repository at this point in the history
190: Convert documentation to intra doc links, add default whitepoint for Lab/Lch, make code fixups r=Ogeon a=okaneco

Add default D65 WhitePoint to Lab and Lch
Collapse if else statements in color_difference.rs
Use !is_empty instead of `len > 0` comparisons in gradient.rs
Use strip_prefix instead of manually stripping, avoids manual indexing
Remove extraneous clones, into_iters, and closures
Simplify matching on single condition into an if let
Remove format! in favor of to_string
Fix deprecated `image` functions in examples
Change some `as` casts to use direct `from` casts when applicable

Closes #177 

Co-authored-by: okaneco <47607823+okaneco@users.noreply.github.com>
  • Loading branch information
bors[bot] and okaneco committed Nov 21, 2020
2 parents 1b5d639 + af295a6 commit 91ff1d4
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 136 deletions.
2 changes: 1 addition & 1 deletion palette/examples/hue.rs
Expand Up @@ -3,7 +3,7 @@ use palette::{FromColor, Hsl, Hue, Lch, Pixel, Srgb};
fn main() {
let mut image = image::open("example-data/input/fruits.png")
.expect("could not open 'example-data/input/fruits.png'")
.to_rgb();
.to_rgb8();

//Shift hue by 180 degrees as HSL in bottom left part, and as LCh in top
//right part. Notice how LCh manages to preserve the apparent lightness of
Expand Down
4 changes: 2 additions & 2 deletions palette/examples/saturate.rs
Expand Up @@ -5,7 +5,7 @@ use image::{GenericImage, GenericImageView};
fn main() {
let mut image = image::open("example-data/input/cat.png")
.expect("could not open 'example-data/input/cat.png'")
.to_rgb();
.to_rgb8();

let width = image.width();
let height = image.height();
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {
}
}
}

let _ = std::fs::create_dir("example-data/output");
match image.save("example-data/output/saturate.png") {
Ok(()) => println!("see 'example-data/output/saturate.png' for the result"),
Expand Down
2 changes: 1 addition & 1 deletion palette/src/alpha/mod.rs
Expand Up @@ -13,7 +13,7 @@ mod alpha;
/// channel of a color type. The color type itself doesn't need to store the
/// transparency value as it can be transformed into or wrapped in a type that
/// has a representation of transparency. This would typically be done by
/// wrapping it in an [`Alpha`](struct.Alpha.html) instance.
/// wrapping it in an [`Alpha`](crate::Alpha) instance.
///
/// # Deriving
/// The trait is trivial enough to be automatically derived. If the color type
Expand Down
8 changes: 2 additions & 6 deletions palette/src/blend/equations.rs
Expand Up @@ -94,12 +94,8 @@ where
Equation::Add => src_color.component_wise(&dst_color, |a, b| a + b),
Equation::Subtract => src_color.component_wise(&dst_color, |a, b| a - b),
Equation::ReverseSubtract => dst_color.component_wise(&src_color, |a, b| a - b),
Equation::Min => source
.color
.component_wise(&destination.color, |a, b| a.min(b)),
Equation::Max => source
.color
.component_wise(&destination.color, |a, b| a.max(b)),
Equation::Min => source.color.component_wise(&destination.color, Float::min),
Equation::Max => source.color.component_wise(&destination.color, Float::max),
};

let alpha = match self.alpha_equation {
Expand Down
6 changes: 3 additions & 3 deletions palette/src/blend/mod.rs
Expand Up @@ -3,7 +3,7 @@
//! Palette offers both OpenGL style blending equations, as well as most of the
//! SVG composition operators (also common in photo manipulation software). The
//! composition operators are all implemented in the
//! [`Blend`](trait.Blend.html) trait, and ready to use with any appropriate
//! [`Blend`](crate::Blend) trait, and ready to use with any appropriate
//! color type:
//!
//! ```
Expand All @@ -15,7 +15,7 @@
//! ```
//!
//! Blending equations can be defined using the
//! [`Equations`](struct.Equations.html) type, which is then passed to the
//! [`Equations`](crate::blend::Equations) type, which is then passed to the
//! `blend` function, from the `Blend` trait:
//!
//! ```
Expand All @@ -32,7 +32,7 @@
//! let c = a.blend(b, blend_mode);
//! ```
//!
//! Note that blending will use [premultiplied alpha](struct.PreAlpha.html),
//! Note that blending will use [premultiplied alpha](crate::blend::PreAlpha),
//! which may result in loss of some color information in some cases. One such
//! case is that a completely transparent resultant color will become black.

Expand Down
30 changes: 9 additions & 21 deletions palette/src/color_difference.rs
Expand Up @@ -60,29 +60,23 @@ pub fn get_ciede_difference<T: FloatComponent>(this: &LabColorDiff<T>, other: &L

let delta_h_prime: T = if c_one_prime == T::zero() || c_two_prime == T::zero() {
from_f64(0.0)
} else if h_prime_difference <= from_f64(180.0) {
h_two_prime - h_one_prime
} else if h_two_prime <= h_one_prime {
h_two_prime - h_one_prime + from_f64(360.0)
} else {
if h_prime_difference <= from_f64(180.0) {
h_two_prime - h_one_prime
} else {
if h_two_prime <= h_one_prime {
h_two_prime - h_one_prime + from_f64(360.0)
} else {
h_two_prime - h_one_prime - from_f64(360.0)
}
}
h_two_prime - h_one_prime - from_f64(360.0)
};

let delta_big_h_prime = from_f64::<T>(2.0)
* (c_one_prime * c_two_prime).sqrt()
* (delta_h_prime / from_f64(2.0) * pi_over_180).sin();
let h_bar_prime = if c_one_prime == T::zero() || c_two_prime == T::zero() {
h_one_prime + h_two_prime
} else if h_prime_difference > from_f64(180.0) {
(h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0)
} else {
if h_prime_difference > from_f64(180.0) {
(h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0)
} else {
(h_one_prime + h_two_prime) / from_f64(2.0)
}
(h_one_prime + h_two_prime) / from_f64(2.0)
};

let l_bar = (this.l + other.l) / from_f64(2.0);
Expand All @@ -103,13 +97,7 @@ pub fn get_ciede_difference<T: FloatComponent>(this: &LabColorDiff<T>, other: &L
* (-(((h_bar_prime - from_f64(275.0)) / from_f64(25.0))
* ((h_bar_prime - from_f64(275.0)) / from_f64(25.0))))
.exp();
let c_bar_prime_pow_seven = c_bar_prime
* c_bar_prime
* c_bar_prime
* c_bar_prime
* c_bar_prime
* c_bar_prime
* c_bar_prime;
let c_bar_prime_pow_seven = c_bar_prime.powi(7);
let r_c: T = from_f64::<T>(2.0)
* (c_bar_prime_pow_seven / (c_bar_prime_pow_seven + twenty_five_pow_seven)).sqrt();
let r_t = -r_c * (from_f64::<T>(2.0) * delta_theta * pi_over_180).sin();
Expand Down
10 changes: 5 additions & 5 deletions palette/src/component.rs
Expand Up @@ -160,9 +160,9 @@ macro_rules! convert_double_to_uint {
impl IntoComponent<f32> for u8 {
#[inline]
fn into_component(self) -> f32 {
let comp_u = self as u32 + C23;
let comp_u = u32::from(self) + C23;
let comp_f = f32::from_bits(comp_u) - f32::from_bits(C23);
let max_u = core::u8::MAX as u32 + C23;
let max_u = u32::from(core::u8::MAX) + C23;
let max_f = (f32::from_bits(max_u) - f32::from_bits(C23)).recip();
comp_f * max_f
}
Expand All @@ -172,9 +172,9 @@ impl IntoComponent<f32> for u8 {
impl IntoComponent<f64> for u8 {
#[inline]
fn into_component(self) -> f64 {
let comp_u = self as u64 + C52;
let comp_u = u64::from(self) + C52;
let comp_f = f64::from_bits(comp_u) - f64::from_bits(C52);
let max_u = core::u8::MAX as u64 + C52;
let max_u = u64::from(core::u8::MAX) + C52;
let max_f = (f64::from_bits(max_u) - f64::from_bits(C52)).recip();
comp_f * max_f
}
Expand Down Expand Up @@ -218,7 +218,7 @@ macro_rules! convert_uint_to_uint {
impl IntoComponent<f64> for f32 {
#[inline]
fn into_component(self) -> f64 {
self as f64
f64::from(self)
}
}
convert_float_to_uint!(f32; direct (u8, u16); via f64 (u32, u64, u128););
Expand Down
26 changes: 13 additions & 13 deletions palette/src/convert.rs
Expand Up @@ -6,7 +6,7 @@
//! The default minimum requirement is to implement `FromColorUnclamped<Xyz>`, but it can
//! also be customized to make use of generics and have other manual implementations.
//!
//! It is also recommended to derive or implement [`WithAlpha`](../trait.WithAlpha.html),
//! It is also recommended to derive or implement [`WithAlpha`](crate::WithAlpha),
//! to be able to convert between all `Alpha` wrapped color types.
//!
//! ## Configuration Attributes
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<T> Display for OutOfBounds<T> {
///
/// `U: IntoColor<T>` is implemented for every type `T: FromColor<U>`.
///
/// See [`FromColor`](trait.FromColor.html) for more details.
/// See [`FromColor`](crate::convert::FromColor) for more details.
pub trait IntoColor<T>: Sized {
/// Convert into T with values clamped to the color defined bounds
///
Expand All @@ -336,7 +336,7 @@ pub trait IntoColor<T>: Sized {
///
/// `U: IntoColorUnclamped<T>` is implemented for every type `T: FromColorUnclamped<U>`.
///
/// See [`FromColorUnclamped`](trait.FromColorUnclamped.html) for more details.
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for more details.
pub trait IntoColorUnclamped<T>: Sized {
/// Convert into T. The resulting color might be invalid in its color space
///
Expand All @@ -354,7 +354,7 @@ pub trait IntoColorUnclamped<T>: Sized {
///
/// `U: TryIntoColor<T>` is implemented for every type `T: TryFromColor<U>`.
///
/// See [`TryFromColor`](trait.TryFromColor.html) for more details.
/// See [`TryFromColor`](crate::convert::TryFromColor) for more details.
pub trait TryIntoColor<T>: Sized {
/// Convert into T, returning ok if the color is inside of its defined
/// range, otherwise an `OutOfBounds` error is returned which contains
Expand All @@ -379,8 +379,8 @@ pub trait TryIntoColor<T>: Sized {
///
/// `U: FromColor<T>` is implemented for every type `U: FromColorUnclamped<T> + Limited`.
///
/// See [`FromColorUnclamped`](trait.FromColorUnclamped.html) for a lossless version of this trait.
/// See [`TryFromColor`](trait.TryFromColor.html) for a trait that gives an error when the result
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a lossless version of this trait.
/// See [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an error when the result
/// is out of bounds.
///
/// # The Difference Between FromColor and From
Expand All @@ -398,7 +398,7 @@ pub trait TryIntoColor<T>: Sized {
/// traits, while `From` and `Into` would not be possible to blanket implement in the same way.
/// This also reduces the work that needs to be done by macros.
///
/// See the [`convert`](index.html) module for how to implement `FromColorUnclamped` for
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
/// custom colors.
pub trait FromColor<T>: Sized {
/// Convert from T with values clamped to the color defined bounds.
Expand All @@ -414,11 +414,11 @@ pub trait FromColor<T>: Sized {

/// A trait for unchecked conversion of one color from another.
///
/// See [`FromColor`](trait.FromColor.html) for a lossy version of this trait.
/// See [`TryFromColor`](trait.TryFromColor.html) for a trait that gives an error when the result
/// See [`FromColor`](crate::convert::FromColor) for a lossy version of this trait.
/// See [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an error when the result
/// is out of bounds.
///
/// See the [`convert`](index.html) module for how to implement `FromColorUnclamped` for
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
/// custom colors.
pub trait FromColorUnclamped<T>: Sized {
/// Convert from T. The resulting color might be invalid in its color space.
Expand All @@ -437,10 +437,10 @@ pub trait FromColorUnclamped<T>: Sized {
///
/// `U: TryFromColor<T>` is implemented for every type `U: FromColorUnclamped<T> + Limited`.
///
/// See [`FromColor`](trait.FromColor.html) for a lossy version of this trait.
/// See [`FromColorUnclamped`](trait.FromColorUnclamped.html) for a lossless version.
/// See [`FromColor`](crate::convert::FromColor) for a lossy version of this trait.
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a lossless version.
///
/// See the [`convert`](index.html) module for how to implement `FromColorUnclamped` for
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
/// custom colors.
pub trait TryFromColor<T>: Sized {
/// Convert from T, returning ok if the color is inside of its defined
Expand Down
4 changes: 2 additions & 2 deletions palette/src/gradient.rs
Expand Up @@ -31,7 +31,7 @@ impl<C: Mix + Clone> Gradient<C> {
C::Scalar: FromF64,
{
let mut points: Vec<_> = colors.into_iter().map(|c| (C::Scalar::zero(), c)).collect();
assert!(points.len() > 0);
assert!(!points.is_empty());
let step_size = C::Scalar::one() / from_f64(max(points.len() - 1, 1) as f64);

for (i, &mut (ref mut p, _)) in points.iter_mut().enumerate() {
Expand All @@ -45,7 +45,7 @@ impl<C: Mix + Clone> Gradient<C> {
/// be at least one color and they are expected to be ordered by their
/// position value.
pub fn with_domain(colors: Vec<(C::Scalar, C)>) -> Gradient<C> {
assert!(colors.len() > 0);
assert!(!colors.is_empty());

//Maybe sort the colors?
Gradient(colors)
Expand Down
10 changes: 5 additions & 5 deletions palette/src/hsl.rs
Expand Up @@ -21,19 +21,19 @@ use crate::{
};

/// Linear HSL with an alpha component. See the [`Hsla` implementation in
/// `Alpha`](struct.Alpha.html#Hsla).
/// `Alpha`](crate::Alpha#Hsla).
pub type Hsla<S = Srgb, T = f32> = Alpha<Hsl<S, T>, T>;

/// HSL color space.
///
/// The HSL color space can be seen as a cylindrical version of
/// [RGB](rgb/struct.Rgb.html), where the `hue` is the angle around the color
/// [RGB](crate::rgb::Rgb), where the `hue` is the angle around the color
/// cylinder, the `saturation` is the distance from the center, and the
/// `lightness` is the height from the bottom. Its composition makes it
/// especially good for operations like changing green to red, making a color
/// more gray, or making it darker.
///
/// See [HSV](struct.Hsv.html) for a very similar color space, with brightness
/// See [HSV](crate::Hsv) for a very similar color space, with brightness
/// instead of lightness.
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -158,7 +158,7 @@ where
}
}

///<span id="Hsla"></span>[`Hsla`](type.Hsla.html) implementations.
///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
impl<T, A> Alpha<Hsl<Srgb, T>, A>
where
T: FloatComponent,
Expand All @@ -173,7 +173,7 @@ where
}
}

///<span id="Hsla"></span>[`Hsla`](type.Hsla.html) implementations.
///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
impl<S, T, A> Alpha<Hsl<S, T>, A>
where
T: FloatComponent,
Expand Down
10 changes: 5 additions & 5 deletions palette/src/hsv.rs
Expand Up @@ -21,13 +21,13 @@ use crate::{
};

/// Linear HSV with an alpha component. See the [`Hsva` implementation in
/// `Alpha`](struct.Alpha.html#Hsva).
/// `Alpha`](crate::Alpha#Hsva).
pub type Hsva<S = Srgb, T = f32> = Alpha<Hsv<S, T>, T>;

/// HSV color space.
///
/// HSV is a cylindrical version of [RGB](rgb/struct.Rgb.html) and it's very
/// similar to [HSL](struct.Hsl.html). The difference is that the `value`
/// HSV is a cylindrical version of [RGB](crate::rgb::Rgb) and it's very
/// similar to [HSL](crate::Hsl). The difference is that the `value`
/// component in HSV determines the _brightness_ of the color, and not the
/// _lightness_. The difference is that, for example, red (100% R, 0% G, 0% B)
/// and white (100% R, 100% G, 100% B) has the same brightness (or value), but
Expand Down Expand Up @@ -155,7 +155,7 @@ where
}
}

///<span id="Hsva"></span>[`Hsva`](type.Hsva.html) implementations.
///<span id="Hsva"></span>[`Hsva`](crate::Hsva) implementations.
impl<T, A> Alpha<Hsv<Srgb, T>, A>
where
T: FloatComponent,
Expand All @@ -170,7 +170,7 @@ where
}
}

///<span id="Hsva"></span>[`Hsva`](type.Hsva.html) implementations.
///<span id="Hsva"></span>[`Hsva`](crate::Hsva) implementations.
impl<S, T, A> Alpha<Hsv<S, T>, A>
where
T: FloatComponent,
Expand Down
10 changes: 5 additions & 5 deletions palette/src/hwb.rs
Expand Up @@ -21,13 +21,13 @@ use crate::{
};

/// Linear HWB with an alpha component. See the [`Hwba` implementation in
/// `Alpha`](struct.Alpha.html#Hwba).
/// `Alpha`](crate::Alpha#Hwba).
pub type Hwba<S = Srgb, T = f32> = Alpha<Hwb<S, T>, T>;

/// HWB color space.
///
/// HWB is a cylindrical version of [RGB](rgb/struct.Rgb.html) and it's very
/// closely related to [HSV](struct.Hsv.html). It describes colors with a
/// HWB is a cylindrical version of [RGB](crate::rgb::Rgb) and it's very
/// closely related to [HSV](crate::Hsv). It describes colors with a
/// starting hue, then a degree of whiteness and blackness to mix into that
/// base hue.
///
Expand Down Expand Up @@ -161,7 +161,7 @@ where
}
}

///<span id="Hwba"></span>[`Hwba`](type.Hwba.html) implementations.
///<span id="Hwba"></span>[`Hwba`](crate::Hwba) implementations.
impl<T, A> Alpha<Hwb<Srgb, T>, A>
where
T: FloatComponent,
Expand All @@ -176,7 +176,7 @@ where
}
}

///<span id="Hwba"></span>[`Hwba`](type.Hwba.html) implementations.
///<span id="Hwba"></span>[`Hwba`](crate::Hwba) implementations.
impl<S, T, A> Alpha<Hwb<S, T>, A>
where
T: FloatComponent,
Expand Down

0 comments on commit 91ff1d4

Please sign in to comment.