Skip to content

Commit 91ff1d4

Browse files
bors[bot]okaneco
andauthored
Merge #190
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>
2 parents 1b5d639 + af295a6 commit 91ff1d4

File tree

27 files changed

+112
-136
lines changed

27 files changed

+112
-136
lines changed

palette/examples/hue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use palette::{FromColor, Hsl, Hue, Lch, Pixel, Srgb};
33
fn main() {
44
let mut image = image::open("example-data/input/fruits.png")
55
.expect("could not open 'example-data/input/fruits.png'")
6-
.to_rgb();
6+
.to_rgb8();
77

88
//Shift hue by 180 degrees as HSL in bottom left part, and as LCh in top
99
//right part. Notice how LCh manages to preserve the apparent lightness of

palette/examples/saturate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use image::{GenericImage, GenericImageView};
55
fn main() {
66
let mut image = image::open("example-data/input/cat.png")
77
.expect("could not open 'example-data/input/cat.png'")
8-
.to_rgb();
8+
.to_rgb8();
99

1010
let width = image.width();
1111
let height = image.height();
@@ -49,7 +49,7 @@ fn main() {
4949
}
5050
}
5151
}
52-
52+
5353
let _ = std::fs::create_dir("example-data/output");
5454
match image.save("example-data/output/saturate.png") {
5555
Ok(()) => println!("see 'example-data/output/saturate.png' for the result"),

palette/src/alpha/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod alpha;
1313
/// channel of a color type. The color type itself doesn't need to store the
1414
/// transparency value as it can be transformed into or wrapped in a type that
1515
/// has a representation of transparency. This would typically be done by
16-
/// wrapping it in an [`Alpha`](struct.Alpha.html) instance.
16+
/// wrapping it in an [`Alpha`](crate::Alpha) instance.
1717
///
1818
/// # Deriving
1919
/// The trait is trivial enough to be automatically derived. If the color type

palette/src/blend/equations.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ where
9494
Equation::Add => src_color.component_wise(&dst_color, |a, b| a + b),
9595
Equation::Subtract => src_color.component_wise(&dst_color, |a, b| a - b),
9696
Equation::ReverseSubtract => dst_color.component_wise(&src_color, |a, b| a - b),
97-
Equation::Min => source
98-
.color
99-
.component_wise(&destination.color, |a, b| a.min(b)),
100-
Equation::Max => source
101-
.color
102-
.component_wise(&destination.color, |a, b| a.max(b)),
97+
Equation::Min => source.color.component_wise(&destination.color, Float::min),
98+
Equation::Max => source.color.component_wise(&destination.color, Float::max),
10399
};
104100

105101
let alpha = match self.alpha_equation {

palette/src/blend/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Palette offers both OpenGL style blending equations, as well as most of the
44
//! SVG composition operators (also common in photo manipulation software). The
55
//! composition operators are all implemented in the
6-
//! [`Blend`](trait.Blend.html) trait, and ready to use with any appropriate
6+
//! [`Blend`](crate::Blend) trait, and ready to use with any appropriate
77
//! color type:
88
//!
99
//! ```
@@ -15,7 +15,7 @@
1515
//! ```
1616
//!
1717
//! Blending equations can be defined using the
18-
//! [`Equations`](struct.Equations.html) type, which is then passed to the
18+
//! [`Equations`](crate::blend::Equations) type, which is then passed to the
1919
//! `blend` function, from the `Blend` trait:
2020
//!
2121
//! ```
@@ -32,7 +32,7 @@
3232
//! let c = a.blend(b, blend_mode);
3333
//! ```
3434
//!
35-
//! Note that blending will use [premultiplied alpha](struct.PreAlpha.html),
35+
//! Note that blending will use [premultiplied alpha](crate::blend::PreAlpha),
3636
//! which may result in loss of some color information in some cases. One such
3737
//! case is that a completely transparent resultant color will become black.
3838

palette/src/color_difference.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,23 @@ pub fn get_ciede_difference<T: FloatComponent>(this: &LabColorDiff<T>, other: &L
6060

6161
let delta_h_prime: T = if c_one_prime == T::zero() || c_two_prime == T::zero() {
6262
from_f64(0.0)
63+
} else if h_prime_difference <= from_f64(180.0) {
64+
h_two_prime - h_one_prime
65+
} else if h_two_prime <= h_one_prime {
66+
h_two_prime - h_one_prime + from_f64(360.0)
6367
} else {
64-
if h_prime_difference <= from_f64(180.0) {
65-
h_two_prime - h_one_prime
66-
} else {
67-
if h_two_prime <= h_one_prime {
68-
h_two_prime - h_one_prime + from_f64(360.0)
69-
} else {
70-
h_two_prime - h_one_prime - from_f64(360.0)
71-
}
72-
}
68+
h_two_prime - h_one_prime - from_f64(360.0)
7369
};
7470

7571
let delta_big_h_prime = from_f64::<T>(2.0)
7672
* (c_one_prime * c_two_prime).sqrt()
7773
* (delta_h_prime / from_f64(2.0) * pi_over_180).sin();
7874
let h_bar_prime = if c_one_prime == T::zero() || c_two_prime == T::zero() {
7975
h_one_prime + h_two_prime
76+
} else if h_prime_difference > from_f64(180.0) {
77+
(h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0)
8078
} else {
81-
if h_prime_difference > from_f64(180.0) {
82-
(h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0)
83-
} else {
84-
(h_one_prime + h_two_prime) / from_f64(2.0)
85-
}
79+
(h_one_prime + h_two_prime) / from_f64(2.0)
8680
};
8781

8882
let l_bar = (this.l + other.l) / from_f64(2.0);
@@ -103,13 +97,7 @@ pub fn get_ciede_difference<T: FloatComponent>(this: &LabColorDiff<T>, other: &L
10397
* (-(((h_bar_prime - from_f64(275.0)) / from_f64(25.0))
10498
* ((h_bar_prime - from_f64(275.0)) / from_f64(25.0))))
10599
.exp();
106-
let c_bar_prime_pow_seven = c_bar_prime
107-
* c_bar_prime
108-
* c_bar_prime
109-
* c_bar_prime
110-
* c_bar_prime
111-
* c_bar_prime
112-
* c_bar_prime;
100+
let c_bar_prime_pow_seven = c_bar_prime.powi(7);
113101
let r_c: T = from_f64::<T>(2.0)
114102
* (c_bar_prime_pow_seven / (c_bar_prime_pow_seven + twenty_five_pow_seven)).sqrt();
115103
let r_t = -r_c * (from_f64::<T>(2.0) * delta_theta * pi_over_180).sin();

palette/src/component.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ macro_rules! convert_double_to_uint {
160160
impl IntoComponent<f32> for u8 {
161161
#[inline]
162162
fn into_component(self) -> f32 {
163-
let comp_u = self as u32 + C23;
163+
let comp_u = u32::from(self) + C23;
164164
let comp_f = f32::from_bits(comp_u) - f32::from_bits(C23);
165-
let max_u = core::u8::MAX as u32 + C23;
165+
let max_u = u32::from(core::u8::MAX) + C23;
166166
let max_f = (f32::from_bits(max_u) - f32::from_bits(C23)).recip();
167167
comp_f * max_f
168168
}
@@ -172,9 +172,9 @@ impl IntoComponent<f32> for u8 {
172172
impl IntoComponent<f64> for u8 {
173173
#[inline]
174174
fn into_component(self) -> f64 {
175-
let comp_u = self as u64 + C52;
175+
let comp_u = u64::from(self) + C52;
176176
let comp_f = f64::from_bits(comp_u) - f64::from_bits(C52);
177-
let max_u = core::u8::MAX as u64 + C52;
177+
let max_u = u64::from(core::u8::MAX) + C52;
178178
let max_f = (f64::from_bits(max_u) - f64::from_bits(C52)).recip();
179179
comp_f * max_f
180180
}
@@ -218,7 +218,7 @@ macro_rules! convert_uint_to_uint {
218218
impl IntoComponent<f64> for f32 {
219219
#[inline]
220220
fn into_component(self) -> f64 {
221-
self as f64
221+
f64::from(self)
222222
}
223223
}
224224
convert_float_to_uint!(f32; direct (u8, u16); via f64 (u32, u64, u128););

palette/src/convert.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! The default minimum requirement is to implement `FromColorUnclamped<Xyz>`, but it can
77
//! also be customized to make use of generics and have other manual implementations.
88
//!
9-
//! It is also recommended to derive or implement [`WithAlpha`](../trait.WithAlpha.html),
9+
//! It is also recommended to derive or implement [`WithAlpha`](crate::WithAlpha),
1010
//! to be able to convert between all `Alpha` wrapped color types.
1111
//!
1212
//! ## Configuration Attributes
@@ -319,7 +319,7 @@ impl<T> Display for OutOfBounds<T> {
319319
///
320320
/// `U: IntoColor<T>` is implemented for every type `T: FromColor<U>`.
321321
///
322-
/// See [`FromColor`](trait.FromColor.html) for more details.
322+
/// See [`FromColor`](crate::convert::FromColor) for more details.
323323
pub trait IntoColor<T>: Sized {
324324
/// Convert into T with values clamped to the color defined bounds
325325
///
@@ -336,7 +336,7 @@ pub trait IntoColor<T>: Sized {
336336
///
337337
/// `U: IntoColorUnclamped<T>` is implemented for every type `T: FromColorUnclamped<U>`.
338338
///
339-
/// See [`FromColorUnclamped`](trait.FromColorUnclamped.html) for more details.
339+
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for more details.
340340
pub trait IntoColorUnclamped<T>: Sized {
341341
/// Convert into T. The resulting color might be invalid in its color space
342342
///
@@ -354,7 +354,7 @@ pub trait IntoColorUnclamped<T>: Sized {
354354
///
355355
/// `U: TryIntoColor<T>` is implemented for every type `T: TryFromColor<U>`.
356356
///
357-
/// See [`TryFromColor`](trait.TryFromColor.html) for more details.
357+
/// See [`TryFromColor`](crate::convert::TryFromColor) for more details.
358358
pub trait TryIntoColor<T>: Sized {
359359
/// Convert into T, returning ok if the color is inside of its defined
360360
/// range, otherwise an `OutOfBounds` error is returned which contains
@@ -379,8 +379,8 @@ pub trait TryIntoColor<T>: Sized {
379379
///
380380
/// `U: FromColor<T>` is implemented for every type `U: FromColorUnclamped<T> + Limited`.
381381
///
382-
/// See [`FromColorUnclamped`](trait.FromColorUnclamped.html) for a lossless version of this trait.
383-
/// See [`TryFromColor`](trait.TryFromColor.html) for a trait that gives an error when the result
382+
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a lossless version of this trait.
383+
/// See [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an error when the result
384384
/// is out of bounds.
385385
///
386386
/// # The Difference Between FromColor and From
@@ -398,7 +398,7 @@ pub trait TryIntoColor<T>: Sized {
398398
/// traits, while `From` and `Into` would not be possible to blanket implement in the same way.
399399
/// This also reduces the work that needs to be done by macros.
400400
///
401-
/// See the [`convert`](index.html) module for how to implement `FromColorUnclamped` for
401+
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
402402
/// custom colors.
403403
pub trait FromColor<T>: Sized {
404404
/// Convert from T with values clamped to the color defined bounds.
@@ -414,11 +414,11 @@ pub trait FromColor<T>: Sized {
414414

415415
/// A trait for unchecked conversion of one color from another.
416416
///
417-
/// See [`FromColor`](trait.FromColor.html) for a lossy version of this trait.
418-
/// See [`TryFromColor`](trait.TryFromColor.html) for a trait that gives an error when the result
417+
/// See [`FromColor`](crate::convert::FromColor) for a lossy version of this trait.
418+
/// See [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an error when the result
419419
/// is out of bounds.
420420
///
421-
/// See the [`convert`](index.html) module for how to implement `FromColorUnclamped` for
421+
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
422422
/// custom colors.
423423
pub trait FromColorUnclamped<T>: Sized {
424424
/// Convert from T. The resulting color might be invalid in its color space.
@@ -437,10 +437,10 @@ pub trait FromColorUnclamped<T>: Sized {
437437
///
438438
/// `U: TryFromColor<T>` is implemented for every type `U: FromColorUnclamped<T> + Limited`.
439439
///
440-
/// See [`FromColor`](trait.FromColor.html) for a lossy version of this trait.
441-
/// See [`FromColorUnclamped`](trait.FromColorUnclamped.html) for a lossless version.
440+
/// See [`FromColor`](crate::convert::FromColor) for a lossy version of this trait.
441+
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a lossless version.
442442
///
443-
/// See the [`convert`](index.html) module for how to implement `FromColorUnclamped` for
443+
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
444444
/// custom colors.
445445
pub trait TryFromColor<T>: Sized {
446446
/// Convert from T, returning ok if the color is inside of its defined

palette/src/gradient.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<C: Mix + Clone> Gradient<C> {
3131
C::Scalar: FromF64,
3232
{
3333
let mut points: Vec<_> = colors.into_iter().map(|c| (C::Scalar::zero(), c)).collect();
34-
assert!(points.len() > 0);
34+
assert!(!points.is_empty());
3535
let step_size = C::Scalar::one() / from_f64(max(points.len() - 1, 1) as f64);
3636

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

5050
//Maybe sort the colors?
5151
Gradient(colors)

palette/src/hsl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ use crate::{
2121
};
2222

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

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

161-
///<span id="Hsla"></span>[`Hsla`](type.Hsla.html) implementations.
161+
///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
162162
impl<T, A> Alpha<Hsl<Srgb, T>, A>
163163
where
164164
T: FloatComponent,
@@ -173,7 +173,7 @@ where
173173
}
174174
}
175175

176-
///<span id="Hsla"></span>[`Hsla`](type.Hsla.html) implementations.
176+
///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
177177
impl<S, T, A> Alpha<Hsl<S, T>, A>
178178
where
179179
T: FloatComponent,

0 commit comments

Comments
 (0)