Rust color scales library for charts, maps, data-visualization and creative coding.
Add colorgrad
to your Cargo.toml
toml
[dependencies]
colorgrad = "0.3.0"
rust
let g = colorgrad::CustomGradient::new().build()?;
```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., 1., 1.),
Color::from_hsv(348., 0.9, 0.8),
])
.build()?;
```
.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()?;
rust
let g = colorgrad::CustomGradient::new()
.html_colors(&["gold", "hotpink", "darkturquoise"])
.build()?;
rust
let g = colorgrad::CustomGradient::new()
.html_colors(&["rgb(125,110,221)", "rgb(90%,45%,97%)", "hsl(229,79%,85%)"])
.build()?;
Default domain is [0..1].
```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .build()?;
assert_eq!(g.domain(), (0., 1.));
```
Set the domain to [0..100].
```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[0., 100.]) .build()?;
assert_eq!(g.domain(), (0., 100.));
```
Set the domain to [-1..1].
```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[-1., 1.]) .build()?;
assert_eq!(g.domain(), (-1., 1.));
```
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.7, 1.]) .build()?;
assert_eq!(g.domain(), (0., 1.));
```
Set exact position for each color. The domain is [15..80].
```rust let g = colorgrad::CustomGradient::new() .html_colors(&["deeppink", "gold", "seagreen"]) .domain(&[15., 30., 80.]) .build()?;
assert_eq!(g.domain(), (15., 80.));
```
rust
let g = colorgrad::CustomGradient::new()
.html_colors(&["#FFF", "#00F"])
.mode(colorgrad::BlendMode::Rgb)
.build()?;
All preset gradients are in the domain [0..1]. Uniform B-splines is used to interpolate the colors.
colorgrad::br_bg()
colorgrad::pr_gn()
colorgrad::pi_yg()
colorgrad::pu_or()
colorgrad::rd_bu()
colorgrad::rd_gy()
colorgrad::rd_yl_bu()
colorgrad::rd_yl_gn()
colorgrad::spectral()
colorgrad::blues()
colorgrad::greens()
colorgrad::greys()
colorgrad::oranges()
colorgrad::purples()
colorgrad::reds()
colorgrad::turbo()
colorgrad::viridis()
colorgrad::inferno()
colorgrad::magma()
colorgrad::plasma()
colorgrad::cividis()
colorgrad::warm()
colorgrad::cool()
colorgrad::cubehelix_default()
colorgrad::bu_gn()
colorgrad::bu_pu()
colorgrad::gn_bu()
colorgrad::or_rd()
colorgrad::pu_bu_gn()
colorgrad::pu_bu()
colorgrad::pu_rd()
colorgrad::rd_pu()
colorgrad::yl_gn_bu()
colorgrad::yl_gn()
colorgrad::yl_or_br()
colorgrad::yl_or_rd()
colorgrad::rainbow()
colorgrad::sinebow()
```rust let grad = colorgrad::rainbow();
assert_eq!(grad.domain(), (0., 1.)); ```
```rust let grad = colorgrad::rainbow();
asserteq!(grad.at(0.0).rgbau8(), (110, 64, 170, 255)); asserteq!(grad.at(0.5).rgbau8(), (175, 240, 91, 255)); asserteq!(grad.at(1.0).rgbau8(), (110, 64, 170, 255)); ```
```rust let grad = colorgrad::rainbow();
for c in grad.colors(10) { println!("{}", c.tohexstring()); } ```
Output:
```console
```
Convert gradient to hard-edged gradient with 11 segments and 0 smoothness.
rust
let g = colorgrad::rainbow().sharp(11, 0.);
This is the effect of different smoothness.
```rust //extern crate colorgrad; //extern crate image;
use std::error::Error;
fn main() -> Result<(), Box
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, _a) = grad.at(x as f64 / fw).rgba_u8();
*pixel = image::Rgb([r, g, b]);
}
imgbuf.save("gradient.png")?;
Ok(())
} ```
Example output:
```rust //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::rainbow().sharp(5, 0.15);
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, _a) = 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 } ```
Example output: