use super::Rgba32;
impl Rgba32 {
pub const BLACK: Self = Self::from_rgb(0, 0, 0);
pub const WHITE: Self = Self::from_rgb(255, 255, 255);
pub const TRANSPARENT: Self = Self::from_unmultiplied_rgba(0, 0, 0, 0);
#[inline]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self::from_unmultiplied_rgba(r, g, b, 255)
}
#[inline]
pub const fn from_unmultiplied_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
let [r, g, b, a] = [r as u32, g as u32, b as u32, a as u32];
Self(r << 24 | g << 16 | b << 8 | a)
}
#[inline]
pub const fn from_u32(rgba: u32) -> Self {
Self(rgba)
}
#[inline]
pub const fn to_array(self) -> [u8; 4] {
[
(self.0 >> 24) as u8,
(self.0 >> 16) as u8,
(self.0 >> 8) as u8,
self.0 as u8,
]
}
#[inline]
pub const fn to_u32(self) -> u32 {
self.0
}
}
impl From<(u8, u8, u8)> for Rgba32 {
#[inline]
fn from((r, g, b): (u8, u8, u8)) -> Self {
Self::from_rgb(r, g, b)
}
}
impl From<[u8; 3]> for Rgba32 {
#[inline]
fn from([r, g, b]: [u8; 3]) -> Self {
Self::from_rgb(r, g, b)
}
}
impl From<[u8; 4]> for Rgba32 {
#[inline]
fn from([r, g, b, a]: [u8; 4]) -> Self {
Self::from_unmultiplied_rgba(r, g, b, a)
}
}
impl From<(u8, u8, u8, u8)> for Rgba32 {
#[inline]
fn from((r, g, b, a): (u8, u8, u8, u8)) -> Self {
Self::from_unmultiplied_rgba(r, g, b, a)
}
}
#[cfg(feature = "ecolor")]
impl From<Rgba32> for ecolor::Color32 {
fn from(color: Rgba32) -> Self {
let [r, g, b, a] = color.to_array();
Self::from_rgba_unmultiplied(r, g, b, a)
}
}
#[cfg(feature = "ecolor")]
impl From<Rgba32> for ecolor::Rgba {
fn from(color: Rgba32) -> Self {
let color: ecolor::Color32 = color.into();
color.into()
}
}
#[cfg(feature = "ecolor")]
impl From<ecolor::Rgba> for Rgba32 {
fn from(val: ecolor::Rgba) -> Self {
val.to_srgba_unmultiplied().into()
}
}
#[cfg(feature = "ecolor")]
impl From<ecolor::Color32> for Rgba32 {
fn from(val: ecolor::Color32) -> Self {
val.to_srgba_unmultiplied().into()
}
}