Skip to content

Instantly share code, notes, and snippets.

@takkkun
Created July 9, 2013 09:10
Show Gist options
  • Save takkkun/5955883 to your computer and use it in GitHub Desktop.
Save takkkun/5955883 to your computer and use it in GitHub Desktop.
var Color = function() {
if (arguments.length == 1)
[this.r, this.g, this.b] = Color.parse(arguments[0]);
else if (arguments.length == 3)
[this.r, this.g, this.b] = arguments;
else
throw 'invalid number of arguments, takes one or three arguments';
};
Color.parse = function(color) {
switch (color.constructor) {
case String:
if (/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i.test(color))
return arguments.callee(RegExp.$1 + RegExp.$1 +
RegExp.$2 + RegExp.$2 +
RegExp.$3 + RegExp.$3);
else if (/^#?([0-9a-f]{6})$/i.test(color))
return arguments.callee(parseInt('0x' + RegExp.$1));
else if (color in Color.COLORS)
return arguments.callee(Color.COLORS[color]);
else
throw 'invalid string "' + color + '", ' +
'takes #XXX, #XXXXXX or COLOR NAME(sharp omittable)';
case Number:
return [color >> 16 & 0xff, color >> 8 & 0xff, color & 0xff];
case Object:
return [color.r, color.g, color.b];
case Array:
return color;
default:
throw 'invalid type';
}
};
Color.converter = {
rgb2ymck: function(red, green, blue) {
if (arguments.length == 1) [red, green, blue] = Color.parse(arguments[0]);
var min = Math.min(1 - red / 255, 1 - green / 255, 1 - blue / 255);
var a = 1 - min; // FIXME rename variable name "a"
return [
(a - blue / 255) / a * 255,
(a - green / 255) / a * 255,
(a - red / 255) / a * 255,
min * 255
];
},
rgb2hsv: function(red, green, blue) {
if (arguments.length == 1) [red, green, blue] = Color.parse(arguments[0]);
var max = Math.max(red, green, blue);
var min = Math.min(red, green, blue);
var value = max / 255;
var diff = max - min;
if (!diff) return [0, 0, value];
var saturation = diff / max;
var r = max - red * 60 / diff;
var g = max - green * 60 / diff;
var b = max - blue * 60 / diff;
var hue = red == max ? b - g : green == max ? 120 + r - b : 240 + g - r;
return [hue < 0 ? hue + 360 : hue, saturation, value];
},
hsv2rgb: function(hue, saturation, value) {
var rgb = function(red, green, blue) {
var fix = function(v) { return v > 255 ? 255 : v < 0 ? 0 : v };
return [fix(red), fix(green), fix(blue)];
};
saturation = Math.ceil(saturation * 255);
value = Math.ceil(value * 255);
if (!saturation) return rgb(value, value, value);
var diff = hue * 6 % 360; // FIXME rename variable name "diff"
var t1 = (255 - saturation) * value / 255;
var t2 = (255 - saturation * diff / 360) * value / 255;
var t3 = (255 - saturation * (360 - diff) / 360) * value / 255;
var p = Math.floor(hue / 60);
if (p == 0) return rgb(value, t3, t1);
else if (p == 1) return rgb(t2, value, t1);
else if (p == 2) return rgb(t1, value, t3);
else if (p == 3) return rgb(t1, t2, value);
else if (p == 4) return rgb(t3, t1, value);
else return rgb(value, t1, t2);
}
};
Color.prototype = {
number: function() {
return (this.r << 8 | this.g) << 8 | this.b;
},
name: function() {
return Color.NAMES[this.number()];
},
hex: function() {
var hex = this.number().toString(16);
for (var i = hex.length; i < 6; i++) hex = '0' + hex;
return '#' + hex;
},
rgb: function() {
return Color.parse(this);
},
ymck: function() {
return Color.converter.rgb2ymck(this);
},
hsv: function() {
return Color.converter.rgb2hsv(this);
},
pccs: function() {
},
munsell: function() {
},
toString: function() {
return this.name() || this.hex();
},
red: function(red) {
return new Color(red, this.g, this.b);
},
green: function(green) {
return new Color(this.r, green, this.b);
},
blue: function(blue) {
return new Color(this.r, this.g, blue);
},
yellow: function(yellow) {
return new Color(yellow, yellow, this.b);
},
magenta: function(magenta) {
return new Color(magenta, this.g, magenta);
},
cyan: function(cyan) {
return new Color(this.r, cyan, cyan);
},
hue: function(hue) {
var [h, s, v] = this.hsv();
return new Color(Color.converter.hsv2rgb(hue, s, v));
},
saturation: function(saturation) {
var [h, s, v] = this.hsv();
return new Color(Color.converter.hsv2rgb(h, saturation, v));
},
value: function(value) {
var [h, s, v] = this.hsv();
return new Color(Color.converter.hsv2rgb(h, s, value));
},
clone: function() {
return new Color(this);
}
};
(function(names) {
Color.NAMES = names;
var colors = {};
for (var color in names) colors[names[color]] = parseInt(color);
Color.COLORS = colors;
})({
0x000000: 'black',
0x000080: 'navy',
0x00008b: 'darkblue',
0x0000cd: 'mediumblue',
0x0000ff: 'blue',
0x006400: 'darkgreen',
0x008000: 'green',
0x008080: 'teal',
0x008b8b: 'darkcyan',
0x00bfff: 'deepskyblue',
0x00ced1: 'darkturquoise',
0x00fa9a: 'mediumspringgreen',
0x00ff00: 'lime',
0x00ff7f: 'springgreen',
0x00ffff: 'aqua',
0x00ffff: 'cyan',
0x191970: 'midnightblue',
0x1e90ff: 'dodgerblue',
0x20b2aa: 'lightseagreen',
0x228b22: 'forestgreen',
0x2e8b57: 'seagreen',
0x2f4f4f: 'darkslategray',
0x32cd32: 'limegreen',
0x3cb371: 'mediumseagreen',
0x40e0d0: 'turquoise',
0x4169e1: 'royalblue',
0x4682b4: 'steelblue',
0x483d8b: 'darkslateblue',
0x48d1cc: 'mediumturquoise',
0x4b0082: 'indigo',
0x556b2f: 'darkolivegreen',
0x5f9ea0: 'cadetblue',
0x6495ed: 'cornflowerblue',
0x66cdaa: 'mediumaquamarine',
0x696969: 'dimgray',
0x6a5acd: 'slateblue',
0x6b8e23: 'olivedrab',
0x708090: 'slategray',
0x778899: 'lightslategray',
0x7b68ee: 'mediumslateblue',
0x7cfc00: 'lawngreen',
0x7fff00: 'chartreuse',
0x7fffd4: 'aquamarine',
0x800000: 'maroon',
0x800080: 'purple',
0x808000: 'olive',
0x808080: 'gray',
0x87ceeb: 'skyblue',
0x87cefa: 'lightskyblue',
0x8a2be2: 'blueviolet',
0x8b0000: 'darkred',
0x8b008b: 'darkmagenta',
0x8b4513: 'saddlebrown',
0x8fbc8f: 'darkseagreen',
0x90ee90: 'lightgreen',
0x9370db: 'mediumpurple',
0x9400d3: 'darkviolet',
0x98fb98: 'palegreen',
0x9932cc: 'darkorchid',
0x9acd32: 'yellowgreen',
0xa0522d: 'sienna',
0xa52a2a: 'brown',
0xa9a9a9: 'darkgray',
0xadd8e6: 'lightblue',
0xadff2f: 'greenyellow',
0xafeeee: 'paleturquoise',
0xb0c4de: 'lightsteelblue',
0xb0e0e6: 'powderblue',
0xb22222: 'firebrick',
0xb8860b: 'darkgoldenrod',
0xba55d3: 'mediumorchid',
0xbc8f8f: 'rosybrown',
0xbdb76b: 'darkkhaki',
0xc0c0c0: 'silver',
0xc71585: 'mediumvioletred',
0xcd5c5c: 'indianred',
0xcd853f: 'peru',
0xd2691e: 'chocolate',
0xd2b48c: 'tan',
0xd3d3d3: 'lightgrey',
0xd8bfd8: 'thistle',
0xda70d6: 'orchid',
0xdaa520: 'goldenrod',
0xdb7093: 'palevioletred',
0xdc143c: 'crimson',
0xdcdcdc: 'gainsboro',
0xdda0dd: 'plum',
0xdeb887: 'burlywood',
0xe0ffff: 'lightcyan',
0xe6e6fa: 'lavender',
0xe9967a: 'darksalmon',
0xee82ee: 'violet',
0xeee8aa: 'palegoldenrod',
0xf08080: 'lightcoral',
0xf0e68c: 'khaki',
0xf0f8ff: 'aliceblue',
0xf0fff0: 'honeydew',
0xf0ffff: 'azure',
0xf4a460: 'sandybrown',
0xf5deb3: 'wheat',
0xf5f5dc: 'beige',
0xf5f5f5: 'whitesmoke',
0xf5fffa: 'mintcream',
0xf8f8ff: 'ghostwhite',
0xfa8072: 'salmon',
0xfaebd7: 'antiquewhite',
0xfaf0e6: 'linen',
0xfafad2: 'lightgoldenrodyellow',
0xfdf5e6: 'oldlace',
0xff0000: 'red',
0xff00ff: 'fuchsia',
0xff00ff: 'magenta',
0xff1493: 'deeppink',
0xff4500: 'orangered',
0xff6347: 'tomato',
0xff69b4: 'hotpink',
0xff7f50: 'coral',
0xff8c00: 'darkorange',
0xffa07a: 'lightsalmon',
0xffa500: 'orange',
0xffb6c1: 'lightpink',
0xffc0cb: 'pink',
0xffd700: 'gold',
0xffdab9: 'peachpuff',
0xffdead: 'navajowhite',
0xffe4b5: 'moccasin',
0xffe4c4: 'bisque',
0xffe4e1: 'mistyrose',
0xffebcd: 'blanchedalmond',
0xffefd5: 'papayawhip',
0xfff0f5: 'lavenderblush',
0xfff5ee: 'seashell',
0xfff8dc: 'cornsilk',
0xfffacd: 'lemonchiffon',
0xfffaf0: 'floralwhite',
0xfffafa: 'snow',
0xffff00: 'yellow',
0xffffe0: 'lightyellow',
0xfffff0: 'ivory',
0xffffff: 'white'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment