1use azalea_buf::AzBuf;
2use azalea_codec::Codec;
3
4use crate::position::Vec3f32;
5
6#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, PartialEq)]
7pub struct RgbColor {
8 value: u32,
9}
10azalea_codec::impl_codec_from!(RgbColor, SerializableRgbColor, struct IncompleteRgbColor);
11
12impl RgbColor {
13 pub const BLACK: Self = Self { value: 0 };
14 pub const WHITE: Self = Self { value: u32::MAX };
16
17 pub fn new(r: u8, g: u8, b: u8) -> Self {
18 Self {
19 value: ((r as u32) << 16) | ((g as u32) << 8) | (b as u32),
20 }
21 }
22 pub fn new_from_floats(r: f32, g: f32, b: f32) -> Self {
23 Self::new((r * 255.) as u8, (g * 255.) as u8, (b * 255.) as u8)
24 }
25
26 pub fn red(&self) -> u8 {
27 (self.value >> 16) as u8
28 }
29 pub fn green(&self) -> u8 {
30 (self.value >> 8) as u8
31 }
32 pub fn blue(&self) -> u8 {
33 self.value as u8
34 }
35}
36
37#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, PartialEq)]
38pub struct ArgbColor {
39 value: u32,
40}
41azalea_codec::impl_codec_from!(ArgbColor, SerializableRgbColor, struct IncompleteArgbColor);
42
43impl ArgbColor {
44 pub fn new(a: u8, r: u8, g: u8, b: u8) -> Self {
45 Self {
46 value: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | b as u32,
47 }
48 }
49 pub fn new_from_floats(a: f32, r: f32, g: f32, b: f32) -> Self {
50 Self::new(
51 (a * 255.) as u8,
52 (r * 255.) as u8,
53 (g * 255.) as u8,
54 (b * 255.) as u8,
55 )
56 }
57
58 pub fn alpha(&self) -> u8 {
59 (self.value >> 24) as u8
60 }
61 pub fn red(&self) -> u8 {
62 (self.value >> 16) as u8
63 }
64 pub fn green(&self) -> u8 {
65 (self.value >> 8) as u8
66 }
67 pub fn blue(&self) -> u8 {
68 self.value as u8
69 }
70}
71
72#[derive(Codec)]
73enum SerializableRgbColor {
74 Int(i32),
75 Vec3f32(Vec3f32),
76}
77impl From<SerializableRgbColor> for RgbColor {
78 fn from(r: SerializableRgbColor) -> Self {
79 match r {
80 SerializableRgbColor::Int(i) => Self { value: i as u32 },
81 SerializableRgbColor::Vec3f32(Vec3f32 { x, y, z }) => Self::new_from_floats(x, y, z),
82 }
83 }
84}
85impl From<RgbColor> for SerializableRgbColor {
86 fn from(r: RgbColor) -> Self {
87 Self::Int(r.value as i32)
88 }
89}
90impl From<SerializableRgbColor> for ArgbColor {
91 fn from(r: SerializableRgbColor) -> Self {
92 match r {
93 SerializableRgbColor::Int(i) => Self { value: i as u32 },
94 SerializableRgbColor::Vec3f32(Vec3f32 { x, y, z }) => {
95 Self::new_from_floats(1., x, y, z)
96 }
97 }
98 }
99}
100impl From<ArgbColor> for SerializableRgbColor {
101 fn from(r: ArgbColor) -> Self {
102 Self::Int(r.value as i32)
103 }
104}