Skip to content

Commit

Permalink
Add helper function to determine if color is transparent (bevyengine#…
Browse files Browse the repository at this point in the history
…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 <mockersf@gmail.com>
  • Loading branch information
2 people authored and ameknite committed Nov 6, 2023
1 parent e58c161 commit 798e981
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
19 changes: 19 additions & 0 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 798e981

Please sign in to comment.