colorgrad-rs

Stars License crates.io Documentation Build Status codecov Total Downloads

Rust color scales library for data visualization, charts, games, maps, generative art and others.

Support This Project

Donate

Index

Usage

Add this to your Cargo.toml

toml colorgrad = "0.5.0"

Custom Gradient

Basic

rust let g = colorgrad::CustomGradient::new().build()?; img

Custom Colors

```rust use colorgrad::Color;

let g = colorgrad::CustomGradient::new() .colors(&[ Color::fromrgbu8(0, 206, 209), Color::fromrgbu8(255, 105, 180), Color::fromrgb(0.274, 0.5, 0.7), Color::fromhsv(50.0, 1.0, 1.0), Color::from_hsv(348.0, 0.9, 0.8), ]) .build()?; ``` img

Using Web Color Format

.html_colors() method accepts named colors, hexadecimal (#rgb, #rgba, #rrggbb, #rrggbbaa), rgb(), rgba(), hsl(), hsla(), hwb(), and hsv().

rust let g = colorgrad::CustomGradient::new() .html_colors(&["#C41189", "#00BFFF", "#FFD700"]) .build()?; img

rust let g = colorgrad::CustomGradient::new() .html_colors(&["gold", "hotpink", "darkturquoise"]) .build()?; img

rust let g = colorgrad::CustomGradient::new() .html_colors(&["rgb(125,110,221)", "rgb(90%,45%,97%)", "hsl(229,79%,85%)"]) .build()?; img

Domain & Color Position

Default domain is [0..1].

```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .build()?;

assert_eq!(g.domain(), (0.0, 1.0)); ``` img

Set the domain to [0..100].

```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[0.0, 100.0]) .build()?;

assert_eq!(g.domain(), (0.0, 100.0)); ``` img

Set the domain to [-1..1].

```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[-1.0, 1.0]) .build()?;

assert_eq!(g.domain(), (-1.0, 1.0)); ``` img

Set exact position for each color. The domain is [0..1].

```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[0.0, 0.7, 1.0]) .build()?;

assert_eq!(g.domain(), (0.0, 1.0)); ``` img

Set exact position for each color. The domain is [15..80].

```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[15.0, 30.0, 80.0]) .build()?;

assert_eq!(g.domain(), (15.0, 80.0)); ``` img

Blending Mode

rust let g = colorgrad::CustomGradient::new() .html_colors(&["#FFF", "#00F"]) .mode(colorgrad::BlendMode::Rgb) .build()?;

Blending Modes

Interpolation Mode

rust let g = colorgrad::CustomGradient::new() .html_colors(&["#C41189", "#00BFFF", "#FFD700"]) .interpolation(colorgrad::Interpolation::Linear) .build()?;

Interpolation Modes

Preset Gradients

All preset gradients are in the domain [0..1]. Uniform B-splines is used to interpolate the colors.

img

Diverging

colorgrad::br_bg() img

colorgrad::pr_gn() img

colorgrad::pi_yg() img

colorgrad::pu_or() img

colorgrad::rd_bu() img

colorgrad::rd_gy() img

colorgrad::rd_yl_bu() img

colorgrad::rd_yl_gn() img

colorgrad::spectral() img

Sequential (Single Hue)

colorgrad::blues() img

colorgrad::greens() img

colorgrad::greys() img

colorgrad::oranges() img

colorgrad::purples() img

colorgrad::reds() img

Sequential (Multi-Hue)

colorgrad::turbo() img

colorgrad::viridis() img

colorgrad::inferno() img

colorgrad::magma() img

colorgrad::plasma() img

colorgrad::cividis() img

colorgrad::warm() img

colorgrad::cool() img

colorgrad::cubehelix_default() img

colorgrad::bu_gn() img

colorgrad::bu_pu() img

colorgrad::gn_bu() img

colorgrad::or_rd() img

colorgrad::pu_bu_gn() img

colorgrad::pu_bu() img

colorgrad::pu_rd() img

colorgrad::rd_pu() img

colorgrad::yl_gn_bu() img

colorgrad::yl_gn() img

colorgrad::yl_or_br() img

colorgrad::yl_or_rd() img

Cyclical

colorgrad::rainbow() img

colorgrad::sinebow() img

Parsing GIMP Gradient

```rust use colorgrad::Color; use std::fs::File; use std::io::BufReader;

let input = File::open("examples/Abstract1.ggr")?; let buf = BufReader::new(input); let fg = Color::fromrgb(0.0, 0.0, 0.0); let bg = Color::fromrgb(1.0, 1.0, 1.0); let (grad, name) = colorgrad::parseggr(buf, &fg, &bg)?;

assert_eq!(name, "Abstract 1"); ```

img

Using the Gradient

Get the domain

```rust let grad = colorgrad::rainbow();

assert_eq!(grad.domain(), (0.0, 1.0)); ```

Get single color at certain position

```rust let grad = colorgrad::blues();

asserteq!(grad.at(0.0).rgbau8(), (247, 251, 255, 255)); asserteq!(grad.at(0.5).rgbau8(), (109, 174, 213, 255)); asserteq!(grad.at(1.0).rgbau8(), (8, 48, 107, 255));

asserteq!(grad.at(0.3).rgbau8(), grad.repeatat(0.3).rgbau8()); asserteq!(grad.at(0.3).rgbau8(), grad.reflectat(0.3).rgbau8());

asserteq!(grad.at(0.7).rgbau8(), grad.repeatat(0.7).rgbau8()); asserteq!(grad.at(0.7).rgbau8(), grad.reflectat(0.7).rgbau8()); ```

The difference of at(), repeat_at() and reflect_at().

Spread Modes

Get n colors evenly spaced across gradient

```rust let grad = colorgrad::rainbow();

for c in grad.colors(10) { println!("{}", c.tohexstring()); } ```

Output:

```console

6e40aa

c83dac

ff5375

ff8c38

c9d33a

7cf659

5dea8d

48b8d0

4775de

6e40aa

```

Hard-Edged Gradient

Convert gradient to hard-edged gradient with 11 segments and 0 smoothness.

rust let g = colorgrad::rainbow().sharp(11, 0.0); img

This is the effect of different smoothness.

img

Examples

Gradient Image

```rust fn main() -> Result<(), Box> { let grad = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .build()?;

let width = 1500;
let height = 70;

let mut imgbuf = image::ImageBuffer::new(width, height);

for (x, _, pixel) in imgbuf.enumerate_pixels_mut() {
    let (r, g, b, a) = grad.at(x as f64 / width as f64).rgba_u8();
    *pixel = image::Rgba([r, g, b, a]);
}

imgbuf.save("gradient.png")?;
Ok(())

} ```

Example output:

img

Colored Noise

```rust use noise::NoiseFn;

fn main() { let scale = 0.015;

let grad = colorgrad::rainbow().sharp(5, 0.15);
let ns = noise::OpenSimplex::new();
let mut imgbuf = image::ImageBuffer::new(600, 350);

for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
    let t = ns.get([x as f64 * scale, y as f64 * scale]);
    let (r, g, b, a) = grad.at(remap(t, -0.5, 0.5, 0.0, 1.0)).rgba_u8();
    *pixel = image::Rgba([r, g, b, a]);
}

imgbuf.save("noise.png").unwrap();

}

// Map t which is in range [a, b] to range [c, d] fn remap(t: f64, a: f64, b: f64, c: f64, d: f64) -> f64 { (t - a) * ((d - c) / (b - a)) + c } ```

Example output:

img

Default Feature

Similar Projects

Projects using colorgrad

Links