#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Color
{
r: f32,
g: f32,
b: f32,
a: f32
}
impl Color
{
pub const TRANSPARENT: Color = Color::from_rgba(0.0, 0.0, 0.0, 0.0);
pub const BLACK: Color = Color::from_rgb(0.0, 0.0, 0.0);
pub const WHITE: Color = Color::from_rgb(1.0, 1.0, 1.0);
pub const RED: Color = Color::from_rgb(1.0, 0.0, 0.0);
pub const GREEN: Color = Color::from_rgb(0.0, 1.0, 0.0);
pub const BLUE: Color = Color::from_rgb(0.0, 0.0, 1.0);
pub const YELLOW: Color = Color::from_rgb(1.0, 1.0, 0.0);
pub const CYAN: Color = Color::from_rgb(0.0, 1.0, 1.0);
pub const MAGENTA: Color = Color::from_rgb(1.0, 0.0, 1.0);
pub const GRAY: Color = Color::from_rgb(0.5, 0.5, 0.5);
pub const LIGHT_GRAY: Color = Color::from_rgb(0.75, 0.75, 0.75);
pub const DARK_GRAY: Color = Color::from_rgb(0.25, 0.25, 0.25);
#[inline]
pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Self
{
Color { r, g, b, a }
}
#[inline]
pub const fn from_rgb(r: f32, g: f32, b: f32) -> Self
{
Color { r, g, b, a: 1.0 }
}
#[inline]
pub fn from_int_rgba(r: u8, g: u8, b: u8, a: u8) -> Self
{
Color {
r: r as f32 / 255.0,
g: g as f32 / 255.0,
b: b as f32 / 255.0,
a: a as f32 / 255.0
}
}
#[inline]
pub fn from_int_rgb(r: u8, g: u8, b: u8) -> Self
{
Color {
r: r as f32 / 255.0,
g: g as f32 / 255.0,
b: b as f32 / 255.0,
a: 1.0
}
}
#[inline]
pub fn from_hex_argb(argb: u32) -> Self
{
Color::from_int_rgba(
(argb >> 16) as u8,
(argb >> 8) as u8,
argb as u8,
(argb >> 24) as u8
)
}
#[inline]
pub fn from_hex_rgb(rgb: u32) -> Self
{
Color::from_int_rgb((rgb >> 16) as u8, (rgb >> 8) as u8, rgb as u8)
}
#[inline]
pub const fn from_gray(brightness: f32) -> Self
{
Self::from_rgb(brightness, brightness, brightness)
}
#[inline]
pub const fn r(&self) -> f32
{
self.r
}
#[inline]
pub const fn g(&self) -> f32
{
self.g
}
#[inline]
pub const fn b(&self) -> f32
{
self.b
}
#[inline]
pub const fn a(&self) -> f32
{
self.a
}
pub fn subjective_brightness(&self) -> f32
{
self.r * 0.299 + self.g * 0.587 + self.b * 0.114
}
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn test_from_hex()
{
assert_eq!(
Color::from_hex_rgb(0xFF5511),
Color::from_int_rgb(0xFF, 0x55, 0x11)
);
assert_eq!(
Color::from_hex_argb(0xAAFF5511),
Color::from_int_rgba(0xFF, 0x55, 0x11, 0xAA)
);
}
}