use core::{any::TypeId, marker::PhantomData};
#[cfg(feature = "random")]
use crate::hsv::UniformHsv;
use crate::{
angle::FromAngle,
bool_mask::{HasBoolMask, LazySelect, Select},
convert::FromColorUnclamped,
encoding::Srgb,
hues::RgbHueIter,
num::{Arithmetics, One, PartialCmp, Real},
rgb::{RgbSpace, RgbStandard},
stimulus::{FromStimulus, Stimulus},
Alpha, FromColor, Hsv, RgbHue, Xyz,
};
pub type Hwba<S = Srgb, T = f32> = Alpha<Hwb<S, T>, T>;
#[derive(Debug, ArrayCast, FromColorUnclamped, WithAlpha)]
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
#[palette(
palette_internal,
rgb_standard = "S",
component = "T",
skip_derives(Hsv, Hwb)
)]
#[repr(C)]
pub struct Hwb<S = Srgb, T = f32> {
#[palette(unsafe_same_layout_as = "T")]
pub hue: RgbHue<T>,
pub whiteness: T,
pub blackness: T,
#[cfg_attr(feature = "serializing", serde(skip))]
#[palette(unsafe_zero_sized)]
pub standard: PhantomData<S>,
}
impl<T> Hwb<Srgb, T> {
pub fn new_srgb<H: Into<RgbHue<T>>>(hue: H, whiteness: T, blackness: T) -> Self {
Self::new_const(hue.into(), whiteness, blackness)
}
pub const fn new_srgb_const(hue: RgbHue<T>, whiteness: T, blackness: T) -> Self {
Self::new_const(hue, whiteness, blackness)
}
}
impl<S, T> Hwb<S, T> {
pub fn new<H: Into<RgbHue<T>>>(hue: H, whiteness: T, blackness: T) -> Self {
Self::new_const(hue.into(), whiteness, blackness)
}
pub const fn new_const(hue: RgbHue<T>, whiteness: T, blackness: T) -> Self {
Hwb {
hue,
whiteness,
blackness,
standard: PhantomData,
}
}
pub fn into_format<U>(self) -> Hwb<S, U>
where
U: FromStimulus<T> + FromAngle<T>,
{
Hwb {
hue: self.hue.into_format(),
whiteness: U::from_stimulus(self.whiteness),
blackness: U::from_stimulus(self.blackness),
standard: PhantomData,
}
}
pub fn from_format<U>(color: Hwb<S, U>) -> Self
where
T: FromStimulus<U> + FromAngle<U>,
{
color.into_format()
}
pub fn into_components(self) -> (RgbHue<T>, T, T) {
(self.hue, self.whiteness, self.blackness)
}
pub fn from_components<H: Into<RgbHue<T>>>((hue, whiteness, blackness): (H, T, T)) -> Self {
Self::new(hue, whiteness, blackness)
}
#[inline]
fn reinterpret_as<St>(self) -> Hwb<St, T> {
Hwb {
hue: self.hue,
whiteness: self.whiteness,
blackness: self.blackness,
standard: PhantomData,
}
}
}
impl<S, T> Hwb<S, T>
where
T: Stimulus,
{
pub fn min_whiteness() -> T {
T::zero()
}
pub fn max_whiteness() -> T {
T::max_intensity()
}
pub fn min_blackness() -> T {
T::zero()
}
pub fn max_blackness() -> T {
T::max_intensity()
}
}
impl<T, A> Alpha<Hwb<Srgb, T>, A> {
pub fn new_srgb<H: Into<RgbHue<T>>>(hue: H, whiteness: T, blackness: T, alpha: A) -> Self {
Self::new_const(hue.into(), whiteness, blackness, alpha)
}
pub const fn new_srgb_const(hue: RgbHue<T>, whiteness: T, blackness: T, alpha: A) -> Self {
Self::new_const(hue, whiteness, blackness, alpha)
}
}
impl<S, T, A> Alpha<Hwb<S, T>, A> {
pub fn new<H: Into<RgbHue<T>>>(hue: H, whiteness: T, blackness: T, alpha: A) -> Self {
Self::new_const(hue.into(), whiteness, blackness, alpha)
}
pub const fn new_const(hue: RgbHue<T>, whiteness: T, blackness: T, alpha: A) -> Self {
Alpha {
color: Hwb::new_const(hue, whiteness, blackness),
alpha,
}
}
pub fn into_format<U, B>(self) -> Alpha<Hwb<S, U>, B>
where
U: FromStimulus<T> + FromAngle<T>,
B: FromStimulus<A>,
{
Alpha {
color: self.color.into_format(),
alpha: B::from_stimulus(self.alpha),
}
}
pub fn from_format<U, B>(color: Alpha<Hwb<S, U>, B>) -> Self
where
T: FromStimulus<U> + FromAngle<U>,
A: FromStimulus<B>,
{
color.into_format()
}
pub fn into_components(self) -> (RgbHue<T>, T, T, A) {
(
self.color.hue,
self.color.whiteness,
self.color.blackness,
self.alpha,
)
}
pub fn from_components<H: Into<RgbHue<T>>>(
(hue, whiteness, blackness, alpha): (H, T, T, A),
) -> Self {
Self::new(hue, whiteness, blackness, alpha)
}
}
impl_reference_component_methods_hue!(Hwb<S>, [whiteness, blackness], standard);
impl_struct_of_arrays_methods_hue!(Hwb<S>, [whiteness, blackness], standard);
impl<S1, S2, T> FromColorUnclamped<Hwb<S1, T>> for Hwb<S2, T>
where
S1: RgbStandard + 'static,
S2: RgbStandard + 'static,
S1::Space: RgbSpace<WhitePoint = <S2::Space as RgbSpace>::WhitePoint>,
Hsv<S1, T>: FromColorUnclamped<Hwb<S1, T>>,
Hsv<S2, T>: FromColorUnclamped<Hsv<S1, T>>,
Self: FromColorUnclamped<Hsv<S2, T>>,
{
#[inline]
fn from_color_unclamped(hwb: Hwb<S1, T>) -> Self {
if TypeId::of::<S1>() == TypeId::of::<S2>() {
hwb.reinterpret_as()
} else {
let hsv = Hsv::<S1, T>::from_color_unclamped(hwb);
let converted_hsv = Hsv::<S2, T>::from_color_unclamped(hsv);
Self::from_color_unclamped(converted_hsv)
}
}
}
impl<S, T> FromColorUnclamped<Hsv<S, T>> for Hwb<S, T>
where
T: One + Arithmetics,
{
#[inline]
fn from_color_unclamped(color: Hsv<S, T>) -> Self {
Hwb {
hue: color.hue,
whiteness: (T::one() - color.saturation) * &color.value,
blackness: (T::one() - color.value),
standard: PhantomData,
}
}
}
impl_tuple_conversion_hue!(Hwb<S> as (H, T, T), RgbHue);
impl_is_within_bounds_hwb!(Hwb<S> where T: Stimulus);
impl_clamp_hwb!(Hwb<S> phantom: standard where T: Stimulus);
impl_mix_hue!(Hwb<S> {whiteness, blackness} phantom: standard);
impl_lighten_hwb!(Hwb<S> phantom: standard where T: Stimulus);
impl_hue_ops!(Hwb<S>, RgbHue);
impl<S, T> HasBoolMask for Hwb<S, T>
where
T: HasBoolMask,
{
type Mask = T::Mask;
}
impl<S, T> Default for Hwb<S, T>
where
T: Stimulus,
RgbHue<T>: Default,
{
fn default() -> Hwb<S, T> {
Hwb::new(
RgbHue::default(),
Self::min_whiteness(),
Self::max_blackness(),
)
}
}
impl_color_add!(Hwb<S>, [hue, whiteness, blackness], standard);
impl_color_sub!(Hwb<S>, [hue, whiteness, blackness], standard);
impl_array_casts!(Hwb<S, T>, [T; 3]);
impl_simd_array_conversion_hue!(Hwb<S>, [whiteness, blackness], standard);
impl_struct_of_array_traits_hue!(Hwb<S>, RgbHueIter, [whiteness, blackness], standard);
impl_copy_clone!(Hwb<S>, [hue, whiteness, blackness], standard);
impl_eq_hue!(Hwb<S>, RgbHue, [hue, whiteness, blackness]);
#[allow(deprecated)]
impl<S, T> crate::RelativeContrast for Hwb<S, T>
where
T: Real + Arithmetics + PartialCmp,
T::Mask: LazySelect<T>,
S: RgbStandard,
Xyz<<S::Space as RgbSpace>::WhitePoint, T>: FromColor<Self>,
{
type Scalar = T;
#[inline]
fn get_contrast_ratio(self, other: Self) -> T {
let xyz1 = Xyz::from_color(self);
let xyz2 = Xyz::from_color(other);
crate::contrast_ratio(xyz1.y, xyz2.y)
}
}
impl_rand_traits_hwb_cone!(
UniformHwb,
Hwb<S>,
UniformHsv,
Hsv {
height: value,
radius: saturation
}
phantom: standard: PhantomData<S>
);
#[cfg(feature = "bytemuck")]
unsafe impl<S, T> bytemuck::Zeroable for Hwb<S, T> where T: bytemuck::Zeroable {}
#[cfg(feature = "bytemuck")]
unsafe impl<S: 'static, T> bytemuck::Pod for Hwb<S, T> where T: bytemuck::Pod {}
#[cfg(test)]
mod test {
use super::Hwb;
test_convert_into_from_xyz!(Hwb);
#[cfg(feature = "approx")]
mod conversion {
use crate::{FromColor, Hwb, Srgb};
#[test]
fn red() {
let a = Hwb::from_color(Srgb::new(1.0, 0.0, 0.0));
let b = Hwb::new_srgb(0.0, 0.0, 0.0);
assert_relative_eq!(a, b);
}
#[test]
fn orange() {
let a = Hwb::from_color(Srgb::new(1.0, 0.5, 0.0));
let b = Hwb::new_srgb(30.0, 0.0, 0.0);
assert_relative_eq!(a, b);
}
#[test]
fn green() {
let a = Hwb::from_color(Srgb::new(0.0, 1.0, 0.0));
let b = Hwb::new_srgb(120.0, 0.0, 0.0);
assert_relative_eq!(a, b);
}
#[test]
fn blue() {
let a = Hwb::from_color(Srgb::new(0.0, 0.0, 1.0));
let b = Hwb::new_srgb(240.0, 0.0, 0.0);
assert_relative_eq!(a, b);
}
#[test]
fn purple() {
let a = Hwb::from_color(Srgb::new(0.5, 0.0, 1.0));
let b = Hwb::new_srgb(270.0, 0.0, 0.0);
assert_relative_eq!(a, b);
}
}
#[cfg(feature = "approx")]
mod clamp {
use crate::{Clamp, Hwb};
#[test]
fn clamp_invalid() {
let expected = Hwb::new_srgb(240.0, 0.0, 0.0);
let clamped = Hwb::new_srgb(240.0, -3.0, -4.0).clamp();
assert_relative_eq!(expected, clamped);
}
#[test]
fn clamp_none() {
let expected = Hwb::new_srgb(240.0, 0.3, 0.7);
let clamped = Hwb::new_srgb(240.0, 0.3, 0.7).clamp();
assert_relative_eq!(expected, clamped);
}
#[test]
fn clamp_over_one() {
let expected = Hwb::new_srgb(240.0, 0.2, 0.8);
let clamped = Hwb::new_srgb(240.0, 5.0, 20.0).clamp();
assert_relative_eq!(expected, clamped);
}
#[test]
fn clamp_under_one() {
let expected = Hwb::new_srgb(240.0, 0.3, 0.1);
let clamped = Hwb::new_srgb(240.0, 0.3, 0.1).clamp();
assert_relative_eq!(expected, clamped);
}
}
raw_pixel_conversion_tests!(Hwb<crate::encoding::Srgb>: hue, whiteness, blackness);
raw_pixel_conversion_fail_tests!(Hwb<crate::encoding::Srgb>: hue, whiteness, blackness);
#[test]
fn check_min_max_components() {
use crate::encoding::Srgb;
assert_eq!(Hwb::<Srgb>::min_whiteness(), 0.0,);
assert_eq!(Hwb::<Srgb>::min_blackness(), 0.0,);
assert_eq!(Hwb::<Srgb>::max_whiteness(), 1.0,);
assert_eq!(Hwb::<Srgb>::max_blackness(), 1.0,);
}
struct_of_arrays_tests!(
Hwb<crate::encoding::Srgb>[hue, whiteness, blackness] phantom: standard,
super::Hwba::new(0.1f32, 0.2, 0.3, 0.4),
super::Hwba::new(0.2, 0.3, 0.4, 0.5),
super::Hwba::new(0.3, 0.4, 0.5, 0.6)
);
#[cfg(feature = "serializing")]
#[test]
fn serialize() {
let serialized = ::serde_json::to_string(&Hwb::new_srgb(0.3, 0.8, 0.1)).unwrap();
assert_eq!(serialized, r#"{"hue":0.3,"whiteness":0.8,"blackness":0.1}"#);
}
#[cfg(feature = "serializing")]
#[test]
fn deserialize() {
let deserialized: Hwb =
::serde_json::from_str(r#"{"hue":0.3,"whiteness":0.8,"blackness":0.1}"#).unwrap();
assert_eq!(deserialized, Hwb::new(0.3, 0.8, 0.1));
}
test_uniform_distribution! {
Hwb<crate::encoding::Srgb, f32> as crate::rgb::Rgb {
red: (0.0, 1.0),
green: (0.0, 1.0),
blue: (0.0, 1.0)
},
min: Hwb::new(0.0f32, 0.0, 0.0),
max: Hwb::new(360.0, 1.0, 1.0)
}
}