cog_task/resource/
color.rs1use eframe::egui::Color32;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
5#[serde(rename_all = "snake_case")]
6pub enum Color {
7 Inherit,
8 Transparent,
9 White,
10 Black,
11 Gray,
12 Red,
13 Blue,
14 Green,
15 Yellow,
16 Rgb(u8, u8, u8),
17 Rgba(u8, u8, u8, u8),
18}
19
20impl Default for Color {
21 #[inline(always)]
22 fn default() -> Self {
23 Color::Inherit
24 }
25}
26
27impl From<&Color> for Color32 {
28 #[inline]
29 fn from(c: &Color) -> Self {
30 match c {
31 Color::Inherit | Color::Transparent => Color32::TRANSPARENT,
32 Color::White => Color32::WHITE,
33 Color::Black => Color32::BLACK,
34 Color::Gray => Color32::GRAY,
35 Color::Red => Color32::RED,
36 Color::Blue => Color32::BLUE,
37 Color::Green => Color32::GREEN,
38 Color::Yellow => Color32::YELLOW,
39 Color::Rgb(r, g, b) => Color32::from_rgb(*r, *g, *b),
40 Color::Rgba(r, g, b, a) => Color32::from_rgba_unmultiplied(*r, *g, *b, *a),
41 }
42 }
43}
44
45impl From<Color> for Color32 {
46 #[inline(always)]
47 fn from(c: Color) -> Self {
48 Self::from(&c)
49 }
50}
51
52impl Color {
53 pub fn or(&self, other: &Self) -> Self {
54 if let Self::Inherit = self {
55 *other
56 } else {
57 *self
58 }
59 }
60}