gradients: add presets

This commit is contained in:
Wez Furlong committed 2021-08-13 22:41:31 -07:00
1 parent 39a5a94915
commit e234ea7933
2 files changed
+161 -15

No files matched your search

+111 -15
View File
@@ -44,13 +44,104 @@ impl Default for GradientOrientation {
}
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
pub enum GradientPreset {
Blues,
BrBg,
BuGn,
BuPu,
Cividis,
Cool,
CubeHelixDefault,
GnBu,
Greens,
Greys,
Inferno,
Magma,
OrRd,
Oranges,
PiYg,
Plasma,
PrGn,
PuBu,
PuBuGn,
PuOr,
PuRd,
Purples,
Rainbow,
RdBu,
RdGy,
RdPu,
RdYlBu,
RdYlGn,
Reds,
Sinebow,
Spectral,
Turbo,
Viridis,
Warm,
YlGn,
YlGnBu,
YlOrBr,
YlOrRd,
}
impl GradientPreset {
fn build(self) -> colorgrad::Gradient {
match self {
Self::Blues => colorgrad::blues(),
Self::BrBg => colorgrad::br_bg(),
Self::BuGn => colorgrad::bu_gn(),
Self::BuPu => colorgrad::bu_pu(),
Self::Cividis => colorgrad::cividis(),
Self::Cool => colorgrad::cool(),
Self::CubeHelixDefault => colorgrad::cubehelix_default(),
Self::GnBu => colorgrad::gn_bu(),
Self::Greens => colorgrad::greens(),
Self::Greys => colorgrad::greys(),
Self::Inferno => colorgrad::inferno(),
Self::Magma => colorgrad::magma(),
Self::OrRd => colorgrad::or_rd(),
Self::Oranges => colorgrad::oranges(),
Self::PiYg => colorgrad::pi_yg(),
Self::Plasma => colorgrad::plasma(),
Self::PrGn => colorgrad::pr_gn(),
Self::PuBu => colorgrad::pu_bu(),
Self::PuBuGn => colorgrad::pu_bu_gn(),
Self::PuOr => colorgrad::pu_or(),
Self::PuRd => colorgrad::pu_rd(),
Self::Purples => colorgrad::purples(),
Self::Rainbow => colorgrad::rainbow(),
Self::RdBu => colorgrad::rd_bu(),
Self::RdGy => colorgrad::rd_gy(),
Self::RdPu => colorgrad::rd_pu(),
Self::RdYlBu => colorgrad::rd_yl_bu(),
Self::RdYlGn => colorgrad::rd_yl_gn(),
Self::Reds => colorgrad::reds(),
Self::Sinebow => colorgrad::sinebow(),
Self::Spectral => colorgrad::spectral(),
Self::Turbo => colorgrad::turbo(),
Self::Viridis => colorgrad::viridis(),
Self::Warm => colorgrad::warm(),
Self::YlGn => colorgrad::yl_gn(),
Self::YlGnBu => colorgrad::yl_gn_bu(),
Self::YlOrBr => colorgrad::yl_or_br(),
Self::YlOrRd => colorgrad::yl_or_rd(),
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Gradient {
#[serde(default)]
pub orientation: GradientOrientation,
#[serde(default)]
pub colors: Vec<String>,
#[serde(default)]
pub preset: Option<GradientPreset>,
#[serde(default)]
pub interpolation: Interpolation,
@@ -70,21 +161,26 @@ pub struct Gradient {
impl Gradient {
pub fn build(&self) -> anyhow::Result<colorgrad::Gradient> {
use colorgrad::{BlendMode as CGMode, Interpolation as CGInterp};
let colors: Vec<&str> = self.colors.iter().map(|s| s.as_str()).collect();
let mut g = colorgrad::CustomGradient::new();
g.html_colors(&colors);
g.mode(match self.blend {
BlendMode::Rgb => CGMode::Rgb,
BlendMode::LinearRgb => CGMode::LinearRgb,
BlendMode::Hsv => CGMode::Hsv,
BlendMode::Oklab => CGMode::Oklab,
});
g.interpolation(match self.interpolation {
Interpolation::Linear => CGInterp::Linear,
Interpolation::Basis => CGInterp::Basis,
Interpolation::CatmullRom => CGInterp::CatmullRom,
});
let g = g.build()?;
let g = match &self.preset {
Some(p) => p.build(),
None => {
let colors: Vec<&str> = self.colors.iter().map(|s| s.as_str()).collect();
let mut g = colorgrad::CustomGradient::new();
g.html_colors(&colors);
g.mode(match self.blend {
BlendMode::Rgb => CGMode::Rgb,
BlendMode::LinearRgb => CGMode::LinearRgb,
BlendMode::Hsv => CGMode::Hsv,
BlendMode::Oklab => CGMode::Oklab,
});
g.interpolation(match self.interpolation {
Interpolation::Linear => CGInterp::Linear,
Interpolation::Basis => CGInterp::Basis,
Interpolation::CatmullRom => CGInterp::CatmullRom,
});
g.build()?
}
};
match (self.segment_size, self.segment_smoothness) {
(Some(size), Some(smoothness)) => Ok(g.sharp(size, smoothness)),
(None, None) => Ok(g),
@@ -26,6 +26,11 @@ return {
"#24243e"
},
-- Instead of specifying `colors`, you can use one of a number of
-- predefined, preset gradients.
-- A list of presets is shown in a section below.
-- preset = "Warm",
-- Specifies the interpolation style to be used.
-- "Linear", "Basis" and "CatmullRom" as supported.
-- The default is "Linear".
@@ -98,3 +103,48 @@ return {
```
<img src="../../../screenshots/radial-gradient.png">
## Presets
The following presets are available:
|Preset|Gradient|
|------+--------|
|Blues|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/blues.png">|
|BrBg|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/br_bg.png">|
|BuGn|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/bu_gn.png">|
|BuPu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/bu_pu.png">|
|Cividis|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/cividis.png">|
|Cool|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/cool.png">|
|CubeHelixDefault|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/cubehelix_default.png">|
|GnBu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/gn_bu.png">|
|Greens|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/greens.png">|
|Greys|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/greys.png">|
|Inferno|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/inferno.png">|
|Magma|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/magma.png">|
|OrRd|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/or_rd.png">|
|Oranges|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/oranges.png">|
|PiYg|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/pi_yg.png">|
|Plasma|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/plasma.png">|
|PrGn|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/pr_gn.png">|
|PuBu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/pu_bu.png">|
|PuBuGn|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/pu_bu_gn.png">|
|PuOr|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/pu_or.png">|
|PuRd|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/pu_rd.png">|
|Purples|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/purples.png">|
|Rainbow|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/rainbow.png">|
|RdBu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/rd_bu.png">|
|RdGy|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/rd_gy.png">|
|RdPu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/rd_pu.png">|
|RdYlBu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/rd_yl_bu.png">|
|RdYlGn|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/rd_yl_gn.png">|
|Reds|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/reds.png">|
|Sinebow|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/sinebow.png">|
|Spectral|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/spectral.png">|
|Turbo|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/turbo.png">|
|Viridis|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/viridis.png">|
|Warm|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/warm.png">|
|YlGn|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/yl_gn.png">|
|YlGnBu|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/yl_gn_by.png">|
|YlOrBr|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/yl_or_br.png">|
|YlOrRd|<img src="https://github.com/mazznoer/colorgrad-rs/raw/master/docs/images/preset/yl_or_rd.png">|