use crate::color::{self, Alpha, IntoColor, Rgb, Rgba};
use crate::math::num_traits::{Float, One};
pub type DefaultScalar = f32;
pub type DefaultRgba = Rgba<DefaultScalar>;
pub trait IntoRgba<S>
where
S: Float,
{
fn into_rgba(self) -> Rgba<S>;
}
pub trait SetColor<S>: Sized
where
S: Float,
{
fn rgba_mut(&mut self) -> &mut Option<Rgba<S>>;
fn color<C>(mut self, color: C) -> Self
where
C: IntoRgba<S>,
{
*self.rgba_mut() = Some(color.into_rgba());
self
}
fn rgb(self, r: S, g: S, b: S) -> Self {
self.color(Rgb::new(r, g, b))
}
fn rgba(self, r: S, g: S, b: S, a: S) -> Self {
self.color(Rgba::new(r, g, b, a))
}
fn hsl(self, h: S, s: S, l: S) -> Self
where
S: Into<color::RgbHue<S>>,
{
let hue = h * S::from(360.0).unwrap();
self.color(color::Hsl::new(hue.into(), s, l))
}
fn hsla(self, h: S, s: S, l: S, a: S) -> Self
where
S: Into<color::RgbHue<S>>,
{
let hue = h * S::from(360.0).unwrap();
self.color(color::Hsla::new(hue.into(), s, l, a))
}
fn hsv(self, h: S, s: S, v: S) -> Self
where
S: Into<color::RgbHue<S>>,
{
let hue = h * S::from(360.0).unwrap();
self.color(color::Hsv::new(hue.into(), s, v))
}
fn hsva(self, h: S, s: S, v: S, a: S) -> Self
where
S: Into<color::RgbHue<S>>,
{
let hue = h * S::from(360.0).unwrap();
self.color(color::Hsva::new(hue.into(), s, v, a))
}
}
impl<S> SetColor<S> for Option<Rgba<S>>
where
S: Float,
{
fn rgba_mut(&mut self) -> &mut Option<Rgba<S>> {
self
}
}
fn into_rgb_with_alpha<C, S>(color: C) -> Rgba<S>
where
C: IntoColor<S>,
S: Float + One,
{
let color = color.into_rgb();
let alpha = One::one();
Alpha { color, alpha }
}
impl<S> IntoRgba<S> for color::Xyz<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Yxy<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Lab<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Lch<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Rgb<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Hsl<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Hsv<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Hwb<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<S> IntoRgba<S> for color::Luma<S>
where
S: Float + One,
{
fn into_rgba(self) -> Rgba<S> {
into_rgb_with_alpha(self)
}
}
impl<C, S> IntoRgba<S> for Alpha<C, S>
where
C: IntoColor<S>,
S: Float,
{
fn into_rgba(self) -> Rgba<S> {
let Alpha { color, alpha } = self;
let color = color.into_rgb();
Alpha { color, alpha }
}
}