pub trait Saturate: Sized {
    type Scalar: Float;

    fn saturate(&self, factor: Self::Scalar) -> Self;
    fn saturate_fixed(&self, amount: Self::Scalar) -> Self;

    fn desaturate(&self, factor: Self::Scalar) -> Self { ... }
    fn desaturate_fixed(&self, amount: Self::Scalar) -> Self { ... }
}
Expand description

A trait for colors where the saturation (or chroma) can be manipulated without conversion.

The trait’s functions are split into two groups of functions: relative and fixed/absolute.

The relative functions, saturate and saturate, scale the saturation towards the maximum saturation value and minimum saturation value, respectively. This means that for a color with 50% saturation, if saturate(0.5) is applied to it, the color will scale halfway to the maximum value of 100% resulting in a new saturation value of 75%. desaturate(0.5) applied to the original color will result in a new color with 25% saturation since the saturation moves halfway toward the minimum value of 0%.

The fixed or absolute functions, saturate_fixed and desaturate_fixed, increase or descrease the saturation by an amount that is independent of the current saturation of the color. So for a color with 50% saturation, if saturate_fixed(0.5) is applied to it, the color will have 50% saturation added to its saturation value resulting in a new value of 100%. desaturate_fixed(0.5) will result in a new color with saturation of 0% since 50% saturation is subtracted from the original value of 50%.

use approx::assert_relative_eq;
use palette::{Hsv, Saturate};

let a = Hsv::new(0.0, 0.5, 1.0);

assert_relative_eq!(a.saturate(0.5).saturation, 0.75);
assert_relative_eq!(a.desaturate_fixed(0.5).saturation, 0.0);

Required Associated Types

The type of the (de)saturation modifier.

Required Methods

Scale the color towards the maximum saturation by factor, a value ranging from 0.0 to 1.0.

use approx::assert_relative_eq;
use palette::{Hsl, Saturate};

let color = Hsl::new(0.0, 0.5, 0.5);
assert_relative_eq!(color.saturate(0.5).saturation, 0.75);

Increase the saturation by amount, a value ranging from 0.0 to 1.0.

use approx::assert_relative_eq;
use palette::{Hsl, Saturate};

let color = Hsl::new(0.0, 0.4, 0.5);
assert_relative_eq!(color.saturate_fixed(0.2).saturation, 0.6);

Provided Methods

Scale the color towards the minimum saturation by factor, a value ranging from 0.0 to 1.0.

use approx::assert_relative_eq;
use palette::{Hsv, Saturate};

let color = Hsv::new(0.0, 0.5, 0.5);
assert_relative_eq!(color.desaturate(0.5).saturation, 0.25);

Increase the saturation by amount, a value ranging from 0.0 to 1.0.

use approx::assert_relative_eq;
use palette::{Hsv, Saturate};

let color = Hsv::new(0.0, 0.4, 0.5);
assert_relative_eq!(color.desaturate_fixed(0.2).saturation, 0.2);

Implementors