Skip to content

Commit 38d1caa

Browse files
committed
Merge pull request servo#12 from SimonSapin/master
Add an RGBA struct (without the CurrentColor variant.)
2 parents 5424cfc + c3985bf commit 38d1caa

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

color.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ mod color_data;
1515
pub type ColorFloat = c_float;
1616

1717

18+
pub struct RGBA {
19+
// All in 0..1
20+
red: ColorFloat,
21+
green: ColorFloat,
22+
blue: ColorFloat,
23+
alpha: ColorFloat,
24+
}
25+
1826
pub enum Color {
1927
CurrentColor,
20-
RGBA(ColorFloat, ColorFloat, ColorFloat, ColorFloat), // 0..1
28+
RGBA(RGBA),
2129
}
2230

2331

@@ -38,7 +46,7 @@ impl Color {
3846
fn parse_color_keyword(value: &str) -> Option<Color> {
3947
let lower_value = value.to_ascii_lower();
4048
match COLOR_KEYWORDS.bsearch_elem(&lower_value.as_slice()) {
41-
Some(index) => Some(COLOR_VALUES[index]),
49+
Some(index) => Some(RGBA(COLOR_VALUES[index])),
4250
None => if "currentcolor" == lower_value { Some(CurrentColor) }
4351
else { None }
4452
}
@@ -60,8 +68,10 @@ fn parse_color_hash(value: &str) -> Option<Color> {
6068
)
6169
macro_rules! to_rgba(
6270
($r: expr, $g: expr, $b: expr,) => {
63-
Some(RGBA($r as ColorFloat / 255., $g as ColorFloat / 255.,
64-
$b as ColorFloat / 255., 1.))
71+
Some(RGBA(RGBA { red: $r as ColorFloat / 255.,
72+
green: $g as ColorFloat / 255.,
73+
blue: $b as ColorFloat / 255.,
74+
alpha: 1. }))
6575
};
6676
)
6777

@@ -170,5 +180,9 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
170180
} else {
171181
1.
172182
};
173-
if iter.next().is_none() { Some(RGBA(red, green, blue, alpha)) } else { None }
183+
if iter.next().is_none() {
184+
Some(RGBA(RGBA { red: red, green: green, blue: blue, alpha: alpha }))
185+
} else {
186+
None
187+
}
174188
}

make_color_data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,22 @@
175175
// This file is generated by make_color_data.py
176176
// This is to make sure that COLOR_KEYWORDS is sorted, to allow binary search.
177177
178-
use super::{Color, RGBA};
178+
use super::RGBA;
179179
180-
macro_rules! RGBA(
180+
macro_rules! rgba(
181181
($r:expr, $g:expr, $b:expr, $a:expr) => {
182-
RGBA($r / 255., $g / 255., $b / 255., $a)
182+
RGBA { red: $r / 255., green: $g / 255., blue: $b / 255., alpha: $a }
183183
};
184184
)
185185
186186
pub static COLOR_KEYWORDS: &'static [&'static str] = &[
187187
%s
188188
];
189189
190-
pub static COLOR_VALUES: &'static [Color] = &[
190+
pub static COLOR_VALUES: &'static [RGBA] = &[
191191
%s
192192
];
193193
''' % (
194194
'\n'.join(' "%s",' % keyword for keyword, _ in COLORS),
195-
'\n'.join(' RGBA!(%s., %s., %s., %s.),' % rgba for _, rgba in COLORS),
195+
'\n'.join(' rgba!(%s., %s., %s., %s.),' % rgba for _, rgba in COLORS),
196196
))

tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ fn color3_hsl() {
156156
fn color3_keywords() {
157157
do run_color_tests(include_str!("css-parsing-tests/color3_keywords.json")) |c| {
158158
match c {
159-
Some(RGBA(r, g, b, a)) => (~[r * 255., g * 255., b * 255., a]).to_json(),
159+
Some(RGBA(RGBA { red: r, green: g, blue: b, alpha: a }))
160+
=> (~[r * 255., g * 255., b * 255., a]).to_json(),
160161
Some(CurrentColor) => json::String(~"currentColor"),
161162
None => json::Null,
162163
}
@@ -226,7 +227,7 @@ impl ToJson for SyntaxError {
226227
impl ToJson for Color {
227228
fn to_json(&self) -> json::Json {
228229
match *self {
229-
RGBA(r, g, b, a) => (~[r, g, b, a]).to_json(),
230+
RGBA(RGBA { red: r, green: g, blue: b, alpha: a }) => (~[r, g, b, a]).to_json(),
230231
CurrentColor => json::String(~"currentColor"),
231232
}
232233
}

0 commit comments

Comments
 (0)