Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 9549be4

Browse files
committed
Add parse_static_color
1 parent 6135889 commit 9549be4

File tree

1 file changed

+88
-93
lines changed

1 file changed

+88
-93
lines changed

color.rs

Lines changed: 88 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -86,40 +86,9 @@ impl Color {
8686
}
8787
8888
pub mod parsing {
89+
use super::*;
8990
use super::{Color, rgb, rgba, hsl, hsla};
90-
use super::css_colors::{black, silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, aqua};
9191
92-
fn fail_unrecognized(col : &str) -> Option<Color> {
93-
warn!("Unrecognized color %s", col);
94-
return None;
95-
}
96-
97-
/** Match an exact color keyword. */
98-
fn parse_by_name(color : &str) -> Option<Color> {
99-
let col = match color.to_ascii().to_lower().to_str_ascii() {
100-
~"black" => black(),
101-
~"silver" => silver(),
102-
~"gray" => gray(),
103-
~"grey" => gray(),
104-
~"white" => white(),
105-
~"maroon" => maroon(),
106-
~"red" => red(),
107-
~"purple" => purple(),
108-
~"fuchsia" => fuchsia(),
109-
~"green" => green(),
110-
~"lime" => lime(),
111-
~"olive" => olive(),
112-
~"yellow" => yellow(),
113-
~"navy" => navy(),
114-
~"blue" => blue(),
115-
~"teal" => teal(),
116-
~"aqua" => aqua(),
117-
_ => return fail_unrecognized(color)
118-
};
119-
120-
return Some(col);
121-
}
122-
12392
/** Parses a color specification in the form rgb(foo,bar,baz) */
12493
fn parse_rgb(color : &str) -> Option<Color> {
12594
// Shave off the rgb( and the )
@@ -213,6 +182,51 @@ pub mod parsing {
213182
}
214183
}
215184
185+
pub fn fail_unrecognized(col : &str) -> Option<Color> {
186+
warn!("Unrecognized color %s", col);
187+
return None;
188+
}
189+
190+
pub fn parse_by_name(name : &str) -> Option<Color> {
191+
let color = parse_static_color!(name,
192+
ALICEBLUE, ANTIQUEWHITE, AQUA, AQUAMARINE, AZURE,
193+
BEIGE, BISQUE, BLACK, BLANCHEDALMOND, BLUE,
194+
BLUEVIOLET, BROWN, BURLYWOOD, CADETBLUE, CHARTREUSE,
195+
CHOCOLATE, CORAL, CORNFLOWERBLUE, CORNSILK, CRIMSON,
196+
CYAN, DARKBLUE, DARKCYAN, DARKGOLDENROD, DARKGRAY,
197+
DARKGREEN, DARKGREY, DARKKHAKI, DARKMAGENTA, DARKOLIVEGREEN,
198+
DARKORANGE, DARKORCHID, DARKRED, DARKSALMON, DARKSEAGREEN,
199+
DARKSLATEBLUE, DARKSLATEGRAY, DARKSLATEGREY, DARKTURQUOISE, DARKVIOLET,
200+
DEEPPINK, DEEPSKYBLUE, DIMGRAY, DIMGREY, DODGERBLUE,
201+
FIREBRICK, FLORALWHITE, FORESTGREEN, FUCHSIA, GAINSBORO,
202+
GHOSTWHITE, GOLD, GOLDENROD, GRAY, GREY,
203+
GREEN, GREENYELLOW, HONEYDEW, HOTPINK, INDIANRED,
204+
INDIGO, IVORY, KHAKI, LAVENDER, LAVENDERBLUSH,
205+
LAWNGREEN, LEMONCHIFFON, LIGHTBLUE, LIGHTCORAL, LIGHTCYAN,
206+
LIGHTGOLDENRODYELLOW, LIGHTGRAY, LIGHTGREEN, LIGHTGREY, LIGHTPINK,
207+
LIGHTSALMON, LIGHTSEAGREEN, LIGHTSKYBLUE, LIGHTSLATEGRAY, LIGHTSLATEGREY,
208+
LIGHTSTEELBLUE, LIGHTYELLOW, LIME, LIMEGREEN, LINEN,
209+
MAGENTA, MAROON, MEDIUMAQUAMARINE, MEDIUMBLUE, MEDIUMORCHID,
210+
MEDIUMPURPLE, MEDIUMSEAGREEN, MEDIUMSLATEBLUE, MEDIUMSPRINGGREEN, MEDIUMTURQUOISE,
211+
MEDIUMVIOLETRED, MIDNIGHTBLUE, MINTCREAM, MISTYROSE, MOCCASIN,
212+
NAVAJOWHITE, NAVY, OLDLACE, OLIVE, OLIVEDRAB,
213+
ORANGE, ORANGERED, ORCHID, PALEGOLDENROD, PALEGREEN,
214+
PALETURQUOISE, PALEVIOLETRED, PAPAYAWHIP, PEACHPUFF, PERU,
215+
PINK, PLUM, POWDERBLUE, PURPLE, RED,
216+
ROSYBROWN, ROYALBLUE, SADDLEBROWN, SALMON, SANDYBROWN,
217+
SEAGREEN, SEASHELL, SIENNA, SILVER, SKYBLUE,
218+
SLATEBLUE, SLATEGRAY, SLATEGREY, SNOW, SPRINGGREEN,
219+
STEELBLUE, TAN, TEAL, THISTLE, TOMATO,
220+
TURQUOISE, VIOLET, WHEAT, WHITE, WHITESMOKE,
221+
YELLOW, YELLOWGREEN);
222+
223+
if color.is_none() {
224+
return fail_unrecognized(name);
225+
}else {
226+
return color;
227+
}
228+
}
229+
216230
#[cfg(test)]
217231
mod test {
218232
use super::{rgb, rgba};
@@ -276,66 +290,6 @@ mod test {
276290
}
277291
}
278292
279-
280-
/** Define the colors specified by css */
281-
pub mod css_colors {
282-
use super::Color;
283-
284-
// The 16 basic css colors
285-
pub fn black() -> Color {
286-
Color {red : 0u8, green : 0u8, blue : 0u8, alpha : 1.0}
287-
}
288-
pub fn silver() -> Color {
289-
Color {red : 192u8, green : 192u8, blue : 192u8, alpha : 1.0}
290-
}
291-
pub fn gray() -> Color {
292-
Color {red : 128u8, green : 128u8, blue : 128u8, alpha : 1.0}
293-
}
294-
pub fn white() -> Color {
295-
Color {red : 255u8, green : 255u8, blue : 255u8, alpha : 1.0}
296-
}
297-
pub fn maroon() -> Color {
298-
Color {red : 128u8, green : 0u8, blue : 0u8, alpha : 1.0}
299-
}
300-
pub fn red() -> Color {
301-
Color {red : 255u8, green : 0u8, blue : 0u8, alpha : 1.0}
302-
}
303-
pub fn purple() -> Color {
304-
Color {red : 128u8, green : 0u8, blue : 128u8, alpha : 1.0}
305-
}
306-
pub fn fuchsia() -> Color {
307-
Color {red : 255u8, green : 0u8, blue : 255u8, alpha : 1.0}
308-
}
309-
pub fn green() -> Color {
310-
Color {red : 0u8, green : 128u8, blue : 0u8, alpha : 1.0}
311-
}
312-
pub fn lime() -> Color {
313-
Color {red : 0u8, green : 255u8, blue : 0u8, alpha : 1.0}
314-
}
315-
pub fn olive() -> Color {
316-
Color {red : 128u8, green : 128u8, blue : 0u8, alpha : 1.0}
317-
}
318-
pub fn yellow() -> Color {
319-
Color {red : 255u8, green : 255u8, blue : 0u8, alpha : 1.0}
320-
}
321-
pub fn navy() -> Color {
322-
Color {red : 0u8, green : 0u8, blue : 128u8, alpha : 1.0}
323-
}
324-
pub fn blue() -> Color {
325-
Color {red : 0u8, green : 0u8, blue : 255u8, alpha : 1.0}
326-
}
327-
pub fn teal() -> Color {
328-
Color {red : 0u8, green : 128u8, blue : 128u8, alpha : 1.0}
329-
}
330-
pub fn aqua() -> Color {
331-
Color {red : 0u8, green : 255u8, blue : 255u8, alpha : 1.0}
332-
}
333-
334-
335-
// The other 130 css colors
336-
// TODO
337-
}
338-
339293
// Define the colors specified by css
340294
define_color!(ALICEBLUE, 240, 248, 255)
341295
define_color!(ANTIQUEWHITE, 250, 235, 215)
@@ -483,4 +437,45 @@ define_color!(WHEAT, 245, 222, 179)
483437
define_color!(WHITE, 255, 255, 255)
484438
define_color!(WHITESMOKE, 245, 245, 245)
485439
define_color!(YELLOW, 255, 255, 0)
486-
define_color!(YELLOWGREEN, 154, 205, 50)
440+
define_color!(YELLOWGREEN, 154, 205, 50)
441+
442+
#[cfg(test)]
443+
mod test {
444+
use super::{rgb, rgba};
445+
use super::parsing::parse_color;
446+
447+
#[test]
448+
fn test_parsing_rgb() {
449+
assert!(parse_color("red").unwrap().eq(&parse_color("rgb(255,0,0)").unwrap()));
450+
assert!(parse_color("red").unwrap().eq(&parse_color("rgba(255,0,0,1.0)").unwrap()));
451+
assert!(parse_color("red").unwrap().eq(&parse_color("rgba(255,0,0,1)").unwrap()));
452+
assert!(parse_color("lime").unwrap().eq(&parse_color("rgba(0,255,0,1.00)").unwrap()));
453+
assert!(rgb(1u8,2u8,3u8).eq(&parse_color("rgb(1,2,03)").unwrap()));
454+
assert!(rgba(15u8,250u8,3u8,0.5).eq(&parse_color("rgba(15,250,3,.5)").unwrap()));
455+
assert!(rgba(15u8,250u8,3u8,0.5).eq(&parse_color("rgba(15,250,3,0.5)").unwrap()));
456+
assert!(None == parse_color("rbga(1,2,3)"));
457+
}
458+
459+
#[test]
460+
fn test_parsing_hsl() {
461+
assert!(parse_color("red").unwrap().eq(&parse_color("hsl(0,1,.5)").unwrap()));
462+
assert!(parse_color("lime").unwrap().eq(&parse_color("hsl(120.0,1.0,.5)").unwrap()));
463+
assert!(parse_color("blue").unwrap().eq(&parse_color("hsl(240.0,1.0,.5)").unwrap()));
464+
assert!(parse_color("green").unwrap().eq(&parse_color("hsl(120.0,1.0,.25)").unwrap()));
465+
assert!(parse_color("white").unwrap().eq(&parse_color("hsl(1.0,1.,1.0)").unwrap()));
466+
assert!(parse_color("white").unwrap().eq(&parse_color("hsl(129.0,0.3,1.0)").unwrap()));
467+
assert!(parse_color("black").unwrap().eq(&parse_color("hsl(231.2,0.75,0.0)").unwrap()));
468+
assert!(parse_color("black").unwrap().eq(&parse_color("hsl(11.2,0.0,0.0)").unwrap()));
469+
assert!(parse_color("gray").unwrap().eq(&parse_color("hsl(0.0,0.0,0.5)").unwrap()));
470+
assert!(parse_color("maroon").unwrap().eq(&parse_color("hsl(0.0,1.0,0.25)").unwrap()));
471+
assert!(parse_color("purple").unwrap().eq(&parse_color("hsl(300.0,1.0,0.25)").unwrap()));
472+
assert!(parse_color("fuchsia").unwrap().eq(&parse_color("hsl(300,1.0,0.5)").unwrap()));
473+
assert!(parse_color("olive").unwrap().eq(&parse_color("hsl(60.,1.0,0.25)").unwrap()));
474+
assert!(parse_color("yellow").unwrap().eq(&parse_color("hsl(60.,1.0,0.5)").unwrap()));
475+
assert!(parse_color("navy").unwrap().eq(&parse_color("hsl(240.0,1.0,.25)").unwrap()));
476+
assert!(parse_color("teal").unwrap().eq(&parse_color("hsl(180.0,1.0,.25)").unwrap()));
477+
assert!(parse_color("aqua").unwrap().eq(&parse_color("hsl(180.0,1.0,.5)").unwrap()));
478+
assert!(None == parse_color("hsl(1,2,3,.4)"));
479+
}
480+
}
481+
>>>>>>> 23872aa... Update

0 commit comments

Comments
 (0)