palette/
angle.rs

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Traits for working with angular values, such as for in hues.

use crate::{
    bool_mask::HasBoolMask,
    num::{Real, Round},
};

#[cfg(feature = "wide")]
mod wide;

/// Represents types that can express half of a rotation (i.e. 180 degrees).
pub trait HalfRotation {
    /// Return a value that represents half of a rotation (i.e. 180 degrees).
    #[must_use]
    fn half_rotation() -> Self;
}

/// Represents types that can express a full rotation (i.e. 360 degrees).
pub trait FullRotation {
    /// Return a value that represents a full rotation (i.e. 360 degrees).
    #[must_use]
    fn full_rotation() -> Self;
}

/// Angle values that are real numbers and can represent both radians and
/// degrees.
pub trait RealAngle: Real {
    /// Consider `self` to be radians and convert it to degrees.
    #[must_use]
    fn radians_to_degrees(self) -> Self;

    /// Consider `self` to be degrees and convert it to radians.
    #[must_use]
    fn degrees_to_radians(self) -> Self;
}

/// Angular equality, where 0 degrees and 360 degrees are equal.
pub trait AngleEq: HasBoolMask {
    /// Check if `self` and `other` represent the same angle on a circle.
    #[must_use]
    fn angle_eq(&self, other: &Self) -> Self::Mask;
}

/// Angle types that can represent the full circle using positive and negative
/// values.
pub trait SignedAngle {
    /// Normalize `self` to a range corresponding to -180 to 180 degrees.
    #[must_use]
    fn normalize_signed_angle(self) -> Self;
}

/// Angle types that can represent the full circle as positive values.
pub trait UnsignedAngle {
    /// Normalize `self` to a range corresponding to 0 to 360 degrees.
    #[must_use]
    fn normalize_unsigned_angle(self) -> Self;
}

/// Performs value-to-value conversion between angle types. See also [`IntoAngle`].
pub trait FromAngle<T> {
    /// Performs a conversion from `angle`.
    fn from_angle(angle: T) -> Self;
}

impl<T> FromAngle<T> for T {
    #[inline]
    fn from_angle(angle: Self) -> Self {
        angle
    }
}

/// Performs value-to-value conversion between angle types. See also [`IntoAngle`].
pub trait IntoAngle<T> {
    /// Performs a conversion into `T`.
    fn into_angle(self) -> T;
}

impl<T, U> IntoAngle<U> for T
where
    U: FromAngle<T>,
{
    #[inline]
    fn into_angle(self) -> U {
        U::from_angle(self)
    }
}

macro_rules! impl_angle_float {
    ($($ty: ident),+) => {
        $(
            impl HalfRotation for $ty {
                #[inline]
                fn half_rotation() -> Self {
                    180.0
                }
            }

            impl FullRotation for $ty {
                #[inline]
                fn full_rotation() -> Self {
                    360.0
                }
            }

            impl RealAngle for $ty {
                #[inline]
                fn degrees_to_radians(self) -> Self {
                    self.to_radians()
                }

                #[inline]
                fn radians_to_degrees(self) -> Self {
                    self.to_degrees()
                }
            }

            impl AngleEq for $ty {
                #[inline]
                fn angle_eq(&self, other: &Self) -> bool {
                    self.normalize_unsigned_angle() == other.normalize_unsigned_angle()
                }
            }

            impl SignedAngle for $ty {
                #[inline]
                fn normalize_signed_angle(self) -> Self {
                    self - Round::ceil(((self + 180.0) / 360.0) - 1.0) * 360.0
                }
            }

            impl UnsignedAngle for $ty {
                #[inline]
                fn normalize_unsigned_angle(self) -> Self {
                    self - (Round::floor(self / 360.0) * 360.0)
                }
            }
        )+
    };
}

macro_rules! impl_from_angle_float {
    ($ty: ident to $other_ty: ident) => {
        impl FromAngle<$other_ty> for $ty {
            #[inline]
            fn from_angle(angle: $other_ty) -> Self {
                angle as $ty
            }
        }
    };
}

macro_rules! impl_from_angle_u8 {
    ($($float_ty: ident),*) => {
        $(
            impl FromAngle<u8> for $float_ty {
                #[inline]
                fn from_angle(angle: u8) -> Self {
                    (angle as $float_ty / 256.0) * Self::full_rotation()
                }
            }

            impl FromAngle<$float_ty> for u8 {
                #[inline]
                fn from_angle(angle: $float_ty) -> Self {
                    let normalized = angle.normalize_unsigned_angle() / $float_ty::full_rotation();
                    let rounded = (normalized * 256.0).round();

                    if rounded > 255.5 {
                        0
                    } else {
                        rounded as u8
                    }
                }
            }
        )*
    };
}

impl_angle_float!(f32, f64);
impl_from_angle_float!(f32 to f64);
impl_from_angle_float!(f64 to f32);
impl_from_angle_u8!(f32, f64);

impl HalfRotation for u8 {
    #[inline]
    fn half_rotation() -> Self {
        128
    }
}

impl AngleEq for u8 {
    #[inline]
    fn angle_eq(&self, other: &Self) -> bool {
        self == other
    }
}

impl UnsignedAngle for u8 {
    #[inline]
    fn normalize_unsigned_angle(self) -> Self {
        self
    }
}

#[cfg(test)]
mod test {
    use crate::RgbHue;

    #[test]
    fn f32_to_u8() {
        let hue_f32 = RgbHue::new(180.0f32);
        let hue_u8 = hue_f32.into_format::<u8>();
        assert_eq!(hue_u8, RgbHue::new(128u8));
    }

    #[test]
    fn u8_to_f32() {
        let hue_f32 = RgbHue::new(128u8);
        let hue_u8 = hue_f32.into_format::<f32>();
        assert_eq!(hue_u8, RgbHue::new(180.0f32));
    }
}