(function(window){
"use strict";
var parseInt = window.parseInt
function solidColor(r,g,b) {
if (!this) return new solidColor(r,g,b);
this.type = "solid";
this.red = r;
this.green = g;
this.blue = b;
Object.freeze(this);
}
function alphaColor(r,g,b,a) {
if (!this) return new alphaColor(r,g,b,a);
this.type = "alpha";
this.red = r;
this.green = g;
this.blue = b;
this.alpha = a;
Object.freeze(this);
}
function unknownColor() {
if (!this) return new unknownColor();
this.type = "unknown";
Object.freeze(this);
}
function parseHexColor(rawstr) {
rawstr = rawstr.trim().substring(1);
if (rawstr.length === 8) {
return alphaColor(
parseInt(rawstr.substring(0,2), 16), parseInt(rawstr.substring(2,4), 16),
parseInt(rawstr.substring(4,6), 16), parseInt(rawstr.substring(4,6), 16)
);
} else if (rawstr.length === 4) {
return alphaColor(
parseInt(rawstr[0], 16) * 0x11, parseInt(rawstr[1], 16) * 0x11, parseInt(rawstr[2], 16) * 0x11, parseInt(rawstr[3], 16) * 0x11
);
} else if (rawstr.length === 6) {
return solidColor(
parseInt(rawstr.substring(0,2), 16), parseInt(rawstr.substring(2,4), 16), parseInt(rawstr.substring(4,6), 16)
);
} else if (rawstr.length === 3) {
return alphaColor( parseInt(rawstr[0], 16) * 0x11, parseInt(rawstr[1], 16) * 0x11, parseInt(rawstr[2], 16) * 0x11 );
}
return unknownColor();
}
// the red component of green
console.log(parseHexColor("#0f0").red);
// the alpha of transparent purple
console.log(parseHexColor("#f0f7").alpha);
// the entire color for turquoise
console.log(parseHexColor("#40E0D0"));
})(self);