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 <mockersf@gmail.com>
This commit is contained in:
TimJentzsch 2023-10-31 16:00:49 +01:00 committed by GitHub
parent dc1f76d9a2
commit d67fbd5e90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View file

@ -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 {

View file

@ -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;
}