Add with_a and friends to Color (#6899)

# 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 <justthecooldude@gmail.com>
This commit is contained in:
ira 2022-12-11 18:34:16 +00:00
parent 75880a0b17
commit ea8f74692f

View file

@ -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();
@ -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();
@ -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 {
@ -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 {