Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;(function(ns)
- {
- var color = {};
- color.reHexTest = /^\s*#[\da-fA-F]{1,6}\s*$/;
- color.reRgbTest = /^\s*rgb\s*\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\)\s*$/i;
- color.reRgbExec = /^\s*rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*$/i;
- color.reHexExec = /^\s*#([\da-fA-F]{1,2})([\da-fA-F]{1,2})?([\da-fA-F]{1,2})?\s*$/;
- color.isHex = function(color)
- {
- return this.reHexTest.test(color);
- }
- color.isRgb = function(color)
- {
- return this.reRgbTest.test(color);
- }
- color.rgbToHex = function(color)
- {
- var rgb;
- if (typeof color === 'object')
- {
- rgb = color[2] | (color[1] << 8) | (color[0] << 16);
- }
- else
- {
- if (this.isHex(color))
- {
- return color;
- }
- if (!this.isRgb(color))
- {
- return false;
- }
- var m = this.reRgbExec.exec(color);
- rgb = parseInt(m[3]) | (parseInt(m[2]) << 8) | (parseInt(m[1]) << 16);
- }
- rgb = rgb.toString(16);
- rgb = "000000".slice(rgb.length) + rgb;
- return '#' + rgb;
- };
- color.hexToRgb = function(color, bStr)
- {
- if (this.isRgb(color))
- {
- return color;
- }
- if (!this.isHex(color))
- {
- return false;
- }
- var m = this.reHexExec.exec(color);
- if (!m) { m = ['','0','0','0']; }
- if (!m[1]) m[1] = '0';
- if (!m[2]) m[2] = '0';
- if (!m[3]) m[3] = '0';
- var rgb = [ parseInt(m[1], 16), parseInt(m[2], 16), parseInt(m[3], 16) ]
- return (!bStr) ? rgb : "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
- };
- ns.textColor = color;
- })(window)
Advertisement
Add Comment
Please, Sign In to add comment