From e8f04292a9bfed66a8deaaff92efcdd60d2a4142 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 25 Mar 2026 12:41:35 +0100 Subject: [PATCH] Add `UiStack::bg_color` (#8020) This lets you ask for the background color of a ui with `ui.stack().bg_color()` --- crates/egui/src/ui_stack.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/egui/src/ui_stack.rs b/crates/egui/src/ui_stack.rs index 07026c45b..0686dc086 100644 --- a/crates/egui/src/ui_stack.rs +++ b/crates/egui/src/ui_stack.rs @@ -1,6 +1,8 @@ use std::sync::Arc; use std::{any::Any, iter::FusedIterator}; +use epaint::Color32; + use crate::{Direction, Frame, Id, Rect}; /// What kind is this [`crate::Ui`]? @@ -253,6 +255,24 @@ impl UiStack { pub fn has_visible_frame(&self) -> bool { !self.info.frame.stroke.is_empty() } + + /// The background color of this [`Ui`]. + /// + /// This blend together all [`Frame::fill`] colors + /// up to the root. + #[inline] + pub fn bg_color(&self) -> Color32 { + let mut total = Color32::TRANSPARENT; + for node in self.iter() { + let fill = node.frame().fill; + if fill.is_opaque() { + return fill; + } else if fill != Color32::TRANSPARENT { + total = fill.blend(total); + } + } + total + } } // these methods act on the entire stack