Color: simplify stringToRGB again.

This commit is contained in:
Jonathan Puckey 2011-03-09 14:29:19 +01:00
parent eacf346aab
commit bda24fbd54

View file

@ -64,8 +64,17 @@ var Color = this.Color = Base.extend(new function() {
gray: '808080', dimgray: '696969', black: '000000'
};
function hexToRGB(hex) {
hex = hex.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
function stringToRGB(string) {
// If the string isn't a hex code, it is a named color whose hex code
// needs to be looked up in the namedColor object:
if (!string.match(/^#[0-9a-f]{3,6}$/i)) {
string = namedColors[string];
if (!string)
throw new Error('The named color "' + string
+ '" does not exist.');
}
// Now convert the hex code to [r, g, b]:
var hex = string.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
if (hex.length >= 4) {
var rgb = new Array(3);
for (var i = 0; i < 3; i++) {
@ -75,18 +84,6 @@ var Color = this.Color = Base.extend(new function() {
}
return rgb;
}
}
function stringToRGB(string) {
if (string.match(/^#[0-9a-f]{3,6}$/i)) {
return hexToRGB(string);
} else {
var hex = namedColors[string];
if (!hex)
throw new Error('The named color "' + string
+ '" does not exist.');
return hexToRGB(hex);
}
};
return {