From ab6c4777cb5996ab41738febe5ed72b8125eaaa0 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 13 Aug 2021 23:52:32 -0700 Subject: [PATCH] config: add wezterm.gradient_colors --- config/src/background.rs | 3 +++ config/src/lua.rs | 13 +++++++++++++ docs/changelog.md | 1 + docs/config/lua/wezterm/gradient_colors.md | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 docs/config/lua/wezterm/gradient_colors.md diff --git a/config/src/background.rs b/config/src/background.rs index 5be517e0c..430e17e99 100644 --- a/config/src/background.rs +++ b/config/src/background.rs @@ -1,3 +1,4 @@ +use luahelper::impl_lua_conversion; use serde::{Deserialize, Serialize}; #[derive(Debug, Copy, Clone, Deserialize, Serialize)] @@ -158,6 +159,8 @@ pub struct Gradient { pub noise: Option, } +impl_lua_conversion!(Gradient); + impl Gradient { pub fn build(&self) -> anyhow::Result { use colorgrad::{BlendMode as CGMode, Interpolation as CGInterp}; diff --git a/config/src/lua.rs b/config/src/lua.rs index c07582995..997af8130 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -1,3 +1,4 @@ +use crate::Gradient; use crate::{FontAttributes, FontStretch, FontWeight, TextStyle}; use anyhow::anyhow; use bstr::BString; @@ -255,6 +256,7 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result { wezterm_mod.set("format", lua.create_function(format)?)?; wezterm_mod.set("strftime", lua.create_function(strftime)?)?; wezterm_mod.set("battery_info", lua.create_function(battery_info)?)?; + wezterm_mod.set("gradient_colors", lua.create_function(gradient_colors)?)?; package.set("path", path_array.join(";"))?; @@ -834,6 +836,17 @@ fn permute_mods<'lua>( Ok(result) } +fn gradient_colors<'lua>( + _lua: &'lua Lua, + (gradient, num_colors): (Gradient, usize), +) -> mlua::Result> { + let g = gradient.build().map_err(|e| mlua::Error::external(e))?; + Ok(g.colors(num_colors) + .into_iter() + .map(|c| c.to_hex_string()) + .collect()) +} + #[cfg(test)] mod test { use super::*; diff --git a/docs/changelog.md b/docs/changelog.md index 93869dc8f..4e4c13fc4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -78,6 +78,7 @@ As features stabilize some brief notes about them will accumulate here. * New: [wezterm.add_to_config_reload_watch_list](config/lua/wezterm/add_to_config_reload_watch_list.md) function to aid with automatically reloading the config when you've split your config across multiple files. Thanks to [@AusCyberman](https://github.com/AusCyberman)! [#989](https://github.com/wez/wezterm/pull/989) * Improved: wezterm now respects default emoji presentation and explicit emoji variation selectors (VS15 and VS16) so that glyphs that have both textual (usually monochrome, single cell width) and emoji (color, double width) presentations can be more faithfully rendered. [#997](https://github.com/wez/wezterm/issues/997) * New: [window_background_gradient](config/lua/config/window_background_gradient.md) option to configure color gradients for your window background +* New: [wezterm.gradient_colors](config/lua/wezterm/gradient_colors.md) function to compute RGB values for gradients for use in your config. ### 20210502-154244-3f7122cb diff --git a/docs/config/lua/wezterm/gradient_colors.md b/docs/config/lua/wezterm/gradient_colors.md new file mode 100644 index 000000000..5a92c56b2 --- /dev/null +++ b/docs/config/lua/wezterm/gradient_colors.md @@ -0,0 +1,21 @@ +# wezterm.gradient_colors(gradient, num_colors) + +*Since: nightly builds only* + +Given a gradient spec and a number of colors, returns a table +holding that many colors spaced evenly across the range of +the gradient. + +This is useful for example to generate colors for tabs, or +to do something fancy like interpolate colors across a gradient +based on the time of the day. + +`gradient` is any gradient allowed by the +[window_background_gradient](../config/window_background_gradient.md) option. + +This example is what you'd see if you opened up the [debug overlay](../keyassignment/ShowDebugOverlay.md) to try this out in the repl: + +```lua +> wezterm.gradient_colors({preset="Rainbow"}, 4) +["#6e40aa", "#ff8c38", "#5dea8d", "#6e40aa"] +```