pub struct Gradient { /* private fields */ }Expand description
A configurable color gradient evaluator.
Gradient maps a normalized position t (0..1) to an RGBA color
by interpolating between control points (stops). It supports multiple
interpolation modes, color spaces, and wrap modes.
§Construction
use optic_color::*;
// Manually
let mut g = Gradient::new();
g.add_stop(0.0, RED);
g.add_stop(1.0, BLUE);
// Convenience
let g = Gradient::two_color(RED, BLUE);
let g = Gradient::rainbow();§Sampling
use optic_color::*;
let g = Gradient::two_color(BLACK, WHITE);
assert_eq!(g.sample(0.0), BLACK);
assert_eq!(g.sample(1.0), WHITE);
let colors = g.sample_n(5); // 5 evenly-spaced colors§Configuration
use optic_color::*;
let g = Gradient::two_color(RED, BLUE)
.set_color_space(GradientColorSpace::Hsv)
.set_interp(GradientInterp::SmoothStep)
.set_wrap(GradientWrap::PingPong);§Presets
Implementations§
Source§impl Gradient
impl Gradient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty gradient.
Defaults: linear RGB interpolation, clamp wrap mode.
Sampling an empty gradient returns RGBA(0, 0, 0, 0).
Sourcepub fn add_stop(&mut self, position: f32, color: impl ToRgba) -> &mut Self
pub fn add_stop(&mut self, position: f32, color: impl ToRgba) -> &mut Self
Add a stop at position (0..1) with the given color.
Stops are kept sorted by position. If a stop already exists at the same position, the new stop is inserted after it.
Returns &mut self for chaining.
Sourcepub fn remove_stop(&mut self, index: usize)
pub fn remove_stop(&mut self, index: usize)
Remove the stop at index.
Does nothing if index is out of bounds.
Sourcepub fn stops(&self) -> &[GradientStop]
pub fn stops(&self) -> &[GradientStop]
Returns all stops as a slice.
Sourcepub fn sample(&self, t: f32) -> RGBA
pub fn sample(&self, t: f32) -> RGBA
Sample the gradient at position t.
The effective position is determined by the current GradientWrap
mode before lookup. Returns the color of the nearest stop if t
falls outside the stop range after wrapping/clamping.
If the gradient has no stops, returns transparent black.
If the gradient has exactly one stop, returns that color for any t.
Sourcepub fn sample_n(&self, count: usize) -> Vec<RGBA>
pub fn sample_n(&self, count: usize) -> Vec<RGBA>
Sample count evenly-spaced colors across the 0..1 range.
The first sample is at t=0, the last at t=1.
Returns an empty vec if count == 0.
Sourcepub fn set_interp(&mut self, mode: GradientInterp) -> &mut Self
pub fn set_interp(&mut self, mode: GradientInterp) -> &mut Self
Set the interpolation mode.
Returns &mut self for chaining.
Sourcepub fn set_color_space(&mut self, space: GradientColorSpace) -> &mut Self
pub fn set_color_space(&mut self, space: GradientColorSpace) -> &mut Self
Set the color space used for interpolation.
Returns &mut self for chaining.
Sourcepub fn set_wrap(&mut self, wrap: GradientWrap) -> &mut Self
pub fn set_wrap(&mut self, wrap: GradientWrap) -> &mut Self
Set the wrap mode.
Returns &mut self for chaining.
Sourcepub fn reverse(&mut self) -> &mut Self
pub fn reverse(&mut self) -> &mut Self
Reverse the stop order (mirrors the gradient).
Returns &mut self for chaining.
Sourcepub fn from_colors(colors: &[impl ToRgba]) -> Self
pub fn from_colors(colors: &[impl ToRgba]) -> Self
Construct a gradient from evenly-spaced colors.
Each color is placed at i / (len-1) along the 0..1 axis.
If the input slice is empty, returns an empty gradient.