1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! This module provides some more flexible conversions and aims to fill the gaps within the `From`
//! and `Into` implementations provided by the `palette` library.
//!
//! If a desired conversion is missing, feel free to open an issue or pull request!

use crate::color::white_point::D65;
use crate::color::{self, encoding, Alpha, Component, IntoColor, LinSrgba};
use crate::math::num_traits::Float;

/// Types that may be converted directly into a linear sRGBA color representation.
///
/// This is more flexible than `Into<LinSrgba<S>>` as it also supports converting from different
/// sRGBA encodings that also have different component types.
///
/// This trait is important for nannou as the `Draw` API works with the `LinSrgba` type as a target
/// for its generic colour type API.
pub trait IntoLinSrgba<S>
where
    S: Component,
{
    /// Convert self into RGBA.
    fn into_lin_srgba(self) -> LinSrgba<S>;
}

impl<S> IntoLinSrgba<S> for color::Xyz<D65, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<S> IntoLinSrgba<S> for color::Yxy<D65, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<S> IntoLinSrgba<S> for color::Lab<D65, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<S> IntoLinSrgba<S> for color::Lch<D65, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<T, S> IntoLinSrgba<S> for color::LinSrgb<T>
where
    T: Component,
    S: Component,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        let color = self.into_format();
        let alpha = S::max_intensity();
        Alpha { color, alpha }
    }
}

impl<T, S> IntoLinSrgba<S> for color::Srgb<T>
where
    T: Component,
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        let color = self.into_format().into_linear();
        let alpha = S::max_intensity();
        Alpha { color, alpha }
    }
}

impl<S> IntoLinSrgba<S> for color::Hsl<encoding::Srgb, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<S> IntoLinSrgba<S> for color::Hsv<encoding::Srgb, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<S> IntoLinSrgba<S> for color::Hwb<encoding::Srgb, S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<S> IntoLinSrgba<S> for color::SrgbLuma<S>
where
    S: Component + Float,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        into_lin_srgb_with_alpha(self)
    }
}

impl<C, S, T> IntoLinSrgba<S> for Alpha<C, T>
where
    C: IntoLinSrgba<S>,
    S: Component,
    T: Component,
{
    fn into_lin_srgba(self) -> LinSrgba<S> {
        let Alpha { color, alpha } = self;
        let mut srgba = color.into_lin_srgba();
        srgba.alpha = alpha.convert();
        srgba
    }
}

fn into_lin_srgb_with_alpha<C, S>(color: C) -> LinSrgba<S>
where
    C: IntoColor<D65, S>,
    S: Component + Float,
{
    let color: color::LinSrgb<S> = color.into_rgb::<encoding::Srgb>();
    let alpha = S::max_intensity();
    Alpha { color, alpha }
}