码迷,mamicode.com
首页 > Web开发 > 详细

Jquery rgbInt rgb hex互转

时间:2016-04-29 14:32:07      阅读:621      评论:0      收藏:0      [点我收藏+]

标签:

;(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

 

Jquery rgbInt rgb hex互转

标签:

原文地址:http://www.cnblogs.com/lhzp/p/5445784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!