Skip to content

Commit

Permalink
Add with_a and friends to Color (bevyengine#6899)
Browse files Browse the repository at this point in the history
# Objective
```rust
// makes clippy complain about 'taking a mutable reference to a `const` item'
let color = *Color::RED.set_a(0.5);

// Now you can do
let color = Color::RED.with_a(0.5);
```

## Changelog
Added `with_r`, `with_g`, `with_b`, and `with_a` to `Color`.

Co-authored-by: devil-ira <[email protected]>
  • Loading branch information
2 people authored and alradish committed Jan 22, 2023
1 parent 3927959 commit 995f0c0
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ impl Color {
self
}

/// Returns this color with red set to a new value in sRGB colorspace.
#[must_use]
pub fn with_r(mut self, r: f32) -> Self {
self.set_r(r);
self
}

/// Set green in sRGB colorspace.
pub fn set_g(&mut self, g: f32) -> &mut Self {
*self = self.as_rgba();
Expand All @@ -368,6 +375,13 @@ impl Color {
self
}

/// Returns this color with green set to a new value in sRGB colorspace.
#[must_use]
pub fn with_g(mut self, g: f32) -> Self {
self.set_g(g);
self
}

/// Set blue in sRGB colorspace.
pub fn set_b(&mut self, b: f32) -> &mut Self {
*self = self.as_rgba();
Expand All @@ -378,6 +392,13 @@ impl Color {
self
}

/// Returns this color with blue set to a new value in sRGB colorspace.
#[must_use]
pub fn with_b(mut self, b: f32) -> Self {
self.set_b(b);
self
}

/// Get alpha.
#[inline(always)]
pub fn a(&self) -> f32 {
Expand All @@ -400,6 +421,13 @@ impl Color {
self
}

/// Returns this color with a new alpha value.
#[must_use]
pub fn with_a(mut self, a: f32) -> Self {
self.set_a(a);
self
}

/// Converts a `Color` to variant `Color::Rgba`
pub fn as_rgba(self: &Color) -> Color {
match self {
Expand Down

0 comments on commit 995f0c0

Please sign in to comment.