Struct palette::gradient::Gradient[][src]

pub struct Gradient<C, T = Vec<(<C as Mix>::Scalar, C)>>(_, _)
where
    C: Mix + Clone,
    T: AsRef<[(C::Scalar, C)]>
;
Expand description

A linear interpolation between colors.

It’s used to smoothly transition between a series of colors, that can be either evenly spaced or have customized positions. The gradient is continuous between the control points, but it’s possible to iterate over a number of evenly spaced points using the take method. Any point outside the domain of the gradient will have the same color as the closest control point.

Implementations

Get a color from the gradient. The color of the closest control point will be returned if i is outside the domain.

Create a gradient of colors with custom spacing and domain. There must be at least one color and they are expected to be ordered by their position value.

Take n evenly spaced colors from the gradient, as an iterator. The iterator includes both ends of the gradient, for n > 1, or just the lower end of the gradient for n = 0.

For example, take(5) will include point 0.0 of the gradient, three intermediate colors, and point 1.0 spaced apart at 1/4 the distance between colors 0.0 and 1.0 on the gradient.

use approx::assert_relative_eq;
use palette::{Gradient, LinSrgb};

let gradient = Gradient::new(vec![
    LinSrgb::new(1.0, 1.0, 0.0),
    LinSrgb::new(0.0, 0.0, 1.0),
]);

let taken_colors: Vec<_> = gradient.take(5).collect();
let colors = vec![
    LinSrgb::new(1.0, 1.0, 0.0),
    LinSrgb::new(0.75, 0.75, 0.25),
    LinSrgb::new(0.5, 0.5, 0.5),
    LinSrgb::new(0.25, 0.25, 0.75),
    LinSrgb::new(0.0, 0.0, 1.0),
];
for (c1, c2) in taken_colors.iter().zip(colors.iter()) {
    assert_relative_eq!(c1, c2);
}

Slice this gradient to limit its domain.

Get the limits of this gradient’s domain.

Create a gradient of evenly spaced colors with the domain [0.0, 1.0]. There must be at least one color.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Convert the source color to the destination color using the specified method Read more

Convert the source color to the destination color using the bradford method by default Read more

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Convert into T with values clamped to the color defined bounds Read more

Convert into T. The resulting color might be invalid in its color space Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more