use crate::math::MintVec4;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
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) << Self::A_SHIFT)
| ((r as u32) << Self::R_SHIFT)
| ((g as u32) << Self::G_SHIFT)
| ((b as u32) << Self::B_SHIFT),
)
}
#[inline]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self::from_rgba(r, g, b, 0xff)
}
#[inline]
pub fn from_rgba_f32s(r: f32, g: f32, b: f32, a: f32) -> Self {
Self::from_rgba(
f32_to_u8_sat(r),
f32_to_u8_sat(g),
f32_to_u8_sat(b),
f32_to_u8_sat(a),
)
}
#[inline]
pub fn to_rgba_f32s(self) -> [f32; 4] {
let &ImColor32Fields { r, g, b, a } = &*self;
[
u8_to_f32_sat(r),
u8_to_f32_sat(g),
u8_to_f32_sat(b),
u8_to_f32_sat(a),
]
}
#[inline]
pub fn to_rgba(self) -> [u8; 4] {
let &ImColor32Fields { r, g, b, a } = &*self;
[r, g, b, a]
}
#[inline]
pub fn from_rgb_f32s(r: f32, g: f32, b: f32) -> Self {
Self::from_rgba(f32_to_u8_sat(r), f32_to_u8_sat(g), f32_to_u8_sat(b), 0xff)
}
#[inline]
pub const fn from_bits(u: u32) -> Self {
Self(u)
}
#[inline]
pub const fn to_bits(self) -> u32 {
self.0
}
const R_SHIFT: u32 = 0;
const G_SHIFT: u32 = 8;
const B_SHIFT: u32 = 16;
const A_SHIFT: u32 = 24;
}
impl Default for ImColor32 {
#[inline]
fn default() -> Self {
Self::TRANSPARENT
}
}
impl std::fmt::Debug for ImColor32 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ImColor32")
.field("r", &self.r)
.field("g", &self.g)
.field("b", &self.b)
.field("a", &self.a)
.finish()
}
}
#[derive(Copy, Clone, Debug)]
#[repr(C, align(4))]
#[rustfmt::skip]
#[allow(missing_docs)]
pub struct ImColor32Fields {
#[cfg(target_endian = "little")] pub r: u8,
#[cfg(target_endian = "little")] pub g: u8,
#[cfg(target_endian = "little")] pub b: u8,
#[cfg(target_endian = "little")] pub a: u8,
#[cfg(target_endian = "big")] pub a: u8,
#[cfg(target_endian = "big")] pub b: u8,
#[cfg(target_endian = "big")] pub g: u8,
#[cfg(target_endian = "big")] pub r: u8,
}
#[cfg(any(
all(target_endian = "little", target_endian = "big"),
all(not(target_endian = "little"), not(target_endian = "big")),
))]
compile_error!("`cfg(target_endian = \"little\")` must be `cfg(not(target_endian = \"big\")`");
const _: [(); core::mem::size_of::<ImColor32>()] = [(); core::mem::size_of::<ImColor32Fields>()];
const _: [(); core::mem::align_of::<ImColor32>()] = [(); core::mem::align_of::<ImColor32Fields>()];
impl core::ops::Deref for ImColor32 {
type Target = ImColor32Fields;
#[inline]
fn deref(&self) -> &ImColor32Fields {
unsafe { &*(self as *const Self as *const ImColor32Fields) }
}
}
impl core::ops::DerefMut for ImColor32 {
#[inline]
fn deref_mut(&mut self) -> &mut ImColor32Fields {
unsafe { &mut *(self as *mut Self as *mut ImColor32Fields) }
}
}
impl From<ImColor32> for u32 {
#[inline]
fn from(color: ImColor32) -> Self {
color.0
}
}
impl From<u32> for ImColor32 {
#[inline]
fn from(color: u32) -> Self {
ImColor32(color)
}
}
impl From<MintVec4> for ImColor32 {
fn from(v: MintVec4) -> Self {
Self::from_rgba_f32s(v.x, v.y, v.z, v.w)
}
}
impl From<[f32; 4]> for ImColor32 {
#[inline]
fn from(v: [f32; 4]) -> Self {
Self::from_rgba_f32s(v[0], v[1], v[2], v[3])
}
}
impl From<(f32, f32, f32, f32)> for ImColor32 {
#[inline]
fn from(v: (f32, f32, f32, f32)) -> Self {
Self::from_rgba_f32s(v.0, v.1, v.2, v.3)
}
}
impl From<[f32; 3]> for ImColor32 {
#[inline]
fn from(v: [f32; 3]) -> Self {
Self::from_rgb_f32s(v[0], v[1], v[2])
}
}
impl From<(f32, f32, f32)> for ImColor32 {
fn from(v: (f32, f32, f32)) -> Self {
Self::from_rgb_f32s(v.0, v.1, v.2)
}
}
impl From<ImColor32> for [f32; 4] {
#[inline]
fn from(v: ImColor32) -> Self {
v.to_rgba_f32s()
}
}
impl From<ImColor32> for (f32, f32, f32, f32) {
#[inline]
fn from(color: ImColor32) -> Self {
let [r, g, b, a]: [f32; 4] = color.into();
(r, g, b, a)
}
}
#[inline]
pub(crate) fn saturate(v: f32) -> f32 {
if v > 0.0 {
if v <= 1.0 {
v
} else {
1.0
}
} else {
0.0
}
}
#[inline]
pub(crate) fn f32_to_u8_sat(f: f32) -> u8 {
let f = saturate(f) * 255.0 + 0.5;
unsafe { f.to_int_unchecked() }
}
#[inline]
pub(crate) fn u8_to_f32_sat(u: u8) -> f32 {
(u as f32) * (1.0 / 255.0)
}
#[test]
fn check_sat() {
assert_eq!(saturate(1.0), 1.0);
assert_eq!(saturate(0.5), 0.5);
assert_eq!(saturate(0.0), 0.0);
assert_eq!(saturate(-1.0), 0.0);
assert_eq!(saturate(1.0 + f32::EPSILON), 1.0);
assert_eq!(saturate(-f32::MIN_POSITIVE), 0.0);
assert_eq!(saturate(f32::NAN), 0.0);
assert_eq!(saturate(-f32::NAN), 0.0);
assert_eq!(saturate(-0.0).to_bits(), 0.0f32.to_bits());
}
#[test]
#[ignore]
fn test_f32_to_u8_sat_exhaustive() {
for f in (0..=u32::MAX).map(f32::from_bits) {
let v = saturate(f);
assert!(
(0.0..=1.0).contains(&v) && (v.to_bits() != (-0.0f32).to_bits()),
"sat({} [e.g. {:#x}]) => {} [e.g {:#x}]",
f,
f.to_bits(),
v,
v.to_bits(),
);
let sat = v * 255.0 + 0.5;
assert!(
sat.trunc() >= 0.0 && sat.trunc() <= (u8::MAX as f32) && sat.is_finite(),
"f32_to_u8_sat({} [e.g. {:#x}]) would be UB!",
f,
f.to_bits(),
);
}
}
#[test]
fn test_saturate_all_u8s() {
for u in 0..=u8::MAX {
let v = f32_to_u8_sat(u8_to_f32_sat(u));
assert_eq!(u, v);
}
}