Skip to content

Commit d67fbd5

Browse files
Add helper function to determine if color is transparent (#10310)
# 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 <[email protected]>
1 parent dc1f76d commit d67fbd5

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

crates/bevy_render/src/color/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,25 @@ impl Color {
549549
self
550550
}
551551

552+
/// Determine if the color is fully transparent, i.e. if the alpha is 0.
553+
///
554+
/// # Examples
555+
///
556+
/// ```
557+
/// # use bevy_render::color::Color;
558+
/// // Fully transparent colors
559+
/// assert!(Color::NONE.is_fully_transparent());
560+
/// assert!(Color::rgba(1.0, 0.5, 0.5, 0.0).is_fully_transparent());
561+
///
562+
/// // (Partially) opaque colors
563+
/// assert!(!Color::BLACK.is_fully_transparent());
564+
/// assert!(!Color::rgba(1.0, 0.5, 0.5, 0.2).is_fully_transparent());
565+
/// ```
566+
#[inline(always)]
567+
pub fn is_fully_transparent(&self) -> bool {
568+
self.a() == 0.0
569+
}
570+
552571
/// Converts a `Color` to variant `Color::Rgba`
553572
pub fn as_rgba(self: &Color) -> Color {
554573
match self {

crates/bevy_ui/src/render/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub fn extract_atlas_uinodes(
202202
)) = uinode_query.get(*entity)
203203
{
204204
// Skip invisible and completely transparent nodes
205-
if !view_visibility.get() || color.0.a() == 0.0 {
205+
if !view_visibility.get() || color.0.is_fully_transparent() {
206206
continue;
207207
}
208208

@@ -306,7 +306,7 @@ pub fn extract_uinode_borders(
306306
{
307307
// Skip invisible borders
308308
if !view_visibility.get()
309-
|| border_color.0.a() == 0.0
309+
|| border_color.0.is_fully_transparent()
310310
|| node.size().x <= 0.
311311
|| node.size().y <= 0.
312312
{
@@ -413,7 +413,10 @@ pub fn extract_uinode_outlines(
413413
uinode_query.get(*entity)
414414
{
415415
// Skip invisible outlines
416-
if !view_visibility.get() || outline.color.a() == 0. || node.outline_width == 0. {
416+
if !view_visibility.get()
417+
|| outline.color.is_fully_transparent()
418+
|| node.outline_width == 0.
419+
{
417420
continue;
418421
}
419422

@@ -508,7 +511,7 @@ pub fn extract_uinodes(
508511
uinode_query.get(*entity)
509512
{
510513
// Skip invisible and completely transparent nodes
511-
if !view_visibility.get() || color.0.a() == 0.0 {
514+
if !view_visibility.get() || color.0.is_fully_transparent() {
512515
continue;
513516
}
514517

0 commit comments

Comments
 (0)