Skip to main content

cranpose_ui_graphics/
color.rs

1//! Color representation and color space utilities
2
3#[derive(Clone, Copy, Debug, PartialEq)]
4pub struct Color(pub f32, pub f32, pub f32, pub f32);
5
6impl Color {
7    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
8        Self(r, g, b, 1.0)
9    }
10
11    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
12        Self(r, g, b, a)
13    }
14
15    pub const fn from_rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self {
16        Self(
17            r as f32 / 255.0,
18            g as f32 / 255.0,
19            b as f32 / 255.0,
20            a as f32 / 255.0,
21        )
22    }
23
24    pub const fn from_rgb_u8(r: u8, g: u8, b: u8) -> Self {
25        Self::from_rgba_u8(r, g, b, 255)
26    }
27
28    pub fn r(&self) -> f32 {
29        self.0
30    }
31
32    pub fn g(&self) -> f32 {
33        self.1
34    }
35
36    pub fn b(&self) -> f32 {
37        self.2
38    }
39
40    pub fn a(&self) -> f32 {
41        self.3
42    }
43
44    pub fn with_alpha(&self, alpha: f32) -> Self {
45        Self(self.0, self.1, self.2, alpha)
46    }
47
48    // Common color constants
49    pub const BLACK: Color = Color(0.0, 0.0, 0.0, 1.0);
50    pub const WHITE: Color = Color(1.0, 1.0, 1.0, 1.0);
51    pub const RED: Color = Color(1.0, 0.0, 0.0, 1.0);
52    pub const GREEN: Color = Color(0.0, 1.0, 0.0, 1.0);
53    pub const BLUE: Color = Color(0.0, 0.0, 1.0, 1.0);
54    pub const TRANSPARENT: Color = Color(0.0, 0.0, 0.0, 0.0);
55}