JavaScript
function ColorObject(color) {
var i, aclr, component;
var colorNames = this.colorNames;
this.colors = [0, 0, 0];
this.alpha = 1;
var parseColor = this.parseColor;
if (color.substr(0, 1) === "#") {
if (color.length == 4) {
aclr = color.split('');
for (i = 1; i < aclr.length; i += 1) {
component = parseInt(aclr[i], 16);
this.colors[i - 1] = component * 16 + component;
}
} else {
for (var i = aclr.length >> 1; i >= 0; i -= 1) {
this.colors[i] = parseInt(color.slice(1, 2), 16);
}
}
} else if (color.slice(0, 4) === "rgba") {
var digits = /rgba\(\s*(\d+%?)\s*,\s*(\d+%?)\s*,\s*(\d+%?)\s*,\s*([+-]?\d*.?\d*)\s*\)/.exec(color);
for (var i = 0; i < 3; i += 1) {
this.colors[i] = parseColor(digits[i + 1]);
}
this.alpha = parseFloat(digits[4]);
this.alpha = this.alpha < 0 ? 0 : this.alpha > 1 ? 1 : this.alpha;
} else if (color.substr(0, 3) === "rgb") {
var digits = /rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/.exec(color);
for (var i = 0; i < 3; i += 1) {
this.colors[i] = parseColor(digits[i + 1]);
}
} else {
if (color in colorNames) {
var digits = /rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\s*\)/.exec(colorNames[color]);
for (var i = 0; i < 3; i += 1) {
this.colors[i] = parseColor(digits[i + 1]);
}
}
}
}
ColorObject.prototype = {
colorNames: {
aqua: "rgb(0,255,255)",
black: "rgb(0,0,0)",
blue: "rgb(0,0,255)",
fuchsia: "rgb(255,0,255)",
gray: "rgb(128,128,128)",
green: "rgb(0,128,0)",
lime: "rgb(0,255,0)",
maroon: "rgb(128,0,0)",
navy: "rgb(0,0,128)",
olive: "rgb(128,128,0)",
purple: "rgb(128,0,128)",
red: "rgb(255,0,0)",
silver: "rgb(192,192,192)",
...