From f617b214d81bf7006c888fec127b6c9145e53ac5 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 5 Dec 2024 13:48:03 +0100 Subject: [PATCH] Add Color32::mul --- crates/ecolor/src/color32.rs | 15 +++++++++++++++ crates/egui/src/widgets/button.rs | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index 80e9e0778d3..c38a8d6b9f8 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -269,3 +269,18 @@ impl Color32 { ) } } + +impl std::ops::Mul for Color32 { + type Output = Self; + + /// Fast gamma-space multiplication. + #[inline] + fn mul(self, other: Self) -> Self { + Self([ + fast_round(self[0] as f32 * other[0] as f32 / 255.0), + fast_round(self[1] as f32 * other[1] as f32 / 255.0), + fast_round(self[2] as f32 * other[2] as f32 / 255.0), + fast_round(self[3] as f32 * other[3] as f32 / 255.0), + ]) + } +} diff --git a/crates/egui/src/widgets/button.rs b/crates/egui/src/widgets/button.rs index bfcd656723c..e4355b49e8d 100644 --- a/crates/egui/src/widgets/button.rs +++ b/crates/egui/src/widgets/button.rs @@ -158,7 +158,7 @@ impl<'a> Button<'a> { self } - /// If true, the tint of the image is the same as the text color. + /// If true, the tint of the image is multiplied by the widget text color. /// /// This makes sense for images that are white, that should have the same color as the text color. /// This will also make the icon color depend on hover state. @@ -336,7 +336,7 @@ impl Widget for Button<'_> { let tlr = image.load_for_size(ui.ctx(), image_size); let mut image_options = image.image_options().clone(); if image_tint_follows_text_color { - image_options.tint = visuals.text_color(); + image_options.tint = image_options.tint * visuals.text_color(); } widgets::image::paint_texture_load_result( ui,