Skip to content
Closed
1 change: 1 addition & 0 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl Painter {
}

/// If set, colors will be modified to look like this
#[deprecated = "Use `multiply_opacity` instead"]
pub fn set_fade_to_color(&mut self, fade_to_color: Option<Color32>) {
self.fade_to_color = fade_to_color;
}
Expand Down
27 changes: 21 additions & 6 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,9 @@ pub struct Visuals {

/// How to display numeric color values.
pub numeric_color_space: NumericColorSpace,

/// How much to modify the alpha of a disabled widget.
pub disabled_alpha: f32,
}

impl Visuals {
Expand Down Expand Up @@ -1052,18 +1055,26 @@ impl Visuals {
self.window_stroke
}

/// When fading out things, we fade the colors towards this.
// TODO(emilk): replace with an alpha
/// Disabled widgets have their alpha modified by this.
#[inline(always)]
pub fn disabled_alpha(&self) -> f32 {
self.disabled_alpha
}

/// Returns a "disabled" version of the given color.
///
/// This function modifies the opcacity of the given color.
/// If this is undesirable use [`gray_out`](Self::gray_out).
#[inline(always)]
pub fn fade_out_to_color(&self) -> Color32 {
self.widgets.noninteractive.weak_bg_fill
pub fn disable(&self, color: Color32) -> Color32 {
color.gamma_multiply(self.disabled_alpha())
}

/// Returned a "grayed out" version of the given color.
/// Returns a "grayed out" version of the given color.
#[doc(alias = "grey_out")]
#[inline(always)]
pub fn gray_out(&self, color: Color32) -> Color32 {
crate::ecolor::tint_color_towards(color, self.fade_out_to_color())
crate::ecolor::tint_color_towards(color, self.widgets.noninteractive.weak_bg_fill)
}
}

Expand Down Expand Up @@ -1383,6 +1394,7 @@ impl Visuals {
image_loading_spinners: true,

numeric_color_space: NumericColorSpace::GammaByte,
disabled_alpha: 0.5,
}
}

Expand Down Expand Up @@ -2062,6 +2074,7 @@ impl Visuals {
image_loading_spinners,

numeric_color_space,
disabled_alpha,
} = self;

ui.collapsing("Background Colors", |ui| {
Expand Down Expand Up @@ -2190,6 +2203,8 @@ impl Visuals {
ui.label("Color picker type");
numeric_color_space.toggle_button_ui(ui);
});

ui.add(Slider::new(disabled_alpha, 0.0..=1.0).text("Disabled element alpha"));
});

ui.vertical_centered(|ui| reset_button(ui, self, "Reset visuals"));
Expand Down
6 changes: 3 additions & 3 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl Ui {
self.enabled = false;
if self.is_visible() {
self.painter
.set_fade_to_color(Some(self.visuals().fade_out_to_color()));
Comment thread
emilk marked this conversation as resolved.
.multiply_opacity(self.visuals().disabled_alpha());
}
}

Expand Down Expand Up @@ -2963,8 +2963,8 @@ impl Ui {

if is_anything_being_dragged && !can_accept_what_is_being_dragged {
// When dragging something else, show that it can't be dropped here:
fill = self.visuals().gray_out(fill);
stroke.color = self.visuals().gray_out(stroke.color);
fill = self.visuals().disable(fill);
stroke.color = self.visuals().disable(stroke.color);
}

frame.frame.fill = fill;
Expand Down
Loading