Implement maths and Animatable for Srgba (#12649)

# Objective

- Implements maths and `Animatable` for `Srgba` as suggested
[here](https://github.com/bevyengine/bevy/issues/12617#issuecomment-2013494774).

## Solution

- Implements `Animatable` and maths for `Srgba` just like their
implemented for other colors.

---

## Changelog

- Updated the example to mention `Srgba`.

## Migration Guide

- The previously existing implementation of mul/div for `Srgba` did not
modify `alpha` but these operations do modify `alpha` now. Users need to
be aware of this change.
This commit is contained in:
Lynn 2024-03-22 18:31:48 +01:00 committed by GitHub
parent 78335a5ddc
commit 6910ca3e8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 47 deletions

View file

@ -1,5 +1,5 @@
use crate::util;
use bevy_color::{ClampColor, Laba, LinearRgba, Oklaba, Xyza};
use bevy_color::{ClampColor, Laba, LinearRgba, Oklaba, Srgba, Xyza};
use bevy_ecs::world::World;
use bevy_math::*;
use bevy_reflect::Reflect;
@ -96,6 +96,7 @@ impl_float_animatable!(DVec4, f64);
impl_color_animatable!(LinearRgba);
impl_color_animatable!(Laba);
impl_color_animatable!(Oklaba);
impl_color_animatable!(Srgba);
impl_color_animatable!(Xyza);
// Vec3 is special cased to use Vec3A internally for blending

View file

@ -1,7 +1,7 @@
use std::ops::{Div, Mul};
use crate::color_difference::EuclideanDistance;
use crate::{Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor, Xyza};
use crate::{
impl_componentwise_point, Alpha, ClampColor, LinearRgba, Luminance, Mix, StandardColor, Xyza,
};
use bevy_math::Vec4;
use bevy_reflect::prelude::*;
use serde::{Deserialize, Serialize};
@ -27,6 +27,8 @@ pub struct Srgba {
impl StandardColor for Srgba {}
impl_componentwise_point!(Srgba, [red, green, blue, alpha]);
impl Srgba {
// The standard VGA colors, with alpha set to 1.0.
// https://en.wikipedia.org/wiki/Web_colors#Basic_colors
@ -389,48 +391,6 @@ pub enum HexColorError {
Char(char),
}
/// All color channels are scaled directly,
/// but alpha is unchanged.
///
/// Values are not clamped.
impl Mul<f32> for Srgba {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
Self {
red: self.red * rhs,
green: self.green * rhs,
blue: self.blue * rhs,
alpha: self.alpha,
}
}
}
impl Mul<Srgba> for f32 {
type Output = Srgba;
fn mul(self, rhs: Srgba) -> Srgba {
rhs * self
}
}
/// All color channels are scaled directly,
/// but alpha is unchanged.
///
/// Values are not clamped.
impl Div<f32> for Srgba {
type Output = Self;
fn div(self, rhs: f32) -> Self {
Self {
red: self.red / rhs,
green: self.green / rhs,
blue: self.blue / rhs,
alpha: self.alpha,
}
}
}
#[cfg(test)]
mod tests {
use crate::testing::assert_approx_eq;

View file

@ -37,7 +37,7 @@ fn main() {
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
// The color spaces `Oklaba`, `Laba`, `LinearRgba` and `Xyza` all are either perceptually or physically linear.
// The color spaces `Oklaba`, `Laba`, `LinearRgba`, `Srgba` and `Xyza` all are either perceptually or physically linear.
// This property allows us to define curves, e.g. bezier curves through these spaces.
// Define the control points for the curve.