use crate::colors::Color;
#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ImColor32(u32);
impl ImColor32 {
pub const BLACK: Self = Self(0xff_00_00_00);
pub const WHITE: Self = Self(0xff_ff_ff_ff);
pub const TRANSPARENT: Self = Self(0);
#[inline]
pub const fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self(((a as u32) << 24) | (r as u32) | ((g as u32) << 8) | ((b as u32) << 16))
}
#[inline]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self::from_rgba(r, g, b, 0xff)
}
pub fn from_rgba_f32s(r: f32, g: f32, b: f32, a: f32) -> Self {
Self::from_rgba(
(r.clamp(0.0, 1.0) * 255.0) as u8,
(g.clamp(0.0, 1.0) * 255.0) as u8,
(b.clamp(0.0, 1.0) * 255.0) as u8,
(a.clamp(0.0, 1.0) * 255.0) as u8,
)
}
#[inline]
pub const fn to_bits(self) -> u32 {
self.0
}
}
impl From<Color> for ImColor32 {
fn from(color: Color) -> Self {
Self::from_rgba_f32s(color.r, color.g, color.b, color.a)
}
}
impl From<[f32; 4]> for ImColor32 {
fn from(arr: [f32; 4]) -> Self {
Self::from_rgba_f32s(arr[0], arr[1], arr[2], arr[3])
}
}
impl From<(f32, f32, f32, f32)> for ImColor32 {
fn from((r, g, b, a): (f32, f32, f32, f32)) -> Self {
Self::from_rgba_f32s(r, g, b, a)
}
}
impl From<[f32; 3]> for ImColor32 {
fn from(arr: [f32; 3]) -> Self {
Self::from_rgba_f32s(arr[0], arr[1], arr[2], 1.0)
}
}
impl From<(f32, f32, f32)> for ImColor32 {
fn from((r, g, b): (f32, f32, f32)) -> Self {
Self::from_rgba_f32s(r, g, b, 1.0)
}
}
impl From<ImColor32> for u32 {
fn from(color: ImColor32) -> Self {
color.0
}
}
impl From<u32> for ImColor32 {
fn from(color: u32) -> Self {
ImColor32(color)
}
}