Skip to content

Commit

Permalink
Add Color::greyscale and builder methods for Image
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Jan 24, 2025
1 parent 1057cbb commit a092b6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions crates/yakui-core/src/geometry/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions crates/yakui-widgets/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ impl Image {
pub fn show(self) -> Response<ImageResponse> {
widget::<ImageWidget>(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)]
Expand Down
14 changes: 10 additions & 4 deletions crates/yakui/examples/images.rs
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
Expand Down

0 comments on commit a092b6f

Please sign in to comment.