标签:
;(function($){ $.SmColor = { rgbInt2rgb: function (rgbInt) { return { red : (rgbInt >> 16) & 255, green : (rgbInt >> 8) & 255, blue : rgbInt & 255 }; }, rgbInt2hex: function (rgbInt) { var color = this.rgbInt2rgb(rgbInt); return this.rgb2hex(color.red, color.green, color.blue); }, rgb2hex: function (red, green, blue) { return "#" + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1); }, rgb2rgbInt: function (red, green, blue) { return ((red << 16) + (green << 8) + blue); }, hex2rgb: function (hexStr) { //hexStr必须为#rrggbb格式 var hex = parseInt(hexStr.substring(1), 16); return { red: (hex & 0xff0000) >> 16, green: (hex & 0x00ff00) >> 8, blue: hex & 0x0000ff }; }, hex2rgbInt: function (hexStr) { //hexStr必须为#rrggbb格式 var color = this.hex2rgb(hexStr); return this.rgb2rgbInt(color.red, color.green, color.blue); }, rgbObj2htm: function(rgbObj){ return "rgb("+ rgbObj.red +", "+ rgbObj.green +", "+ rgbObj.blue +")"; } }; })(jQuery); $(function(){ console.log("rgbInt2rgb(16711680):" + $.SmColor.rgbObj2htm($.SmColor.rgbInt2rgb(16711680))); console.log("rgbInt2hex(16711680):" + $.SmColor.rgbInt2hex(16711680)); console.log("rgb2hex(255,0,0):" + $.SmColor.rgb2hex(255,0,0)); console.log("rgb2rgbInt(255,0,0):" + $.SmColor.rgb2rgbInt(255,0,0)); console.log("hex2rgb(‘#ff0000‘):" + $.SmColor.rgbObj2htm($.SmColor.hex2rgb("#ff0000"))); console.log("hex2rgbInt(‘#ff0000‘):" + $.SmColor.hex2rgbInt("#ff0000")); }); 输出: //rgbInt2rgb(16711680):rgb(255, 0, 0) //rgbInt2hex(16711680):#ff0000 //rgb2hex(255,0,0):#ff0000 //rgb2rgbInt(255,0,0):16711680 //hex2rgb(‘#ff0000‘):rgb(255, 0, 0) //hex2rgbInt(‘#ff0000‘):16711680
标签:
原文地址:http://www.cnblogs.com/lhzp/p/5445784.html