paiagram/
colors.rs

1use bevy::color::{Srgba, palettes::tailwind::*};
2use egui::{Color32, Widget};
3use strum_macros::{EnumCount, EnumIter};
4
5#[derive(Debug, Clone, Copy)]
6pub enum DisplayColor {
7    Predefined(PredefinedColor),
8    Custom(Color32),
9}
10
11impl Default for DisplayColor {
12    fn default() -> Self {
13        Self::Predefined(PredefinedColor::Neutral)
14    }
15}
16
17impl DisplayColor {
18    pub fn get(self, is_dark: bool) -> Color32 {
19        match self {
20            Self::Predefined(p) => p.get(is_dark),
21            Self::Custom(c) => c,
22        }
23    }
24}
25
26impl Widget for DisplayColor {
27    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
28        let is_dark = ui.visuals().dark_mode;
29        let a = Color32::default();
30        ui.button("123")
31        // TODO: finish this
32    }
33}
34
35#[derive(Debug, Clone, Copy, EnumIter, EnumCount)]
36pub enum PredefinedColor {
37    Red,
38    Orange,
39    Amber,
40    Yellow,
41    Lime,
42    Green,
43    Emerald,
44    Teal,
45    Cyan,
46    Sky,
47    Blue,
48    Indigo,
49    Violet,
50    Purple,
51    Fuchsia,
52    Pink,
53    Rose,
54    Slate,
55    Gray,
56    Zinc,
57    Neutral,
58    Stone,
59}
60
61impl PredefinedColor {
62    // use 700 shade if light, otherwise use 400
63    // neutral is special
64    pub const fn get(self, is_dark: bool) -> Color32 {
65        #[rustfmt::skip]
66        let c = match (self, is_dark) {
67            (Self::Red, true)       => RED_400,
68            (Self::Red, false)      => RED_700,
69            (Self::Orange, true)    => ORANGE_400,
70            (Self::Orange, false)   => ORANGE_700,
71            (Self::Amber, true)     => AMBER_400,
72            (Self::Amber, false)    => AMBER_700,
73            (Self::Yellow, true)    => YELLOW_400,
74            (Self::Yellow, false)   => YELLOW_700,
75            (Self::Lime, true)      => LIME_400,
76            (Self::Lime, false)     => LIME_700,
77            (Self::Green, true)     => GREEN_400,
78            (Self::Green, false)    => GREEN_700,
79            (Self::Emerald, true)   => EMERALD_400,
80            (Self::Emerald, false)  => EMERALD_700,
81            (Self::Teal, true)      => TEAL_400,
82            (Self::Teal, false)     => TEAL_700,
83            (Self::Cyan, true)      => CYAN_400,
84            (Self::Cyan, false)     => CYAN_700,
85            (Self::Sky, true)       => SKY_400,
86            (Self::Sky, false)      => SKY_700,
87            (Self::Blue, true)      => BLUE_400,
88            (Self::Blue, false)     => BLUE_700,
89            (Self::Indigo, true)    => INDIGO_400,
90            (Self::Indigo, false)   => INDIGO_700,
91            (Self::Violet, true)    => VIOLET_400,
92            (Self::Violet, false)   => VIOLET_700,
93            (Self::Purple, true)    => PURPLE_400,
94            (Self::Purple, false)   => PURPLE_700,
95            (Self::Fuchsia, true)   => FUCHSIA_400,
96            (Self::Fuchsia, false)  => FUCHSIA_700,
97            (Self::Pink, true)      => PINK_400,
98            (Self::Pink, false)     => PINK_700,
99            (Self::Rose, true)      => ROSE_400,
100            (Self::Rose, false)     => ROSE_700,
101            (Self::Slate, true)     => SLATE_400,
102            (Self::Slate, false)    => SLATE_700,
103            (Self::Gray, true)      => GRAY_400,
104            (Self::Gray, false)     => GRAY_700,
105            (Self::Zinc, true)      => ZINC_400,
106            (Self::Zinc, false)     => ZINC_700,
107            (Self::Neutral, true)   => NEUTRAL_400,
108            (Self::Neutral, false)  => NEUTRAL_700,
109            (Self::Stone, true)     => STONE_400,
110            (Self::Stone, false)    => STONE_700,
111        };
112        translate_srgba_to_color32(c)
113    }
114}
115
116pub const fn translate_srgba_to_color32(c: Srgba) -> Color32 {
117    Color32::from_rgba_unmultiplied_const(
118        (c.red * 256.0) as u8,
119        (c.green * 256.0) as u8,
120        (c.blue * 256.0) as u8,
121        (c.alpha * 256.0) as u8,
122    )
123}