From 24d06594475c1a4990f45a1b29bba6c3f5872130 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 30 Mar 2024 18:12:39 +0100 Subject: [PATCH] Change `Frame::multiply_with_opacity` to multiply in gamma space This produces a more perceptually even change when used in animations, like when fading out a closed window --- crates/ecolor/src/color32.rs | 2 +- crates/egui/src/containers/frame.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index 0b60176f5cd..a2f06294819 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -212,7 +212,7 @@ impl Color32 { /// Multiply with 0.5 to make color half as opaque in linear space. /// /// This is using linear space, which is not perceptually even. - /// You may want to use [`Self::gamma_multiply`] instead. + /// You likely want to use [`Self::gamma_multiply`] instead. #[inline] pub fn linear_multiply(self, factor: f32) -> Self { crate::ecolor_assert!(0.0 <= factor && factor <= 1.0); diff --git a/crates/egui/src/containers/frame.rs b/crates/egui/src/containers/frame.rs index b982cfd0b03..82ef47770a7 100644 --- a/crates/egui/src/containers/frame.rs +++ b/crates/egui/src/containers/frame.rs @@ -196,11 +196,15 @@ impl Frame { self } + /// Opacity multiplier in gamma space. + /// + /// For instance, multiplying with `0.5` + /// will make the frame half transparent. #[inline] pub fn multiply_with_opacity(mut self, opacity: f32) -> Self { - self.fill = self.fill.linear_multiply(opacity); - self.stroke.color = self.stroke.color.linear_multiply(opacity); - self.shadow.color = self.shadow.color.linear_multiply(opacity); + self.fill = self.fill.gamma_multiply(opacity); + self.stroke.color = self.stroke.color.gamma_multiply(opacity); + self.shadow.color = self.shadow.color.gamma_multiply(opacity); self } }