mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22: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'
|
||||
};
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue