//! Demonstrates how to animate colors in different color spaces using mixing and splines. use bevy::{math::VectorSpace, prelude::*}; // We define this trait so we can reuse the same code for multiple color types that may be implemented using curves. trait CurveColor: VectorSpace + Into + Send + Sync + 'static {} impl + Send + Sync + 'static> CurveColor for T {} // We define this trait so we can reuse the same code for multiple color types that may be implemented using mixing. trait MixedColor: Mix + Into + Send + Sync + 'static {} impl + Send + Sync + 'static> MixedColor for T {} #[derive(Debug, Component)] struct Curve(CubicCurve); #[derive(Debug, Component)] struct Mixed([T; 4]); fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems( Update, ( animate_curve::, animate_curve::, animate_curve::, animate_mixed::, animate_mixed::, animate_mixed::, ), ) .run(); } fn setup(mut commands: Commands) { commands.spawn(Camera2d); // 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. // For more information, please see the cubic curve example. let colors = [ LinearRgba::WHITE, LinearRgba::rgb(1., 1., 0.), // Yellow LinearRgba::RED, LinearRgba::BLACK, ]; // Spawn a sprite using the provided colors as control points. spawn_curve_sprite(&mut commands, 275., colors); // Spawn another sprite using the provided colors as control points after converting them to the `Xyza` color space. spawn_curve_sprite(&mut commands, 175., colors.map(Xyza::from)); spawn_curve_sprite(&mut commands, 75., colors.map(Oklaba::from)); // Other color spaces like `Srgba` or `Hsva` are neither perceptually nor physically linear. // As such, we cannot use curves in these spaces. // However, we can still mix these colors and animate that way. In fact, mixing colors works in any color space. // Spawn a spritre using the provided colors for mixing. spawn_mixed_sprite(&mut commands, -75., colors.map(Hsla::from)); spawn_mixed_sprite(&mut commands, -175., colors.map(Srgba::from)); spawn_mixed_sprite(&mut commands, -275., colors.map(Oklcha::from)); } fn spawn_curve_sprite(commands: &mut Commands, y: f32, points: [T; 4]) { commands.spawn(( Sprite::sized(Vec2::new(75., 75.)), Transform::from_xyz(0., y, 0.), Curve(CubicBezier::new([points]).to_curve().unwrap()), )); } fn spawn_mixed_sprite(commands: &mut Commands, y: f32, colors: [T; 4]) { commands.spawn(( Transform::from_xyz(0., y, 0.), Sprite::sized(Vec2::new(75., 75.)), Mixed(colors), )); } fn animate_curve( time: Res