From a092b6f2d95ea59d2ef9adf99f701e42c7b7929c Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 24 Jan 2025 18:58:36 -0500 Subject: [PATCH] Add Color::greyscale and builder methods for Image --- crates/yakui-core/src/geometry/color.rs | 10 ++++++++++ crates/yakui-widgets/src/widgets/image.rs | 8 ++++++++ crates/yakui/examples/images.rs | 14 ++++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/crates/yakui-core/src/geometry/color.rs b/crates/yakui-core/src/geometry/color.rs index 0b279012..0860b4e7 100644 --- a/crates/yakui-core/src/geometry/color.rs +++ b/crates/yakui-core/src/geometry/color.rs @@ -24,6 +24,16 @@ impl Color { Self { r, g, b, a: 255 } } + /// Create a new `Color` with all channels set to the given value. + pub const fn greyscale(v: u8) -> Self { + Self { + r: v, + g: v, + b: v, + a: 255, + } + } + /// Create a color from a number, intended to be written as a hex literal. /// /// ```rust diff --git a/crates/yakui-widgets/src/widgets/image.rs b/crates/yakui-widgets/src/widgets/image.rs index 9572c9e1..0c207fcc 100644 --- a/crates/yakui-widgets/src/widgets/image.rs +++ b/crates/yakui-widgets/src/widgets/image.rs @@ -41,6 +41,14 @@ impl Image { pub fn show(self) -> Response { widget::(self) } + + pub fn color(self, color: Color) -> Self { + Self { color, ..self } + } + + pub fn fit_mode(self, fit_mode: ImageFit) -> Self { + Self { fit_mode, ..self } + } } #[derive(Debug)] diff --git a/crates/yakui/examples/images.rs b/crates/yakui/examples/images.rs index 9cb91650..b7e9e4e9 100644 --- a/crates/yakui/examples/images.rs +++ b/crates/yakui/examples/images.rs @@ -1,13 +1,19 @@ -use yakui::center; -use yakui::{image, nineslice, pad, widgets::Pad, Vec2}; +use yakui::widgets::Image; +use yakui::{center, Color}; +use yakui::{nineslice, widgets::Pad, Vec2}; use bootstrap::ExampleState; pub fn run(state: &mut ExampleState) { - pad(Pad::all(20.0), || { + let shade = ((state.time.sin() * 0.5 + 0.5) * 255.0).round() as u8; + let tint = Color::greyscale(shade); + + Pad::all(20.0).show(|| { nineslice(state.brown_inlay, Pad::all(15.0), 9.0, || { center(|| { - image(state.monkey, Vec2::splat(800.0)); + Image::new(state.monkey, Vec2::splat(800.0)) + .color(tint) + .show(); }); }); });