@@ -9,7 +9,7 @@ macro_rules! make_hues {
9
9
$( #[ $doc] ) +
10
10
///
11
11
///The hue is a circular type, where `0` and `360` is the same, and
12
- ///it's normalized to `(-180, + 180]` when it's converted to a linear
12
+ ///it's normalized to `(-180, 180]` when it's converted to a linear
13
13
///number (like `f32`). This makes many calculations easier, but may
14
14
///also have some surprising effects if it's expected to act as a
15
15
///linear number.
@@ -22,14 +22,24 @@ macro_rules! make_hues {
22
22
$name( radians * T :: from( 180.0 ) . unwrap( ) / T :: from( PI ) . unwrap( ) )
23
23
}
24
24
25
- ///Convert the hue to radians.
25
+ ///Get the hue as degrees, in the range `(-180, 180]`.
26
+ pub fn to_degrees( self ) -> T {
27
+ normalize_angle( self . 0 )
28
+ }
29
+
30
+ ///Convert the hue to radians, in the range `(-π, π]`.
26
31
pub fn to_radians( self ) -> T {
27
32
normalize_angle( self . 0 ) * T :: from( PI ) . unwrap( ) / T :: from( 180.0 ) . unwrap( )
28
33
}
29
34
30
- ///Returns the saved Hue value
31
- pub fn to_float( self ) -> T {
32
- normalize_angle( self . 0 )
35
+ ///Convert the hue to positive degrees, in the range `[0, 360)`.
36
+ pub fn to_positive_degrees( self ) -> T {
37
+ normalize_angle_positive( self . 0 )
38
+ }
39
+
40
+ ///Convert the hue to positive radians, in the range `[0, 2π)`.
41
+ pub fn to_positive_radians( self ) -> T {
42
+ normalize_angle_positive( self . 0 ) * T :: from( PI ) . unwrap( ) / T :: from( 180.0 ) . unwrap( )
33
43
}
34
44
}
35
45
@@ -41,13 +51,13 @@ macro_rules! make_hues {
41
51
42
52
impl Into <f64 > for $name<f64 > {
43
53
fn into( self ) -> f64 {
44
- normalize_angle( self . 0 ) as f64
54
+ normalize_angle( self . 0 )
45
55
}
46
56
}
47
57
48
58
impl Into <f32 > for $name<f32 > {
49
59
fn into( self ) -> f32 {
50
- normalize_angle( self . 0 ) as f32
60
+ normalize_angle( self . 0 )
51
61
}
52
62
}
53
63
impl Into <f32 > for $name<f64 > {
@@ -58,15 +68,15 @@ macro_rules! make_hues {
58
68
59
69
impl <T : Float > PartialEq for $name<T > {
60
70
fn eq( & self , other: & $name<T >) -> bool {
61
- let hue_s: T = ( * self ) . to_float ( ) ;
62
- let hue_o: T = ( * other) . to_float ( ) ;
71
+ let hue_s: T = ( * self ) . to_degrees ( ) ;
72
+ let hue_o: T = ( * other) . to_degrees ( ) ;
63
73
hue_s. eq( & hue_o)
64
74
}
65
75
}
66
76
67
77
impl <T : Float > PartialEq <T > for $name<T > {
68
78
fn eq( & self , other: & T ) -> bool {
69
- let hue: T = ( * self ) . to_float ( ) ;
79
+ let hue: T = ( * self ) . to_degrees ( ) ;
70
80
hue. eq( & normalize_angle( * other) )
71
81
}
72
82
}
@@ -121,12 +131,27 @@ make_hues! {
121
131
}
122
132
123
133
fn normalize_angle < T : Float > ( mut deg : T ) -> T {
124
- while deg > T :: from ( 180.0 ) . unwrap ( ) {
125
- deg = deg - T :: from ( 360.0 ) . unwrap ( ) ;
134
+ let c180 = T :: from ( 180.0 ) . unwrap ( ) ;
135
+ let c360 = T :: from ( 360.0 ) . unwrap ( ) ;
136
+ while deg > c180 {
137
+ deg = deg - c360;
138
+ }
139
+
140
+ while deg <= -c180 {
141
+ deg = deg + c360;
142
+ }
143
+
144
+ deg
145
+ }
146
+
147
+ fn normalize_angle_positive < T : Float > ( mut deg : T ) -> T {
148
+ let c360 = T :: from ( 360.0 ) . unwrap ( ) ;
149
+ while deg >= c360 {
150
+ deg = deg - c360;
126
151
}
127
152
128
- while deg <= - T :: from ( 180.0 ) . unwrap ( ) {
129
- deg = deg + T :: from ( 360.0 ) . unwrap ( ) ;
153
+ while deg < T :: zero ( ) {
154
+ deg = deg + c360 ;
130
155
}
131
156
132
157
deg
0 commit comments