From 798e9811b796af238dad249f04372aca32095b28 Mon Sep 17 00:00:00 2001 From: TimJentzsch Date: Tue, 31 Oct 2023 16:00:49 +0100 Subject: [PATCH] Add helper function to determine if color is transparent (#10310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - We need to check multiple times if a color is fully transparent, e.g. for performance optimizations. - Make code more readable. - Reduce code duplication, to simplify making changes if needed (e.g. if we need to take floating point weirdness into account later on). ## Solution - Introduce a new `Color::is_fully_transparent` helper function to determine if the alpha of a color is 0. - Use the helper function in our UI rendering code. --- ## Changelog - Added `Color::is_fully_transparent` helper function. --------- Co-authored-by: François --- crates/bevy_render/src/color/mod.rs | 19 +++++++++++++++++++ crates/bevy_ui/src/render/mod.rs | 11 +++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 3643439383bda7..b4139b8787482d 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -549,6 +549,25 @@ impl Color { self } + /// Determine if the color is fully transparent, i.e. if the alpha is 0. + /// + /// # Examples + /// + /// ``` + /// # use bevy_render::color::Color; + /// // Fully transparent colors + /// assert!(Color::NONE.is_fully_transparent()); + /// assert!(Color::rgba(1.0, 0.5, 0.5, 0.0).is_fully_transparent()); + /// + /// // (Partially) opaque colors + /// assert!(!Color::BLACK.is_fully_transparent()); + /// assert!(!Color::rgba(1.0, 0.5, 0.5, 0.2).is_fully_transparent()); + /// ``` + #[inline(always)] + pub fn is_fully_transparent(&self) -> bool { + self.a() == 0.0 + } + /// Converts a `Color` to variant `Color::Rgba` pub fn as_rgba(self: &Color) -> Color { match self { diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index a7a52c628319ee..951f602e35c744 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -202,7 +202,7 @@ pub fn extract_atlas_uinodes( )) = uinode_query.get(*entity) { // Skip invisible and completely transparent nodes - if !view_visibility.get() || color.0.a() == 0.0 { + if !view_visibility.get() || color.0.is_fully_transparent() { continue; } @@ -306,7 +306,7 @@ pub fn extract_uinode_borders( { // Skip invisible borders if !view_visibility.get() - || border_color.0.a() == 0.0 + || border_color.0.is_fully_transparent() || node.size().x <= 0. || node.size().y <= 0. { @@ -413,7 +413,10 @@ pub fn extract_uinode_outlines( uinode_query.get(*entity) { // Skip invisible outlines - if !view_visibility.get() || outline.color.a() == 0. || node.outline_width == 0. { + if !view_visibility.get() + || outline.color.is_fully_transparent() + || node.outline_width == 0. + { continue; } @@ -508,7 +511,7 @@ pub fn extract_uinodes( uinode_query.get(*entity) { // Skip invisible and completely transparent nodes - if !view_visibility.get() || color.0.a() == 0.0 { + if !view_visibility.get() || color.0.is_fully_transparent() { continue; }