#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const TRANSPARENT: Color = Color::rgba(0.0, 0.0, 0.0, 0.0);
pub const BLACK: Color = Color::rgba(0.0, 0.0, 0.0, 1.0);
pub const WHITE: Color = Color::rgba(1.0, 1.0, 1.0, 1.0);
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b, a: 1.0 }
}
pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
Self {
r: r as f32 / 255.0,
g: g as f32 / 255.0,
b: b as f32 / 255.0,
a: a as f32 / 255.0,
}
}
pub fn with_alpha(self, a: f32) -> Self {
Self { a, ..self }
}
pub fn components(self) -> [f32; 4] {
[self.r, self.g, self.b, self.a]
}
pub fn premultiplied(self) -> [f32; 4] {
[self.r * self.a, self.g * self.a, self.b * self.a, self.a]
}
pub fn is_opaque(self) -> bool {
self.a >= 1.0
}
}