mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Color: simplify stringToRGB again.
This commit is contained in:
parent
eacf346aab
commit
bda24fbd54
1 changed files with 11 additions and 14 deletions
|
@ -64,8 +64,17 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
gray: '808080', dimgray: '696969', black: '000000'
|
gray: '808080', dimgray: '696969', black: '000000'
|
||||||
};
|
};
|
||||||
|
|
||||||
function hexToRGB(hex) {
|
function stringToRGB(string) {
|
||||||
hex = hex.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
|
// 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) {
|
if (hex.length >= 4) {
|
||||||
var rgb = new Array(3);
|
var rgb = new Array(3);
|
||||||
for (var i = 0; i < 3; i++) {
|
for (var i = 0; i < 3; i++) {
|
||||||
|
@ -75,18 +84,6 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
}
|
}
|
||||||
return rgb;
|
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 {
|
return {
|
||||||
|
|
Loading…
Reference in a new issue