Skip to main content

Gradient

Struct Gradient 

Source
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

  • fire — black → red → orange → yellow → white
  • rainbow — full HSV hue sweep
  • grayscale — black → white

Implementations§

Source§

impl Gradient

Source

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).

Source

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.

Source

pub fn remove_stop(&mut self, index: usize)

Remove the stop at index.

Does nothing if index is out of bounds.

Source

pub fn stops(&self) -> &[GradientStop]

Returns all stops as a slice.

Source

pub fn clear(&mut self)

Remove all stops.

Source

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.

Source

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.

Source

pub fn set_interp(&mut self, mode: GradientInterp) -> &mut Self

Set the interpolation mode.

Returns &mut self for chaining.

Source

pub fn set_color_space(&mut self, space: GradientColorSpace) -> &mut Self

Set the color space used for interpolation.

Returns &mut self for chaining.

Source

pub fn set_wrap(&mut self, wrap: GradientWrap) -> &mut Self

Set the wrap mode.

Returns &mut self for chaining.

Source

pub fn reverse(&mut self) -> &mut Self

Reverse the stop order (mirrors the gradient).

Returns &mut self for chaining.

Source

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.

Source

pub fn two_color(a: impl ToRgba, b: impl ToRgba) -> Self

Construct a two-stop gradient.

Source

pub fn rainbow() -> Self

A full HSV rainbow sweep (red → red via 360°).

Source

pub fn fire() -> Self

A fire color ramp (black → red → orange → yellow → white).

Source

pub fn grayscale() -> Self

A greyscale ramp (black → white).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.