#![no_std]
#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct ColorAlpha<ComponentTy, ColorTy> {
pub color: ColorTy,
pub alpha: ComponentTy,
}
#[cfg(feature = "bytemuck")]
unsafe impl<ComponentTy: Zeroable, ColorTy: Zeroable> Zeroable
for ColorAlpha<ComponentTy, ColorTy>
{
}
#[cfg(feature = "bytemuck")]
unsafe impl<ComponentTy: Pod, ColorTy: Pod> Pod for ColorAlpha<ComponentTy, ColorTy> {}
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct PremultipliedColorAlpha<ComponentTy, ColorTy> {
pub color: ColorTy,
pub alpha: ComponentTy,
}
#[cfg(feature = "bytemuck")]
unsafe impl<ComponentTy: Zeroable, ColorTy: Zeroable> Zeroable
for PremultipliedColorAlpha<ComponentTy, ColorTy>
{
}
#[cfg(feature = "bytemuck")]
unsafe impl<ComponentTy: Pod, ColorTy: Pod> Pod for PremultipliedColorAlpha<ComponentTy, ColorTy> {}
macro_rules! color_struct {
{
$(#[$doc:meta])*
$name:ident {
$($(#[$compdoc:meta])+
$compname:ident,)+
}
} => {
$(#[$doc])*
#[repr(C)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct $name<ComponentTy> {
$($(#[$compdoc])+
pub $compname: ComponentTy,)+
}
#[cfg(feature = "bytemuck")]
unsafe impl<ComponentTy: Zeroable> Zeroable for $name<ComponentTy> {}
#[cfg(feature = "bytemuck")]
unsafe impl<ComponentTy: Pod> Pod for $name<ComponentTy> {}
impl<ComponentTy> From<[ComponentTy; 3]> for $name<ComponentTy> {
fn from([$($compname),+]: [ComponentTy; 3]) -> $name<ComponentTy> {
$name {
$($compname,)+
}
}
}
#[allow(clippy::from_over_into)]
impl<ComponentTy> Into<[ComponentTy; 3]> for $name<ComponentTy> {
fn into(self) -> [ComponentTy; 3] {
let $name {
$($compname,)+
} = self;
[$($compname),+]
}
}
impl<ComponentTy> AsRef<[ComponentTy; 3]> for $name<ComponentTy> {
fn as_ref(&self) -> &[ComponentTy; 3] {
unsafe { &*(self as *const $name<ComponentTy> as *const [ComponentTy; 3]) }
}
}
macro_rules! impl_alpha_traits {
($alphaty:ident) => {
impl<ComponentTy> From<$alphaty<ComponentTy, $name<ComponentTy>>> for $name<ComponentTy> {
fn from(col_alpha: $alphaty<ComponentTy, $name<ComponentTy>>) -> $name<ComponentTy> {
col_alpha.color
}
}
impl<ComponentTy> From<[ComponentTy; 4]> for $alphaty<ComponentTy, $name<ComponentTy>> {
fn from([a, b, c, alpha]: [ComponentTy; 4]) -> $alphaty<ComponentTy, $name<ComponentTy>> {
$alphaty {
color: $name::from([a, b, c]),
alpha
}
}
}
#[allow(clippy::from_over_into)]
impl<ComponentTy> Into<[ComponentTy; 4]> for $alphaty<ComponentTy, $name<ComponentTy>> {
fn into(self) -> [ComponentTy; 4] {
let $alphaty {
color,
alpha
} = self;
let $name {
$($compname,)+
} = color;
[$($compname,)+ alpha]
}
}
}
}
impl_alpha_traits!(ColorAlpha);
impl_alpha_traits!(PremultipliedColorAlpha);
};
}
color_struct! {
EncodedSrgb {
r,
g,
b,
}
}
color_struct! {
LinearSrgb {
r,
g,
b,
}
}
color_struct! {
EncodedRec709 {
r,
g,
b,
}
}
color_struct! {
Rec709 {
r,
g,
b,
}
}
color_struct! {
GenericColor {
comp1,
comp2,
comp3,
}
}
color_struct! {
AcesCg {
r,
g,
b,
}
}
color_struct! {
Aces2065 {
r,
g,
b,
}
}
color_struct! {
AcesCc {
r,
g,
b,
}
}
color_struct! {
AcesCct {
r,
g,
b,
}
}
color_struct! {
DisplayP3 {
r,
g,
b,
}
}
color_struct! {
EncodedDisplayP3 {
r,
g,
b,
}
}
color_struct! {
DciP3 {
r,
g,
b,
}
}
color_struct! {
DciXYZPrime {
x,
y,
z,
}
}
color_struct! {
Bt2020 {
r,
g,
b,
}
}
color_struct! {
EncodedBt2020 {
r,
g,
b,
}
}
color_struct! {
Bt2100 {
r,
g,
b,
}
}
color_struct! {
EncodedBt2100PQ {
r,
g,
b,
}
}
color_struct! {
EncodedBt2100HLG {
r,
g,
b,
}
}
color_struct! {
ICtCpPQ {
i,
ct,
cp,
}
}
color_struct! {
ICtCpHLG {
i,
ct,
cp,
}
}
color_struct! {
CieXYZ {
x,
y,
z,
}
}
color_struct! {
CieLab {
l,
a,
b,
}
}
color_struct! {
CieLCh {
l,
c,
h,
}
}
color_struct! {
Oklab {
l,
a,
b,
}
}
color_struct! {
Oklch {
l,
c,
h,
}
}