clay_layout/
color.rs

1use crate::bindings::*;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4#[repr(C)]
5pub struct Color {
6    pub r: f32,
7    pub g: f32,
8    pub b: f32,
9    pub a: f32,
10}
11
12impl Color {
13    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
14        Self { r, g, b, a: 255.0 }
15    }
16    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
17        Self { r, g, b, a }
18    }
19
20    /// Allows using hex values to build colors
21    /// ```
22    /// use clay_layout::color::Color;
23    /// assert_eq!(Color::rgb(255.0, 255.0, 255.0), Color::u_rgb(0xFF, 0xFF, 0xFF));
24    /// ```
25    pub const fn u_rgb(r: u8, g: u8, b: u8) -> Self {
26        Self::rgb(r as _, g as _, b as _)
27    }
28    /// Allows using hex values to build colors
29    /// ```
30    /// use clay_layout::color::Color;
31    /// assert_eq!(Color::rgba(255.0, 255.0, 255.0, 255.0), Color::u_rgba(0xFF, 0xFF, 0xFF, 0xFF));
32    /// ```
33    pub const fn u_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
34        Self::rgba(r as _, g as _, b as _, a as _)
35    }
36}
37
38impl From<Clay_Color> for Color {
39    fn from(value: Clay_Color) -> Self {
40        unsafe { core::mem::transmute(value) }
41    }
42}
43impl From<Color> for Clay_Color {
44    fn from(value: Color) -> Self {
45        unsafe { core::mem::transmute(value) }
46    }
47}
48
49impl From<(f32, f32, f32)> for Color {
50    fn from(value: (f32, f32, f32)) -> Self {
51        Self::rgb(value.0, value.1, value.2)
52    }
53}
54impl From<(f32, f32, f32, f32)> for Color {
55    fn from(value: (f32, f32, f32, f32)) -> Self {
56        Self::rgba(value.0, value.1, value.2, value.3)
57    }
58}
59
60impl From<(u8, u8, u8)> for Color {
61    fn from(value: (u8, u8, u8)) -> Self {
62        Self::u_rgb(value.0, value.1, value.2)
63    }
64}
65impl From<(u8, u8, u8, u8)> for Color {
66    fn from(value: (u8, u8, u8, u8)) -> Self {
67        Self::u_rgba(value.0, value.1, value.2, value.3)
68    }
69}