Skip to content

Commit eb44ab1

Browse files
committed
Auto merge of #35 - sidred:use_float_fn, r=Ogeon
use flt() function Use function flt() to convert constants to trait Float type. Changed all T::from().unwrap() to use this flt() function defined in lib.rs. Makes the code easier to read, especially the formula's. closes #33
2 parents 16f0071 + eb7b027 commit eb44ab1

16 files changed

+159
-160
lines changed

examples/color_scheme.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate palette;
22
extern crate image;
33
extern crate clap;
44

5-
use palette::{Color, Rgb, Hue, Shade};
5+
use palette::{Color, Hue, Shade};
66
use palette::pixel::Srgb;
77

88
use image::{RgbImage, GenericImage, SubImage};
@@ -156,7 +156,7 @@ fn blit_shades<I: GenericImage<Pixel=image::Rgb<u8>> + 'static>(color: Color, mu
156156
let height = canvas.height();
157157

158158
let primary = Srgb::linear_to_pixel(color);
159-
159+
160160
//Generate one lighter and two darker versions of the color
161161
let light = Srgb::linear_to_pixel(color.lighten(0.1));
162162
let dark1 = Srgb::linear_to_pixel(color.darken(0.1));

examples/readme_examples.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ extern crate palette;
33
extern crate num;
44

55
use image::{RgbImage, GenericImage};
6-
use num::traits::Float;
76

87
use palette::{Rgba, Gradient, Mix};
98
use palette::pixel::Srgb;
@@ -75,18 +74,18 @@ fn display_colors(filename: &str, colors: &[[u8; 3]]) {
7574
}
7675
}
7776

78-
fn display_gradients<T: Float, A: Mix<Scalar=T> + Clone, B: Mix<Scalar=T> + Clone>(filename: &str, grad1: Gradient<A>, grad2: Gradient<B>) where
79-
Rgba<T>: From<A>,
80-
Rgba<T>: From<B>,
77+
fn display_gradients<A: Mix<Scalar=f64> + Clone, B: Mix<Scalar=f64> + Clone>(filename: &str, grad1: Gradient<A>, grad2: Gradient<B>) where
78+
Rgba<f64>: From<A>,
79+
Rgba<f64>: From<B>,
8180
{
8281
let mut image = RgbImage::new(256, 64);
8382

8483
for (x, _, pixel) in image.sub_image(0, 0, 256, 32).pixels_mut() {
85-
pixel.data = Srgb::linear_to_pixel(grad1.get(T::from(x).unwrap() / T::from(255.0).unwrap()));
84+
pixel.data = Srgb::linear_to_pixel(grad1.get(x as f64 / 255.0));
8685
}
8786

8887
for (x, _, pixel) in image.sub_image(0, 32, 256, 32).pixels_mut() {
89-
pixel.data = Srgb::linear_to_pixel(grad2.get(T::from(x).unwrap() / T::from(255.0).unwrap()));
88+
pixel.data = Srgb::linear_to_pixel(grad2.get(x as f64/ 255.0));
9089
}
9190

9291
match image.save(filename) {

src/gradient.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use num::{Float, One, Zero, NumCast};
44
use std::cmp::max;
55

6+
use flt;
7+
68
use Mix;
79

810
///A linear interpolation between colors.
@@ -22,10 +24,10 @@ impl<C: Mix + Clone> Gradient<C> {
2224
pub fn new<I: IntoIterator<Item = C>>(colors: I) -> Gradient<C> {
2325
let mut points: Vec<_> = colors.into_iter().map(|c| (C::Scalar::zero(), c)).collect();
2426
assert!(points.len() > 0);
25-
let step_size = C::Scalar::one() / <C::Scalar as NumCast>::from(max(points.len() - 1, 1) as f64).unwrap();
27+
let step_size = C::Scalar::one() / flt(max(points.len() - 1, 1) as f64);
2628

2729
for (i, &mut (ref mut p, _)) in points.iter_mut().enumerate() {
28-
*p = <C::Scalar as NumCast>::from(i).unwrap() * step_size;
30+
*p = flt::<C::Scalar, _>(i) * step_size;
2931
}
3032

3133
Gradient(points)
@@ -125,7 +127,7 @@ impl<'a, C: Mix + Clone> Iterator for Take<'a, C> {
125127

126128
fn next(&mut self) -> Option<C> {
127129
if self.current < self.len {
128-
let i = self.from + <C::Scalar as NumCast>::from(self.current).unwrap() * (self.diff / <C::Scalar as NumCast>::from(self.len).unwrap());
130+
let i = self.from + (self.diff / flt(self.len)) * flt(self.current);
129131
self.current += 1;
130132
Some(self.gradient.get(i))
131133
} else {

src/hsl.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use num::traits::Float;
1+
use num::Float;
22

33
use std::ops::{Add, Sub};
44

5-
use {Color, Alpha, Rgb, Luma, Xyz, Yxy, Lab, Lch, Hsv, Limited, Mix, Shade, GetHue, Hue, Saturate, RgbHue, clamp};
5+
use {Color, Alpha, Rgb, Luma, Xyz, Yxy, Lab, Lch, Hsv, Limited, Mix, Shade, GetHue, Hue, Saturate, RgbHue, clamp, flt};
66

77
///Linear HSL with an alpha component. See the [`Hsla` implementation in `Alpha`](struct.Alpha.html#Hsla).
88
pub type Hsla<T = f32> = Alpha<Hsl<T>, T>;
@@ -218,22 +218,22 @@ impl<T: Float> From<Rgb<T>> for Hsl<T> {
218218
}
219219

220220
let diff = val_max - val_min;
221-
let lightness = (val_min + val_max) / T::from(2.0).unwrap();
221+
let lightness = (val_min + val_max) / flt(2.0);
222222

223223
let hue = if diff == T::zero() {
224224
T::zero()
225225
} else {
226-
T::from(60.0).unwrap() * match chan_max {
227-
Channel::Red => ((rgb.green - rgb.blue) / diff) % T::from(6.0).unwrap(),
228-
Channel::Green => ((rgb.blue - rgb.red) / diff + T::from(2.0).unwrap()),
229-
Channel::Blue => ((rgb.red - rgb.green) / diff + T::from(4.0).unwrap()),
226+
flt::<T,_>(60.0) * match chan_max {
227+
Channel::Red => ((rgb.green - rgb.blue) / diff) % flt(6.0),
228+
Channel::Green => ((rgb.blue - rgb.red) / diff + flt(2.0)),
229+
Channel::Blue => ((rgb.red - rgb.green) / diff + flt(4.0)),
230230
}
231231
};
232232

233233
let saturation = if diff == T::zero() {
234234
T::zero()
235235
} else {
236-
diff / (T::one() - (T::from(2.0).unwrap() * lightness - T::one()).abs())
236+
diff / (T::one() - ( lightness * flt(2.0) - T::one()).abs())
237237
};
238238

239239
Hsl {
@@ -276,19 +276,19 @@ impl<T: Float> From<Lch<T>> for Hsl<T> {
276276

277277
impl<T: Float> From<Hsv<T>> for Hsl<T> {
278278
fn from(hsv: Hsv<T>) -> Hsl<T> {
279-
let x = (T::from(2.0).unwrap() - hsv.saturation) * hsv.value;
279+
let x = (flt::<T,_>(2.0) - hsv.saturation) * hsv.value;
280280
let saturation = if hsv.value == T::zero() {
281281
T::zero()
282282
} else if x < T::one() {
283283
hsv.saturation * hsv.value / x
284284
} else {
285-
hsv.saturation * hsv.value / (T::from(2.0).unwrap() - x)
285+
hsv.saturation * hsv.value / (flt::<T,_>(2.0) - x)
286286
};
287287

288288
Hsl {
289289
hue: hsv.hue,
290290
saturation: saturation,
291-
lightness: x / T::from(2.0).unwrap(),
291+
lightness: x / flt(2.0),
292292
}
293293
}
294294
}

src/hsv.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use num::traits::Float;
1+
use num::Float;
22

33
use std::ops::{Add, Sub};
44

5-
use {Color, Alpha, Rgb, Luma, Xyz, Yxy, Lab, Lch, Hsl, Limited, Mix, Shade, GetHue, Hue, Saturate, RgbHue, clamp};
5+
use {Color, Alpha, Rgb, Luma, Xyz, Yxy, Lab, Lch, Hsl, Limited, Mix, Shade, GetHue, Hue, Saturate, RgbHue, clamp, flt};
66

77
///Linear HSV with an alpha component. See the [`Hsva` implementation in `Alpha`](struct.Alpha.html#Hsva).
88
pub type Hsva<T = f32> = Alpha<Hsv<T>, T>;
@@ -222,10 +222,10 @@ impl<T: Float> From<Rgb<T>> for Hsv<T> {
222222
let hue = if diff == T::zero() {
223223
T::zero()
224224
} else {
225-
T::from(60.0).unwrap() * match chan_max {
226-
Channel::Red => ((rgb.green - rgb.blue) / diff) % T::from(6.0).unwrap(),
227-
Channel::Green => ((rgb.blue - rgb.red) / diff + T::from(2.0).unwrap()),
228-
Channel::Blue => ((rgb.red - rgb.green) / diff + T::from(4.0).unwrap()),
225+
flt::<T,_>(60.0) * match chan_max {
226+
Channel::Red => ((rgb.green - rgb.blue) / diff) % flt(6.0),
227+
Channel::Green => ((rgb.blue - rgb.red) / diff + flt(2.0)),
228+
Channel::Blue => ((rgb.red - rgb.green) / diff + flt(4.0)),
229229
}
230230
};
231231

@@ -275,15 +275,15 @@ impl<T: Float> From<Lch<T>> for Hsv<T> {
275275

276276
impl<T: Float> From<Hsl<T>> for Hsv<T> {
277277
fn from(hsl: Hsl<T>) -> Hsv<T> {
278-
let x = hsl.saturation * if hsl.lightness < T::from(0.5).unwrap() {
278+
let x = hsl.saturation * if hsl.lightness < flt(0.5) {
279279
hsl.lightness
280280
} else {
281281
T::one() - hsl.lightness
282282
};
283283

284284
Hsv {
285285
hue: hsl.hue,
286-
saturation: T::from(2.0).unwrap() * x / (hsl.lightness + x),
286+
saturation: x * flt(2.0) / (hsl.lightness + x),
287287
value: hsl.lightness + x,
288288
}
289289
}

src/hues.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use num::traits::Float;
1+
use num::Float;
22

33
use std::f64::consts::PI;
44
use std::cmp::PartialEq;
55
use std::ops::{Add, Sub};
66

7+
use flt;
8+
79
macro_rules! make_hues {
810
($($(#[$doc:meta])+ struct $name:ident;)+) => ($(
911
$(#[$doc])+
@@ -19,7 +21,7 @@ macro_rules! make_hues {
1921
impl<T:Float> $name<T> {
2022
///Create a new hue from radians, instead of degrees.
2123
pub fn from_radians(radians: T) -> $name<T> {
22-
$name(radians * T::from(180.0).unwrap() / T::from(PI).unwrap())
24+
$name(radians * flt(180.0) / flt(PI))
2325
}
2426

2527
///Get the hue as degrees, in the range `(-180, 180]`.
@@ -29,7 +31,7 @@ macro_rules! make_hues {
2931

3032
///Convert the hue to radians, in the range `(-π, π]`.
3133
pub fn to_radians(self) -> T {
32-
normalize_angle(self.0) * T::from(PI).unwrap() / T::from(180.0).unwrap()
34+
normalize_angle(self.0) * flt(PI) / flt(180.0)
3335
}
3436

3537
///Convert the hue to positive degrees, in the range `[0, 360)`.
@@ -39,7 +41,7 @@ macro_rules! make_hues {
3941

4042
///Convert the hue to positive radians, in the range `[0, 2π)`.
4143
pub fn to_positive_radians(self) -> T {
42-
normalize_angle_positive(self.0) * T::from(PI).unwrap() / T::from(180.0).unwrap()
44+
normalize_angle_positive(self.0) * flt(PI) / flt(180.0)
4345
}
4446
}
4547

@@ -131,8 +133,8 @@ make_hues! {
131133
}
132134

133135
fn normalize_angle<T: Float>(mut deg: T) -> T {
134-
let c180 = T::from(180.0).unwrap();
135-
let c360 = T::from(360.0).unwrap();
136+
let c180 = flt(180.0);
137+
let c360 = flt(360.0);
136138
while deg > c180 {
137139
deg = deg - c360;
138140
}
@@ -145,7 +147,7 @@ fn normalize_angle<T: Float>(mut deg: T) -> T {
145147
}
146148

147149
fn normalize_angle_positive<T: Float>(mut deg: T) -> T {
148-
let c360 = T::from(360.0).unwrap();
150+
let c360 = flt(360.0);
149151
while deg >= c360 {
150152
deg = deg - c360;
151153
}

src/lab.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use num::traits::Float;
1+
use num::Float;
22

33
use std::ops::{Add, Sub, Mul, Div};
44

5-
use {Color, Alpha, Rgb, Luma, Xyz, Yxy, Lch, Hsv, Hsl, Limited, Mix, Shade, GetHue, LabHue, clamp};
5+
use {Color, Alpha, Rgb, Luma, Xyz, Yxy, Lch, Hsv, Hsl, Limited, Mix, Shade, GetHue, LabHue, clamp, flt};
66

77
use tristimulus::{X_N, Y_N, Z_N};
88

@@ -222,14 +222,9 @@ alpha_from!(Lab {Rgb, Xyz, Yxy, Luma, Lch, Hsv, Hsl, Color});
222222
impl<T: Float> From<Xyz<T>> for Lab<T> {
223223
fn from(xyz: Xyz<T>) -> Lab<T> {
224224
Lab {
225-
l: (T::from(116.0).unwrap() * f(xyz.y / T::from(Y_N).unwrap()) -
226-
T::from(16.0).unwrap()) / T::from(100.0).unwrap(),
227-
a: (T::from(500.0).unwrap() *
228-
(f(xyz.x / T::from(X_N).unwrap()) - f(xyz.y / T::from(Y_N).unwrap()))) /
229-
T::from(128.0).unwrap(),
230-
b: (T::from(200.0).unwrap() *
231-
(f(xyz.y / T::from(Y_N).unwrap()) - f(xyz.z / T::from(Z_N).unwrap()))) /
232-
T::from(128.0).unwrap(),
225+
l: (f(xyz.y / flt(Y_N)) * flt(116.0) - flt(16.0)) / flt(100.0),
226+
a: (f(xyz.x / flt(X_N)) - f(xyz.y / flt(Y_N))) * flt(500.0) / flt(128.0),
227+
b: (f(xyz.y / flt(Y_N)) - f(xyz.z / flt(Z_N))) * flt(200.0) / flt(128.0),
233228
}
234229
}
235230
}
@@ -276,15 +271,14 @@ impl<T: Float> From<Hsl<T>> for Lab<T> {
276271

277272
fn f<T: Float>(t: T) -> T {
278273
//(6/29)^3
279-
let c_6_o_29_p_3: T = T::from(0.00885645167).unwrap();
274+
let c_6_o_29_p_3: T = flt(0.00885645167);
280275
//(29/6)^2
281-
let c_29_o_6_p_2: T = T::from(23.3611111111).unwrap();
276+
let c_29_o_6_p_2: T = flt(23.3611111111);
282277

283278
if t > c_6_o_29_p_3 {
284-
t.powf(T::one() / T::from(3.0).unwrap())
279+
t.powf(T::one() / flt(3.0))
285280
} else {
286-
(T::one() / T::from(3.0).unwrap()) * c_29_o_6_p_2 * t +
287-
(T::from(4.0).unwrap() / T::from(29.0).unwrap())
281+
(T::one() / flt(3.0)) * c_29_o_6_p_2 * t + (flt::<T,_>(4.0) / flt(29.0))
288282
}
289283
}
290284

src/lch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use num::traits::Float;
1+
use num::Float;
22

33
use std::ops::{Add, Sub};
44

src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
extern crate approx;
4949
extern crate num;
5050

51-
use num::traits::Float;
51+
use num::{Float, ToPrimitive, NumCast};
5252

5353
use pixel::{Srgb, GammaRgb};
5454

@@ -597,3 +597,8 @@ pub trait Saturate: Sized {
597597
self.saturate(-factor)
598598
}
599599
}
600+
601+
///A convenience function to convert a constant number to Float Type
602+
fn flt<T: num::Float, P: ToPrimitive>(prim: P) -> T {
603+
NumCast::from(prim).unwrap()
604+
}

src/luma.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use num::traits::Float;
1+
use num::Float;
22

33
use std::ops::{Add, Sub, Mul, Div};
44

5-
use {Color, Alpha, Rgb, Xyz, Yxy, Lab, Lch, Hsv, Hsl, Limited, Mix, Shade, clamp};
5+
use {Color, Alpha, Rgb, Xyz, Yxy, Lab, Lch, Hsv, Hsl, Limited, Mix, Shade, clamp, flt};
66

77
///Linear luminance with an alpha component. See the [`Lumaa` implementation in `Alpha`](struct.Alpha.html#Lumaa).
88
pub type Lumaa<T = f32> = Alpha<Luma<T>, T>;
@@ -31,7 +31,7 @@ impl<T: Float> Luma<T> {
3131
///Linear luminance from an 8 bit value.
3232
pub fn new_u8(luma: u8) -> Luma<T> {
3333
Luma {
34-
luma: T::from(luma).unwrap() / T::from(255.0).unwrap(),
34+
luma: flt::<T,_>(luma) / flt(255.0),
3535
}
3636
}
3737
}
@@ -50,7 +50,7 @@ impl<T: Float> Alpha<Luma<T>, T> {
5050
pub fn new_u8(luma: u8, alpha: u8) -> Lumaa<T> {
5151
Alpha {
5252
color: Luma::new_u8(luma),
53-
alpha: T::from(alpha).unwrap() / T::from(255.0).unwrap(),
53+
alpha: flt::<T,_>(alpha) / flt(255.0),
5454
}
5555
}
5656
}
@@ -186,7 +186,7 @@ alpha_from!(Luma {Rgb, Xyz, Yxy, Lab, Lch, Hsv, Hsl, Color});
186186
impl<T: Float> From<Rgb<T>> for Luma<T> {
187187
fn from(rgb: Rgb<T>) -> Luma<T> {
188188
Luma {
189-
luma: rgb.red * T::from(0.2126).unwrap() + rgb.green * T::from(0.7152).unwrap() + rgb.blue * T::from(0.0722).unwrap(),
189+
luma: rgb.red * flt(0.2126) + rgb.green * flt(0.7152) + rgb.blue * flt(0.0722),
190190
}
191191
}
192192
}

src/pixel/gamma_rgb.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use num::Float;
22

3-
use {Alpha, Rgb, Rgba, clamp};
3+
use {Alpha, Rgb, Rgba, clamp, flt};
44

55
use pixel::RgbPixel;
66

@@ -71,10 +71,10 @@ impl<T: Float> GammaRgb<T> {
7171
///Create a new gamma encoded color, with transparency, from `u8` values.
7272
pub fn with_alpha_u8(red: u8, green: u8, blue: u8, alpha: u8, gamma: T) -> GammaRgb<T> {
7373
GammaRgb {
74-
red: T::from(red).unwrap() / T::from(255.0).unwrap(),
75-
green: T::from(green).unwrap() / T::from(255.0).unwrap(),
76-
blue: T::from(blue).unwrap() / T::from(255.0).unwrap(),
77-
alpha: T::from(alpha).unwrap() / T::from(255.0).unwrap(),
74+
red: flt::<T,_>(red) / flt(255.0),
75+
green: flt::<T,_>(green) / flt(255.0),
76+
blue: flt::<T,_>(blue) / flt(255.0),
77+
alpha: flt::<T,_>(alpha) / flt(255.0),
7878
gamma: gamma,
7979
}
8080
}

0 commit comments

Comments
 (0)