From 5a3399accd3302506f6de9829087a26bc59374d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:07:36 +0100 Subject: [PATCH 1/2] Implement `Mix` for `Hsva` and `Hwba` --- crates/bevy_color/src/hsva.rs | 23 ++++++++++++++++++++++- crates/bevy_color/src/hwba.rs | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/bevy_color/src/hsva.rs b/crates/bevy_color/src/hsva.rs index 8f45a476d6565..59fc44b6ad666 100644 --- a/crates/bevy_color/src/hsva.rs +++ b/crates/bevy_color/src/hsva.rs @@ -1,4 +1,4 @@ -use crate::{Alpha, ClampColor, Hwba, Lcha, LinearRgba, Srgba, StandardColor, Xyza}; +use crate::{Alpha, ClampColor, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza}; use bevy_reflect::prelude::*; use serde::{Deserialize, Serialize}; @@ -74,6 +74,27 @@ impl Default for Hsva { } } +impl Mix for Hsva { + #[inline] + fn mix(&self, other: &Self, factor: f32) -> Self { + let n_factor = 1.0 - factor; + // TODO: Refactor this into EuclideanModulo::lerp_modulo + let shortest_angle = ((((other.hue - self.hue) % 360.) + 540.) % 360.) - 180.; + let mut hue = self.hue + shortest_angle * factor; + if hue < 0. { + hue += 360.; + } else if hue >= 360. { + hue -= 360.; + } + Self { + hue, + saturation: self.saturation * n_factor + other.saturation * factor, + value: self.value * n_factor + other.value * factor, + alpha: self.alpha * n_factor + other.alpha * factor, + } + } +} + impl Alpha for Hsva { #[inline] fn with_alpha(&self, alpha: f32) -> Self { diff --git a/crates/bevy_color/src/hwba.rs b/crates/bevy_color/src/hwba.rs index bfde45d8dcd4c..25fc8ca263015 100644 --- a/crates/bevy_color/src/hwba.rs +++ b/crates/bevy_color/src/hwba.rs @@ -2,7 +2,7 @@ //! in [_HWB - A More Intuitive Hue-Based Color Model_] by _Smith et al_. //! //! [_HWB - A More Intuitive Hue-Based Color Model_]: https://web.archive.org/web/20240226005220/http://alvyray.com/Papers/CG/HWB_JGTv208.pdf -use crate::{Alpha, ClampColor, Lcha, LinearRgba, Srgba, StandardColor, Xyza}; +use crate::{Alpha, ClampColor, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza}; use bevy_reflect::prelude::*; use serde::{Deserialize, Serialize}; @@ -78,6 +78,27 @@ impl Default for Hwba { } } +impl Mix for Hwba { + #[inline] + fn mix(&self, other: &Self, factor: f32) -> Self { + let n_factor = 1.0 - factor; + // TODO: Refactor this into EuclideanModulo::lerp_modulo + let shortest_angle = ((((other.hue - self.hue) % 360.) + 540.) % 360.) - 180.; + let mut hue = self.hue + shortest_angle * factor; + if hue < 0. { + hue += 360.; + } else if hue >= 360. { + hue -= 360.; + } + Self { + hue, + whiteness: self.whiteness * n_factor + other.whiteness * factor, + blackness: self.blackness * n_factor + other.blackness * factor, + alpha: self.alpha * n_factor + other.alpha * factor, + } + } +} + impl Alpha for Hwba { #[inline] fn with_alpha(&self, alpha: f32) -> Self { From b7abdbbb588d97591e4a0f659e39a2abd241b388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lynn=20B=C3=BCttgenbach?= <62256001+solis-lumine-vorago@users.noreply.github.com> Date: Fri, 22 Mar 2024 10:15:54 +0100 Subject: [PATCH 2/2] Resolve artifacts --- crates/bevy_color/src/hsva.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_color/src/hsva.rs b/crates/bevy_color/src/hsva.rs index 8d088b0a52379..d536cc1294f25 100644 --- a/crates/bevy_color/src/hsva.rs +++ b/crates/bevy_color/src/hsva.rs @@ -1,4 +1,4 @@ -use crate::{Alpha, ClampColor, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza}; +use crate::{Alpha, ClampColor, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza}; use bevy_reflect::prelude::*; use serde::{Deserialize, Serialize};