JavaScript
/**
* Converts a hexadecimal color to an array
* Containing it's red, green and blue values
*
* @param {String} color Hexadecimal color
* @return {Array} Red, green and blue values of the color
*/
function hex2array(color) {
// Initialise variables
var rgb = [];
// Take off the hash
color = color.slice(1);
// Convert it to the right length if it is the shorthand
if(color.length === 3) {
color = color.replace(/([0-9a-f])/ig, '$1$1');
}
// Split the string into its main components and convert them to RGB
for(i = 0; i < 3; i += 1) {
rgb.push(parseInt(color.slice(i * 2, (i + 1) * 2), 16));
}
// Return the finished array
return rgb;
}
/**
* Converts an RGB color to an array
* Containing it's red, green and blue values
* The RGB color must resemble `rgb(0, 0, 0)`
*
* @param {String} color RGB color
* @return {Array} Red, green and blue values of the color
*/
function rgb2array(color) {
// Initialise variables
var i = null,
rgb = null;
// Take out the 'rgb(', ')', spaces and split it by commas
rgb = color.replace(/rgb\(|\)|\s/gi, '').split(',');
// Loop over the rgb values converting them to integers
for(i = 0; i < 3; i += 1) {
rgb[i] = parseInt(rgb[i], 10);
}
// Return the finished array
return rgb;
}
// Color names, used in a function down below
var colors = {
aliceblue: [240, 248, 255],
antiquewhite: [250, 235, 215],
aqua: [0, 255, 255],
aquamarine: [127, 255, 212],
azure: [240, 255, 255],
beige: [245, 245, 220],
bisque: [255, 228, 196],
black: [0, 0, 0],
blanchedalmond: [255, 235, 205],
blue: [0, 0, 255],
blueviolet: [138, 43, 226],
brown: [165, 42, 42],
burlywood: [222, 184, 135],
cadetblue: [95, 158, 160],
chartreuse: [127, 255, 0],
chocolate: [210, 105, 30],
coral: [255, 127, 80],
cornflowerblue: [100, 149, 237],
cornsilk:...