Skip to content

Add an RGBA struct (without the CurrentColor variant.) #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ mod color_data;
pub type ColorFloat = c_float;


pub struct RGBA {
// All in 0..1
red: ColorFloat,
green: ColorFloat,
blue: ColorFloat,
alpha: ColorFloat,
}

pub enum Color {
CurrentColor,
RGBA(ColorFloat, ColorFloat, ColorFloat, ColorFloat), // 0..1
RGBA(RGBA),
}


Expand All @@ -38,7 +46,7 @@ impl Color {
fn parse_color_keyword(value: &str) -> Option<Color> {
let lower_value = value.to_ascii_lower();
match COLOR_KEYWORDS.bsearch_elem(&lower_value.as_slice()) {
Some(index) => Some(COLOR_VALUES[index]),
Some(index) => Some(RGBA(COLOR_VALUES[index])),
None => if "currentcolor" == lower_value { Some(CurrentColor) }
else { None }
}
Expand All @@ -60,8 +68,10 @@ fn parse_color_hash(value: &str) -> Option<Color> {
)
macro_rules! to_rgba(
($r: expr, $g: expr, $b: expr,) => {
Some(RGBA($r as ColorFloat / 255., $g as ColorFloat / 255.,
$b as ColorFloat / 255., 1.))
Some(RGBA(RGBA { red: $r as ColorFloat / 255.,
green: $g as ColorFloat / 255.,
blue: $b as ColorFloat / 255.,
alpha: 1. }))
};
)

Expand Down Expand Up @@ -170,5 +180,9 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
} else {
1.
};
if iter.next().is_none() { Some(RGBA(red, green, blue, alpha)) } else { None }
if iter.next().is_none() {
Some(RGBA(RGBA { red: red, green: green, blue: blue, alpha: alpha }))
} else {
None
}
}
10 changes: 5 additions & 5 deletions make_color_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,22 @@
// This file is generated by make_color_data.py
// This is to make sure that COLOR_KEYWORDS is sorted, to allow binary search.

use super::{Color, RGBA};
use super::RGBA;

macro_rules! RGBA(
macro_rules! rgba(
($r:expr, $g:expr, $b:expr, $a:expr) => {
RGBA($r / 255., $g / 255., $b / 255., $a)
RGBA { red: $r / 255., green: $g / 255., blue: $b / 255., alpha: $a }
};
)

pub static COLOR_KEYWORDS: &'static [&'static str] = &[
%s
];

pub static COLOR_VALUES: &'static [Color] = &[
pub static COLOR_VALUES: &'static [RGBA] = &[
%s
];
''' % (
'\n'.join(' "%s",' % keyword for keyword, _ in COLORS),
'\n'.join(' RGBA!(%s., %s., %s., %s.),' % rgba for _, rgba in COLORS),
'\n'.join(' rgba!(%s., %s., %s., %s.),' % rgba for _, rgba in COLORS),
))
5 changes: 3 additions & 2 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ fn color3_hsl() {
fn color3_keywords() {
do run_color_tests(include_str!("css-parsing-tests/color3_keywords.json")) |c| {
match c {
Some(RGBA(r, g, b, a)) => (~[r * 255., g * 255., b * 255., a]).to_json(),
Some(RGBA(RGBA { red: r, green: g, blue: b, alpha: a }))
=> (~[r * 255., g * 255., b * 255., a]).to_json(),
Some(CurrentColor) => json::String(~"currentColor"),
None => json::Null,
}
Expand Down Expand Up @@ -226,7 +227,7 @@ impl ToJson for SyntaxError {
impl ToJson for Color {
fn to_json(&self) -> json::Json {
match *self {
RGBA(r, g, b, a) => (~[r, g, b, a]).to_json(),
RGBA(RGBA { red: r, green: g, blue: b, alpha: a }) => (~[r, g, b, a]).to_json(),
CurrentColor => json::String(~"currentColor"),
}
}
Expand Down