refactor(colorscheme): add RosePine enum

This commit is contained in:
2024-12-27 14:01:52 +02:00
parent 4635b0f134
commit 3b5c92e998
10 changed files with 206 additions and 52 deletions

View File

@@ -14,7 +14,7 @@ impl TransitionFloor {
self.into()
}
pub fn opposite(&self) -> Self {
pub const fn opposite(&self) -> Self {
match self {
Self::Ascend => Self::Descend,
Self::Descend => Self::Ascend,
@@ -31,7 +31,7 @@ impl TransitionFloor {
impl From<TransitionFloor> for f32 {
fn from(value: TransitionFloor) -> Self {
f32::from(&value)
Self::from(&value)
}
}

View File

@@ -1,8 +1,8 @@
use crate::theme::palette::rose_pine::{LOVE, PINE};
use super::resources::GlobalMazeConfig;
use crate::theme::{palette::rose_pine::RosePine, prelude::ColorScheme};
use bevy::{prelude::*, utils::HashMap};
use std::f32::consts::FRAC_PI_2;
use strum::IntoEnumIterator;
const WALL_OVERLAP_MODIFIER: f32 = 1.25;
const HEX_SIDES: u32 = 6;
@@ -13,7 +13,7 @@ pub struct MazeAssets {
pub wall_mesh: Handle<Mesh>,
pub hex_material: Handle<StandardMaterial>,
pub wall_material: Handle<StandardMaterial>,
pub custom_materials: HashMap<String, Handle<StandardMaterial>>,
pub custom_materials: HashMap<RosePine, Handle<StandardMaterial>>,
}
impl MazeAssets {
@@ -22,11 +22,9 @@ impl MazeAssets {
materials: &mut ResMut<Assets<StandardMaterial>>,
global_config: &GlobalMazeConfig,
) -> Self {
let mut custom_materials = HashMap::new();
custom_materials.extend(vec![
("LOVE".to_string(), materials.add(red_material())),
("PINE".to_string(), materials.add(blue_material())),
]);
let custom_materials = RosePine::iter()
.map(|color| (color, materials.add(color.to_standart_material())))
.collect();
Self {
hex_mesh: meshes.add(generate_hex_mesh(
global_config.hex_size,
@@ -73,17 +71,3 @@ pub fn white_material() -> StandardMaterial {
..default()
}
}
pub fn red_material() -> StandardMaterial {
StandardMaterial {
emissive: LOVE.to_linear(),
..default()
}
}
pub fn blue_material() -> StandardMaterial {
StandardMaterial {
emissive: PINE.to_linear(),
..default()
}
}

View File

@@ -7,6 +7,7 @@ use crate::{
events::SpawnMaze,
resources::GlobalMazeConfig,
},
theme::palette::rose_pine::RosePine,
};
use bevy::prelude::*;
use hexlab::prelude::*;
@@ -100,12 +101,12 @@ pub(super) fn spawn_single_hex_tile(
let material = match tile.pos() {
pos if pos == maze_config.start_pos => assets
.custom_materials
.get("PINE")
.get(&RosePine::Pine)
.cloned()
.unwrap_or_default(),
pos if pos == maze_config.end_pos => assets
.custom_materials
.get("LOVE")
.get(&RosePine::Love)
.cloned()
.unwrap_or_default(),
_ => assets.hex_material.clone(),

View File

@@ -1,6 +1,7 @@
use crate::theme::palette::rose_pine::PINE;
use bevy::prelude::*;
use crate::theme::{palette::rose_pine::RosePine, prelude::ColorScheme};
pub(super) fn generate_pill_mesh(radius: f32, half_length: f32) -> Mesh {
Mesh::from(Capsule3d {
radius,
@@ -9,9 +10,10 @@ pub(super) fn generate_pill_mesh(radius: f32, half_length: f32) -> Mesh {
}
pub(super) fn blue_material() -> StandardMaterial {
let color = RosePine::Pine;
StandardMaterial {
base_color: PINE,
emissive: PINE.to_linear() * 3.,
base_color: color.to_color(),
emissive: color.to_linear_rgba() * 3.,
..default()
}
}

104
src/theme/colorscheme.rs Normal file
View File

@@ -0,0 +1,104 @@
use bevy::prelude::*;
use std::ops::Deref;
/// A trait for types that can be converted to a Bevy `Color`.
///
/// Implementing this trait allows a type to be easily converted to various Bevy color types.
///
/// # Examples
///
/// ```
/// use bevy::prelude::*;
/// use maze_ascension::theme::prelude::ColorScheme;
///
/// struct MyColor(u8, u8, u8);
///
/// impl ColorScheme for MyColor {
/// fn to_color(&self) -> Color {
/// Color::srgb(
/// self.0 as f32 / 255.0,
/// self.1 as f32 / 255.0,
/// self.2 as f32 / 255.0
/// )
/// }
/// }
///
/// let my_color = MyColor(255, 0, 0);
/// let bevy_color: Color = my_color.to_color();
/// assert_eq!(bevy_color, Color::srgb(1., 0., 0.));
/// ```
pub trait ColorScheme {
/// Converts the implementing type to a Bevy `Color`.
fn to_color(&self) -> Color;
/// Converts the implementing type to a Bevy `LinearRgba`.
///
/// This method provides a default implementation based on `to_color()`.
fn to_linear_rgba(&self) -> LinearRgba {
self.to_color().to_linear()
}
/// Converts the implementing type to a Bevy `StandardMaterial`.
///
/// This method provides a default implementation that sets the emissive color.
fn to_standart_material(&self) -> StandardMaterial {
StandardMaterial {
emissive: self.to_linear_rgba(),
..default()
}
}
}
/// A wrapper type that implements `From` traits for types implementing `ColorScheme`.
///
/// This wrapper allows for easy conversion from `ColorScheme` types to Bevy color types.
///
/// # Examples
///
/// ```
/// use bevy::prelude::*;
/// use maze_ascension::theme::prelude::{ColorScheme, ColorSchemeWrapper};
///
/// struct MyColor(u8, u8, u8);
///
/// impl ColorScheme for MyColor {
/// fn to_color(&self) -> Color {
/// Color::srgb(
/// self.0 as f32 / 255.0,
/// self.1 as f32 / 255.0,
/// self.2 as f32 / 255.0
/// )
/// }
/// }
///
/// let my_color = MyColor(0, 255, 0);
/// let wrapper = ColorSchemeWrapper(my_color);
/// let bevy_color: Color = wrapper.into();
/// assert_eq!(bevy_color, Color::srgb(0., 1., 0.));
/// ```
pub struct ColorSchemeWrapper<T: ColorScheme>(pub T);
impl<T: ColorScheme> From<T> for ColorSchemeWrapper<T> {
fn from(value: T) -> Self {
Self(value)
}
}
impl<T: ColorScheme> Deref for ColorSchemeWrapper<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ColorScheme> From<ColorSchemeWrapper<T>> for Color {
fn from(value: ColorSchemeWrapper<T>) -> Self {
value.to_color()
}
}
impl<T: ColorScheme> From<ColorSchemeWrapper<T>> for LinearRgba {
fn from(value: ColorSchemeWrapper<T>) -> Self {
value.to_linear_rgba()
}
}

View File

@@ -2,6 +2,7 @@
// Unused utilities may trigger this lints undesirably.
mod colorscheme;
pub mod interaction;
pub mod palette;
mod widgets;
@@ -9,6 +10,7 @@ mod widgets;
#[allow(unused_imports)]
pub mod prelude {
pub use super::{
colorscheme::{ColorScheme, ColorSchemeWrapper},
interaction::{InteractionPalette, OnPress},
palette as ui_palette,
widgets::{Containers as _, Widgets as _},

View File

@@ -1,18 +1,45 @@
use super::rgb_u8;
use crate::theme::prelude::ColorScheme;
use bevy::prelude::*;
use strum::EnumIter;
pub const BASE: Color = rgb_u8(25, 23, 36);
pub const SURFACE: Color = rgb_u8(31, 29, 46);
pub const OVERLAY: Color = rgb_u8(38, 35, 58);
pub const MUTED: Color = rgb_u8(110, 106, 134);
pub const SUBTLE: Color = rgb_u8(144, 140, 170);
pub const TEXT: Color = rgb_u8(224, 222, 244);
pub const LOVE: Color = rgb_u8(235, 111, 146);
pub const GOLD: Color = rgb_u8(246, 193, 119);
pub const ROSE: Color = rgb_u8(235, 188, 186);
pub const PINE: Color = rgb_u8(49, 116, 143);
pub const FOAM: Color = rgb_u8(156, 207, 216);
pub const IRIS: Color = rgb_u8(196, 167, 231);
pub const HIGHLIGHT_LOW: Color = rgb_u8(33, 32, 46);
pub const HIGHLIGHT_MED: Color = rgb_u8(64, 61, 82);
pub const HIGHLIGHT_HIGH: Color = rgb_u8(82, 79, 103);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
pub enum RosePine {
Base,
Surface,
Overlay,
Muted,
Subtle,
Text,
Love,
Gold,
Rose,
Pine,
Foam,
Iris,
HighlightLow,
HighlightMed,
HighlightHigh,
}
impl ColorScheme for RosePine {
fn to_color(&self) -> Color {
match self {
Self::Base => rgb_u8(25, 23, 36),
Self::Surface => rgb_u8(31, 29, 46),
Self::Overlay => rgb_u8(38, 35, 58),
Self::Muted => rgb_u8(110, 106, 134),
Self::Subtle => rgb_u8(144, 140, 170),
Self::Text => rgb_u8(224, 222, 244),
Self::Love => rgb_u8(235, 111, 146),
Self::Gold => rgb_u8(246, 193, 119),
Self::Rose => rgb_u8(235, 188, 186),
Self::Pine => rgb_u8(49, 116, 143),
Self::Foam => rgb_u8(156, 207, 216),
Self::Iris => rgb_u8(196, 167, 231),
Self::HighlightLow => rgb_u8(33, 32, 46),
Self::HighlightMed => rgb_u8(64, 61, 82),
Self::HighlightHigh => rgb_u8(82, 79, 103),
}
}
}