#color #scheme #gradient #colormap #color-scales

colorgrad

Rust color scales library for charts, maps, data visualization and creative coding

2 unstable releases

0.2.0 Jan 11, 2021
0.1.0 Dec 23, 2020

#72 in Images

MIT/Apache

35KB
432 lines

colorgrad-rs

crates.io Documentation Build Status Build Status codecov

Rust color scales library for charts, maps, data-visualization and creative coding.

Index

Usage

Add colorgrad to your Cargo.toml

[dependencies]
colorgrad = "0.2.0"

Custom Gradient

Basic

let g = colorgrad::CustomGradient::new().build().unwrap();

img

Custom Colors

use colorgrad::Color;

let g = colorgrad::CustomGradient::new()
    .colors(&[
        Color::from_rgb_u8(0, 206, 209),
        Color::from_rgb_u8(255, 105, 180),
        Color::from_rgb(0.274, 0.5, 0.7),
        Color::from_hsv(50., 1., 1.),
        Color::from_hsv(348., 0.9, 0.8),
    ])
    .build()
    .unwrap();

img

Using Web Color Format

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

let g = colorgrad::CustomGradient::new()
    .html_colors(&["#c41189", "#00BFFF", "#FFD700"])
    .build()
    .unwrap();

img

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

img

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

img

Domain & Color Position

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

assert_eq!(g.domain(), (0., 1.));

img

let g = colorgrad::CustomGradient::new()
    .html_colors(&["deeppink", "gold", "seagreen"])
    .domain(&[0., 100.])
    .build()
    .unwrap();

assert_eq!(g.domain(), (0., 100.));

img

let g = colorgrad::CustomGradient::new()
    .html_colors(&["deeppink", "gold", "seagreen"])
    .domain(&[-1., 1.])
    .build()
    .unwrap();

assert_eq!(g.domain(), (-1., 1.));

img

let g = colorgrad::CustomGradient::new()
    .html_colors(&["deeppink", "gold", "seagreen"])
    .domain(&[0., 0.7, 1.])
    .build()
    .unwrap();

assert_eq!(g.domain(), (0., 1.));

img

let g = colorgrad::CustomGradient::new()
    .html_colors(&["deeppink", "gold", "seagreen"])
    .domain(&[15., 30., 80.])
    .build()
    .unwrap();

assert_eq!(g.domain(), (15., 80.));

img

Blending Mode

let g = colorgrad::CustomGradient::new()
    .html_colors(&["#ff0", "#008ae5"])
    .mode(colorgrad::BlendMode::Rgb)
    .build()
    .unwrap();

img

let g = colorgrad::CustomGradient::new()
    .html_colors(&["#ff0", "#008ae5"])
    .mode(colorgrad::BlendMode::Lrgb)
    .build()
    .unwrap();

img

let g = colorgrad::CustomGradient::new()
    .html_colors(&["#ff0", "#008ae5"])
    .mode(colorgrad::BlendMode::Hsv)
    .build()
    .unwrap();

img

Preset Gradients

All preset gradients are in the domain 0..1.

Diverging

colorgrad::brbg() img

colorgrad::prgn() img

colorgrad::piyg() img

colorgrad::puor() img

colorgrad::rdbu() img

colorgrad::rdgy() img

colorgrad::rdylbu() img

colorgrad::rdylgn() 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::bugn() img

colorgrad::bupu() img

colorgrad::gnbu() img

colorgrad::orrd() img

colorgrad::pubugn() img

colorgrad::pubu() img

colorgrad::purd() img

colorgrad::rdpu() img

colorgrad::ylgnbu() img

colorgrad::ylgn() img

colorgrad::ylorbr() img

colorgrad::ylorrd() img

Cyclical

colorgrad::rainbow() img

colorgrad::sinebow() img

Hard-Edged Gradient

let g1 = colorgrad::CustomGradient::new()
    .html_colors(&["#18dbf4", "#f6ff56"])
    .build()
    .unwrap();

let g2 = g1.sharp(7);

img

img

let g = colorgrad::spectral().sharp(19);

img

Examples

Gradient Image

extern crate colorgrad;
extern crate image;

fn main() {
    let grad = colorgrad::CustomGradient::new()
        .html_colors(&["deeppink", "gold", "seagreen"])
        .build()
        .unwrap();

    let w = 1500;
    let h = 70;
    let fw = w as f64;

    let mut imgbuf = image::ImageBuffer::new(w, h);

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

    imgbuf.save("gradient.png").unwrap();
}

img

Colored Noise

extern crate colorgrad;
extern crate image;
extern crate noise;

use noise::NoiseFn;

fn main() {
    let w = 600;
    let h = 350;
    let scale = 0.015;

    let grad = colorgrad::spectral();
    let ns = noise::OpenSimplex::new();
    let mut imgbuf = image::ImageBuffer::new(w, h);

    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, _) = grad.at(remap(t, -0.5, 0.5, 0.0, 1.0)).rgba_u8();
        *pixel = image::Rgb([r, g, b]);
    }
    imgbuf.save("noise.png").unwrap();
}

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

img

Inspirations

Links

Dependencies

~0.8–1.3MB
~29K SLoC