I made a small Javascript color class for RGB and Hex colors, this class also includes RGB and Hex validation functions. I've added the code as a snippet to this answer.
var colorClass = function() {_x000D_
this.validateRgb = function(color) {_x000D_
return typeof color === 'object' &&_x000D_
color.length === 3 &&_x000D_
Math.min.apply(null, color) >= 0 &&_x000D_
Math.max.apply(null, color) <= 255;_x000D_
};_x000D_
this.validateHex = function(color) {_x000D_
return color.match(/^\#?(([0-9a-f]{3}){1,2})$/i);_x000D_
};_x000D_
this.hexToRgb = function(color) {_x000D_
var hex = color.replace(/^\#/, '');_x000D_
var length = hex.length;_x000D_
return [_x000D_
parseInt(length === 6 ? hex['0'] + hex['1'] : hex['0'] + hex['0'], 16),_x000D_
parseInt(length === 6 ? hex['2'] + hex['3'] : hex['1'] + hex['1'], 16),_x000D_
parseInt(length === 6 ? hex['4'] + hex['5'] : hex['2'] + hex['2'], 16)_x000D_
];_x000D_
};_x000D_
this.rgbToHex = function(color) {_x000D_
return '#' +_x000D_
('0' + parseInt(color['0'], 10).toString(16)).slice(-2) +_x000D_
('0' + parseInt(color['1'], 10).toString(16)).slice(-2) +_x000D_
('0' + parseInt(color['2'], 10).toString(16)).slice(-2);_x000D_
};_x000D_
};_x000D_
_x000D_
var colors = new colorClass();_x000D_
console.log(colors.hexToRgb('#FFFFFF'));// [255, 255, 255]_x000D_
console.log(colors.rgbToHex([255, 255, 255]));// #FFFFFF
_x000D_