diff --git a/color-types/src/lib.rs b/color-types/src/lib.rs index 42f62f062..1168eb676 100644 --- a/color-types/src/lib.rs +++ b/color-types/src/lib.rs @@ -372,7 +372,7 @@ impl SrgbaTuple { Self::from_hsla(h, s, l, a) } - /// Find the complementary color: the one opposite it on the color wheel + /// Rotate the hue angle by the specified number of degrees pub fn adjust_hue_fixed(&self, amount: f64) -> Self { let (h, s, l, a) = self.to_hsla(); let h = normalize_angle(h + amount); @@ -383,6 +383,10 @@ impl SrgbaTuple { self.adjust_hue_fixed(180.) } + pub fn complement_ryb(&self) -> Self { + self.adjust_hue_fixed_ryb(180.) + } + pub fn triad(&self) -> (Self, Self) { (self.adjust_hue_fixed(120.), self.adjust_hue_fixed(-120.)) } @@ -394,50 +398,61 @@ impl SrgbaTuple { self.adjust_hue_fixed(180.), ) } + + /// Rotate the hue angle by the specified number of degrees, using + /// the RYB color wheel + pub fn adjust_hue_fixed_ryb(&self, amount: f64) -> Self { + let (h, s, l, a) = self.to_hsla(); + let h = rgb_hue_to_ryb_hue(h); + let h = normalize_angle(h + amount); + let h = ryb_huge_to_rgb_hue(h); + Self::from_hsla(h, s, l, a) + } } -/// From "Paint Inspired Color Compositing" by Gosset and Chen -/// has a python -/// implementation -/// has a copy of the paper -/// itself at -pub fn ryb_to_rgb(r: f32, y: f32, b: f32) -> (f32, f32, f32) { - fn cubic(t: f32, a: f32, b: f32) -> f32 { - let weight = t * t * (3. - 2. * t); - a + weight * (b - a) +/// Convert an RGB color space hue angle to an RYB colorspace hue angle +/// +fn rgb_hue_to_ryb_hue(hue: f64) -> f64 { + if hue < 35. { + map_range(hue, 0., 35., 0., 60.) + } else if hue < 60. { + map_range(hue, 35., 60., 60., 122.) + } else if hue < 120. { + map_range(hue, 60., 120., 122., 165.) + } else if hue < 180. { + map_range(hue, 120., 180., 165., 218.) + } else if hue < 240. { + map_range(hue, 180., 240., 218., 275.) + } else if hue < 300. { + map_range(hue, 240., 300., 275., 330.) + } else { + map_range(hue, 300., 360., 330., 360.) } +} - let red = { - let x0 = cubic(b, 1.0, 0.163); - let x1 = cubic(b, 1.0, 0.0); - let x2 = cubic(b, 1.0, 0.5); - let x3 = cubic(b, 1.0, 0.2); - let y0 = cubic(y, x0, x1); - let y1 = cubic(y, x2, x3); - cubic(r, y0, y1) - }; +/// Convert an RYB color space hue angle to an RGB colorspace hue angle +fn ryb_huge_to_rgb_hue(hue: f64) -> f64 { + if hue < 60. { + map_range(hue, 0., 60., 0., 35.) + } else if hue < 122. { + map_range(hue, 60., 122., 35., 60.) + } else if hue < 165. { + map_range(hue, 122., 165., 60., 120.) + } else if hue < 218. { + map_range(hue, 165., 218., 120., 180.) + } else if hue < 275. { + map_range(hue, 218., 275., 180., 240.) + } else if hue < 330. { + map_range(hue, 275., 330., 240., 300.) + } else { + map_range(hue, 330., 360., 300., 360.) + } +} - let green = { - let x0 = cubic(b, 1.0, 0.373); - let x1 = cubic(b, 1.0, 0.66); - let x2 = cubic(b, 0.0, 0.0); - let x3 = cubic(b, 0.5, 0.094); - let y0 = cubic(y, x0, x1); - let y1 = cubic(y, x2, x3); - cubic(r, y0, y1) - }; - - let blue = { - let x0 = cubic(b, 1.0, 0.6); - let x1 = cubic(b, 0.0, 0.2); - let x2 = cubic(b, 0.0, 0.5); - let x3 = cubic(b, 0.0, 0.0); - let y0 = cubic(y, x0, x1); - let y1 = cubic(y, x2, x3); - cubic(r, y0, y1) - }; - - (red, green, blue) +fn map_range(x: f64, x1: f64, x2: f64, y1: f64, y2: f64) -> f64 { + let a_slope = (y2 - y1) / (x2 - x1); + let a_slope_intercept = y1 - (a_slope * x1); + x * a_slope + a_slope_intercept } fn normalize_angle(t: f64) -> f64 { diff --git a/docs/config/lua/wezterm.color/parse.md b/docs/config/lua/wezterm.color/parse.md index 45d4b424d..f47f639db 100644 --- a/docs/config/lua/wezterm.color/parse.md +++ b/docs/config/lua/wezterm.color/parse.md @@ -36,6 +36,18 @@ Returns the complement of the color. The complement is computed by converting to HSL, rotating by 180 degrees and converting back to RGBA. +## `color:complement_ryb()` + +*Since: nightly builds only* + +Returns the complement of the color using the [RYB color +model](https://en.wikipedia.org/wiki/RYB_color_model), which more closely +matches how artists think of mixing colors. + +The complement is computed by converting to HSL, converting the +hue angle to the equivalent RYB angle, rotating by 180 degrees and +and then converting back to RGBA. + ## `color:triad()` *Since: nightly builds only* @@ -55,7 +67,7 @@ Returns the other three colors that form a square. The other colors are 90 degrees apart on the HSL color wheel. ```lua -local a, b, = wezterm:color.parse("yellow"):square() +local a, b, c = wezterm:color.parse("yellow"):square() ``` ## `color:saturate(factor)` @@ -115,3 +127,18 @@ Decrease the lightness by amount, a value ranging from `0.0` to `1.0`. *Since: nightly builds only* Adjust the hue angle by the specified number of degrees. + +180 degrees gives the complementary color. +Three colors separated by 120 degrees form the triad. +Four colors separated by 90 degrees form the square. + +## `color:adjust_hue_fixed_ryb(degrees)` + +*Since: nightly builds only* + +Adjust the hue angle using the [RYB color model](https://en.wikipedia.org/wiki/RYB_color_model), which more closely +matches how artists think of mixing colors, by the specified number of degrees. + +180 degrees gives the complementary color. +Three colors separated by 120 degrees form the triad. +Four colors separated by 90 degrees form the square. diff --git a/lua-api-crates/color-funcs/src/lib.rs b/lua-api-crates/color-funcs/src/lib.rs index 8bf2a1b97..cd074cdd5 100644 --- a/lua-api-crates/color-funcs/src/lib.rs +++ b/lua-api-crates/color-funcs/src/lib.rs @@ -9,6 +9,9 @@ impl ColorWrap { pub fn complement(&self) -> Self { Self(self.0.complement().into()) } + pub fn complement_ryb(&self) -> Self { + Self(self.0.complement_ryb().into()) + } pub fn triad(&self) -> (Self, Self) { let (a, b) = self.0.triad(); (Self(a.into()), Self(b.into())) @@ -32,6 +35,9 @@ impl ColorWrap { pub fn adjust_hue_fixed(&self, amount: f64) -> Self { Self(self.0.adjust_hue_fixed(amount).into()) } + pub fn adjust_hue_fixed_ryb(&self, amount: f64) -> Self { + Self(self.0.adjust_hue_fixed_ryb(amount).into()) + } } impl UserData for ColorWrap { @@ -44,6 +50,7 @@ impl UserData for ColorWrap { Ok(this.0 == other.0) }); methods.add_method("complement", |_, this, _: ()| Ok(this.complement())); + methods.add_method("complement_ryb", |_, this, _: ()| Ok(this.complement_ryb())); methods.add_method("triad", |_, this, _: ()| Ok(this.triad())); methods.add_method("square", |_, this, _: ()| Ok(this.square())); methods.add_method("saturate", |_, this, factor: f64| Ok(this.saturate(factor))); @@ -73,6 +80,9 @@ impl UserData for ColorWrap { methods.add_method("adjust_hue_fixed", |_, this, amount: f64| { Ok(this.adjust_hue_fixed(amount)) }); + methods.add_method("adjust_hue_fixed_ryb", |_, this, amount: f64| { + Ok(this.adjust_hue_fixed_ryb(amount)) + }); } }