Skip to content

Commit

Permalink
Change Frame::multiply_with_opacity to multiply in gamma space (#4283)
Browse files Browse the repository at this point in the history
This produces a more perceptually even change when used in animations,
like when fading out a closed window
  • Loading branch information
emilk authored Mar 30, 2024
1 parent e03ea2e commit ab720ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions crates/egui/src/containers/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit ab720ce

Please sign in to comment.