From bda24fbd5458b94f8c320ae3a908009449a5a8f6 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Wed, 9 Mar 2011 14:29:19 +0100 Subject: [PATCH] Color: simplify stringToRGB again. --- src/color/Color.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/color/Color.js b/src/color/Color.js index 9993687d..fc7ff912 100644 --- a/src/color/Color.js +++ b/src/color/Color.js @@ -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 {