Skip to content

Commit 7c58a70

Browse files
committed
Tweak float types
1 parent a5ee51d commit 7c58a70

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
pub struct NumericValue {
88
representation: ~str,
99
value: f64,
10-
int_value: Option<i32>,
10+
int_value: Option<i64>,
1111
}
1212

1313

color.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use std::libc::c_float;
12
use ast::*;
23
use super::to_ascii_lower;
34
use self::color_data::{COLOR_KEYWORDS, COLOR_VALUES};
45

56
mod color_data;
67

7-
pub type ColorFloat = f64;
8+
9+
// Try to match rust-azure’s AzFloat
10+
pub type ColorFloat = c_float;
811

912

1013
pub enum Color {
@@ -115,31 +118,31 @@ fn parse_color_function(name: &str, arguments: &[(ComponentValue, SourceLocation
115118
// Either integers or percentages, but all the same type.
116119
match iter.next() {
117120
Some(&Number(ref v)) if v.int_value.is_some() => {
118-
red = v.value / 255.;
121+
red = (v.value as ColorFloat / 255.) as ColorFloat;
119122
expect_comma!();
120-
green = expect_integer!() / 255.;
123+
green = (expect_integer!() / 255.) as ColorFloat;
121124
expect_comma!();
122-
blue = expect_integer!() / 255.;
125+
blue = (expect_integer!() / 255.) as ColorFloat;
123126
}
124127
Some(&Percentage(ref v)) => {
125-
red = v.value / 100.;
128+
red = (v.value as ColorFloat / 100.) as ColorFloat;
126129
expect_comma!();
127-
green = expect_percentage!() / 100.;
130+
green = (expect_percentage!() / 100.) as ColorFloat;
128131
expect_comma!();
129-
blue = expect_percentage!() / 100.;
132+
blue = (expect_percentage!() / 100.) as ColorFloat;
130133
}
131134
_ => return None
132135
};
133136
} else {
134-
let hue: ColorFloat = expect_number!() / 360.;
137+
let hue = expect_number!() / 360.;
135138
let hue = hue - hue.floor();
136139
expect_comma!();
137-
let saturation: ColorFloat = (expect_percentage!() / 100.).max(&0.).min(&1.);
140+
let saturation = (expect_percentage!() / 100.).max(&0.).min(&1.);
138141
expect_comma!();
139-
let lightness: ColorFloat = (expect_percentage!() / 100.).max(&0.).min(&1.);
142+
let lightness = (expect_percentage!() / 100.).max(&0.).min(&1.);
140143

141144
// http://www.w3.org/TR/css3-color/#hsl-color
142-
fn hue_to_rgb(m1: ColorFloat, m2: ColorFloat, mut h: ColorFloat) -> ColorFloat {
145+
fn hue_to_rgb(m1: f64, m2: f64, mut h: f64) -> f64 {
143146
if h < 0. { h += 1. }
144147
if h > 1. { h -= 1. }
145148

@@ -151,17 +154,14 @@ fn parse_color_function(name: &str, arguments: &[(ComponentValue, SourceLocation
151154
let m2 = if lightness <= 0.5 { lightness * (saturation + 1.) }
152155
else { lightness + saturation - lightness * saturation };
153156
let m1 = lightness * 2. - m2;
154-
red = hue_to_rgb(m1, m2, hue + 1. / 3.);
155-
green = hue_to_rgb(m1, m2, hue);
156-
blue = hue_to_rgb(m1, m2, hue - 1. / 3.);
157+
red = hue_to_rgb(m1, m2, hue + 1. / 3.) as ColorFloat;
158+
green = hue_to_rgb(m1, m2, hue) as ColorFloat;
159+
blue = hue_to_rgb(m1, m2, hue - 1. / 3.) as ColorFloat;
157160
}
158161

159162
let alpha = if has_alpha {
160163
expect_comma!();
161-
match iter.next() {
162-
Some(&Number(ref a)) => a.value.max(&0.).min(&1.),
163-
_ => return None
164-
}
164+
(expect_number!()).max(&0.).min(&1.) as ColorFloat
165165
} else {
166166
1.
167167
};

tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn write_whole_file(path: &Path, data: &str) {
2222

2323
fn almost_equals(a: &json::Json, b: &json::Json) -> bool {
2424
match (a, b) {
25-
(&json::Number(a), &json::Number(b)) => (a - b).abs() < 1e-10,
25+
(&json::Number(a), &json::Number(b)) => (a - b).abs() < 1e-6,
2626
(&json::String(ref a), &json::String(ref b)) => a == b,
2727
(&json::Boolean(a), &json::Boolean(b)) => a == b,
2828
(&json::List(ref a), &json::List(ref b))

tokenizer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// http://dev.w3.org/csswg/css3-syntax/#tokenization
66

7-
use std::{str, u32, i32, f64};
7+
use std::{str, u32, i64, f64};
88

99
use ast::*;
1010
use super::eq_ascii_lower;
@@ -426,9 +426,9 @@ fn consume_numeric(parser: &mut Parser) -> ComponentValue {
426426
int_value: if is_integer { Some(
427427
// Remove any + sign as int::from_str() does not parse them.
428428
if representation[0] != '+' as u8 {
429-
i32::from_str(representation)
429+
i64::from_str(representation)
430430
} else {
431-
i32::from_str(representation.slice_from(1))
431+
i64::from_str(representation.slice_from(1))
432432
}.get()
433433
)} else { None },
434434
value: f64::from_str(representation).get(),

0 commit comments

Comments
 (0)