Skip to content

Commit

Permalink
Merge #246
Browse files Browse the repository at this point in the history
246: Make most operator traits take their input by value and change `TransferFn` to `TransferFn<T>` r=Ogeon a=Ogeon

This makes all of the traits have the same convention, modeled after the `core::ops` traits, and leaves any cloning to the caller. I considered adding an `Output`type too, but that can go into a separate pass.

## Breaking Change

Almost every trait that takes parameters have changed here, so that may be breaking for non-Copy types. The `clamp_self` method is also gone.


Co-authored-by: Erik Hedvall <erikwhedvall@gmail.com>
  • Loading branch information
bors[bot] and Ogeon authored Jul 18, 2021
2 parents e1546d0 + 7286522 commit 7f712ed
Show file tree
Hide file tree
Showing 36 changed files with 777 additions and 685 deletions.
16 changes: 6 additions & 10 deletions palette/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,19 @@ Palette comes with a number of color operations built in, such as saturate/desat
```rust
use palette::{Hue, Shade, Mix, Hsl, Hsv};

fn transform_color<C>(color: &C, amount: f32) -> C
fn transform_color<C>(color: C, amount: f32) -> C
where
C: Hue + Shade<Scalar=f32> + Mix<Scalar=f32> + Clone,
C: Hue + Shade<Scalar=f32> + Mix<Scalar=f32> + Copy,
f32: Into<C::Hue>,
{
let new_color = color.shift_hue(170.0).lighten(1.0);

// Interpolate between the old and new color.
color.mix(&new_color, amount)
color.mix(new_color, amount)
}

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);
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 Expand Up @@ -317,18 +317,14 @@ impl Clamp for Color {
&& zero_to_one.contains(&self.a)
}

fn clamp(&self) -> Self {
fn clamp(self) -> Self {
Color {
r: self.r.min(1.0).max(0.0),
g: self.g.min(1.0).max(0.0),
b: self.b.min(1.0).max(0.0),
a: self.a.min(1.0).max(0.0),
}
}

fn clamp_self(&mut self) {
*self = self.clamp();
}
}


Expand Down
10 changes: 5 additions & 5 deletions palette/examples/readme_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,31 @@ fn pixels_and_buffers() {
fn color_operations_1() {
use palette::{Hsl, Hsv, Hue, Mix, Shade};

fn transform_color<C>(color: &C, amount: f32) -> C
fn transform_color<C>(color: C, amount: f32) -> C
where
C: Hue + Shade<Scalar = f32> + Mix<Scalar = f32> + Clone,
C: Hue + Shade<Scalar = f32> + Mix<Scalar = f32> + Copy,
f32: Into<C::Hue>,
{
let new_color = color.shift_hue(170.0).lighten(1.0);

// Interpolate between the old and new color.
color.mix(&new_color, amount)
color.mix(new_color, amount)
}

// Write example image
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);
let color = transform_color(hsl_color, amount);
palette::Srgb::from_color(color).into_format()
};

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

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

Expand Down
7 changes: 7 additions & 0 deletions palette/src/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub trait WithAlpha<A>: Sized {
/// let transparent = transparent.with_alpha(0.8f32);
/// assert_eq!(transparent.alpha, 0.8);
/// ```
#[must_use]
fn with_alpha(self, alpha: A) -> Self::WithAlpha;

/// Removes the transparency from the color. If `Self::Color` has
Expand All @@ -111,6 +112,7 @@ pub trait WithAlpha<A>: Sized {
/// let color = transparent.without_alpha();
/// assert_eq!(transparent.color, color);
/// ```
#[must_use]
fn without_alpha(self) -> Self::Color;

/// Splits the color into separate color and transparency values.
Expand All @@ -130,6 +132,7 @@ pub trait WithAlpha<A>: Sized {
/// assert_eq!(transparent.color, color);
/// assert_eq!(transparent.alpha, alpha);
/// ```
#[must_use]
fn split(self) -> (Self::Color, A);

/// Transforms the color into a fully opaque color with a transparency
Expand All @@ -143,6 +146,8 @@ pub trait WithAlpha<A>: Sized {
/// let opaque: Srgba<u8> = color.opaque();
/// assert_eq!(opaque.alpha, 255);
/// ```
#[must_use]
#[inline]
fn opaque(self) -> Self::WithAlpha
where
A: Component,
Expand All @@ -161,6 +166,8 @@ pub trait WithAlpha<A>: Sized {
/// let transparent: Srgba<u8> = color.transparent();
/// assert_eq!(transparent.alpha, 0);
/// ```
#[must_use]
#[inline]
fn transparent(self) -> Self::WithAlpha
where
A: Zero,
Expand Down
45 changes: 25 additions & 20 deletions palette/src/alpha/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,34 @@ impl<C, T> DerefMut for Alpha<C, T> {
}
}

impl<C: Mix> Mix for Alpha<C, C::Scalar> {
impl<C> Mix for Alpha<C, C::Scalar>
where
C: Mix,
{
type Scalar = C::Scalar;

fn mix(&self, other: &Alpha<C, C::Scalar>, factor: C::Scalar) -> Alpha<C, C::Scalar> {
Alpha {
color: self.color.mix(&other.color, factor),
alpha: self.alpha + factor * (other.alpha - self.alpha),
}
#[inline]
fn mix(mut self, other: Alpha<C, C::Scalar>, factor: C::Scalar) -> Alpha<C, C::Scalar> {
self.color = self.color.mix(other.color, factor);
self.alpha = self.alpha + factor * (other.alpha - self.alpha);

self
}
}

impl<C: Shade> Shade for Alpha<C, C::Scalar> {
type Scalar = C::Scalar;

fn lighten(&self, factor: C::Scalar) -> Alpha<C, C::Scalar> {
#[inline]
fn lighten(self, factor: C::Scalar) -> Alpha<C, C::Scalar> {
Alpha {
color: self.color.lighten(factor),
alpha: self.alpha,
}
}

fn lighten_fixed(&self, amount: C::Scalar) -> Alpha<C, C::Scalar> {
#[inline]
fn lighten_fixed(self, amount: C::Scalar) -> Alpha<C, C::Scalar> {
Alpha {
color: self.color.lighten_fixed(amount),
alpha: self.alpha,
Expand All @@ -145,32 +151,34 @@ impl<C: GetHue, T> GetHue for Alpha<C, T> {
}

impl<C: Hue, T: Clone> Hue for Alpha<C, T> {
fn with_hue<H: Into<C::Hue>>(&self, hue: H) -> Alpha<C, T> {
fn with_hue<H: Into<C::Hue>>(self, hue: H) -> Alpha<C, T> {
Alpha {
color: self.color.with_hue(hue),
alpha: self.alpha.clone(),
alpha: self.alpha,
}
}

fn shift_hue<H: Into<C::Hue>>(&self, amount: H) -> Alpha<C, T> {
fn shift_hue<H: Into<C::Hue>>(self, amount: H) -> Alpha<C, T> {
Alpha {
color: self.color.shift_hue(amount),
alpha: self.alpha.clone(),
alpha: self.alpha,
}
}
}

impl<C: Saturate> Saturate for Alpha<C, C::Scalar> {
type Scalar = C::Scalar;

fn saturate(&self, factor: C::Scalar) -> Alpha<C, C::Scalar> {
#[inline]
fn saturate(self, factor: C::Scalar) -> Alpha<C, C::Scalar> {
Alpha {
color: self.color.saturate(factor),
alpha: self.alpha,
}
}

fn saturate_fixed(&self, amount: C::Scalar) -> Alpha<C, C::Scalar> {
#[inline]
fn saturate_fixed(self, amount: C::Scalar) -> Alpha<C, C::Scalar> {
Alpha {
color: self.color.saturate_fixed(amount),
alpha: self.alpha,
Expand All @@ -179,21 +187,18 @@ impl<C: Saturate> Saturate for Alpha<C, C::Scalar> {
}

impl<C: Clamp, T: Component> Clamp for Alpha<C, T> {
#[inline]
fn is_within_bounds(&self) -> bool {
self.color.is_within_bounds() && self.alpha >= T::zero() && self.alpha <= T::max_intensity()
}

fn clamp(&self) -> Alpha<C, T> {
#[inline]
fn clamp(self) -> Self {
Alpha {
color: self.color.clamp(),
alpha: clamp(self.alpha, T::zero(), T::max_intensity()),
}
}

fn clamp_self(&mut self) {
self.color.clamp_self();
self.alpha = clamp(self.alpha, T::zero(), T::max_intensity());
}
}

impl<C: Blend, T: Float> Blend for Alpha<C, T>
Expand Down
2 changes: 2 additions & 0 deletions palette/src/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ where
C::Scalar: Float,
{
/// Apply this blend function to a pair of colors.
#[must_use]
fn apply_to(
self,
source: PreAlpha<C, C::Scalar>,
Expand All @@ -69,6 +70,7 @@ where
C::Scalar: Float,
F: FnOnce(PreAlpha<C, C::Scalar>, PreAlpha<C, C::Scalar>) -> PreAlpha<C, C::Scalar>,
{
#[inline]
fn apply_to(
self,
source: PreAlpha<C, C::Scalar>,
Expand Down
Loading

0 comments on commit 7f712ed

Please sign in to comment.