rgb和hsl转换(js)

JavaScript010

rgb和hsl转换(js),第1张

/**

    * HSL颜色值转换为RGB.

    * 换算公式改编自 http://en.wikipedia.org/wiki/HSL_color_space.

    * h, s, 和 l 设定在 [0, 1] 之间

    * 返回的 r, g, 和 b 在 [0, 255]之间

    *

    * @param  Number  h      色相

    * @param  Number  s      饱和度

    * @param  Number  l      亮度

    * @return  Array          RGB色值数值

    */

    function hslToRgb(h, s, l){

        var r, g, b

        if(s == 0){

            r = g = b = l// achromatic

        }else{

            var hue2rgb = function hue2rgb(p, q, t){

                if(t <0) t += 1

                if(t >1) t -= 1

                if(t <1/6) return p + (q - p) * 6 * t

                if(t <1/2) return q

                if(t <2/3) return p + (q - p) * (2/3 - t) * 6

                return p

            }

            var q = l <0.5 ? l * (1 + s) : l + s - l * s

            var p = 2 * l - q

            r = hue2rgb(p, q, h + 1/3)

            g = hue2rgb(p, q, h)

            b = hue2rgb(p, q, h - 1/3)

        }

        return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]

    }

    /**

    * RGB 颜色值转换为 HSL.

    * 转换公式参考自 http://en.wikipedia.org/wiki/HSL_color_space.

        * r, g, 和 b 需要在 [0, 255] 范围内

    * 返回的 h, s, 和 l 在 [0, 1] 之间

    *

    * @param  Number  r      红色色值

    * @param  Number  g      绿色色值

    * @param  Number  b      蓝色色值

    * @return  Array          HSL各值数组

    */

    function rgbToHsl(r, g, b){

        r /= 255, g /= 255, b /= 255

        var max = Math.max(r, g, b), min = Math.min(r, g, b)

        var h, s, l = (max + min) / 2

        if(max == min){

            h = s = 0// achromatic

        }else{

            var d = max - min

            s = l >0.5 ? d / (2 - max - min) : d / (max + min)

            switch(max){

                case r: h = (g - b) / d + (g <b ? 6 : 0)break

                case g: h = (b - r) / d + 2break

                case b: h = (r - g) / d + 4break

            }

            h /= 6

        }

        return [Math.floor(h*100), Math.round(s*100), Math.round(l*100)]

    }

原文:https://blog.csdn.net/qq_35321405/article/details/79786977

function hexToRgb(hex) {

    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)    

    

    return result ? {        

    

        r: parseInt(result[1], 16),       

        

        g: parseInt(result[2], 16),        

        

        b: parseInt(result[3], 16)    

    } : null

    

}

console.log( hexToRgb( '#FFFFFF' ) ) 

// return { r : 255, g : 255, b : 255 }

(function(d){function h(a,b,e){var c="rgb"+(d.support.rgba?"a":"")+"("+parseInt(a[0]+e*(b[0]-a[0]),10)+","+parseInt(a[1]+e*(b[1]-a[1]),10)+","+parseInt(a[2]+e*(b[2]-a[2]),10)d.support.rgba&&(c+=","+(a&&b?parseFloat(a[3]+e*(b[3]-a[3])):1))return c+")"}function f(a){var breturn(b=/#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(a))?[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],16),1]:(b=/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(a))?[17*parseInt(b[1],16),17*parseInt(b[2],

16),17*parseInt(b[3],16),1]:(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))?[parseInt(b[1]),parseInt(b[2]),parseInt(b[3]),1]:(b=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(a))?[parseInt(b[1],10),parseInt(b[2],10),parseInt(b[3],10),parseFloat(b[4])]:l[a]}d.extend(!0,d,{support:{rgba:function(){var a=d("script:first"),b=a.css("color"),e=!1if(/^rgba/.test(b))e=!0else try{e=b!=a.css("color","rgba(0, 0, 0, 0.5)").css("color"),

a.css("color",b)}catch(c){}return e}()}})var k="color backgroundColor borderBottomColor borderLeftColor borderRightColor borderTopColor outlineColor".split(" ")d.each(k,function(a,b){d.Tween.propHooks[b]={get:function(a){return d(a.elem).css(b)},set:function(a){var c=a.elem.style,g=f(d(a.elem).css(b)),m=f(a.end)a.run=function(a){c[b]=h(g,m,a)}}}})d.Tween.propHooks.borderColor={set:function(a){var b=a.elem.style,e=[],c=k.slice(2,6)d.each(c,function(b,c){e[c]=f(d(a.elem).css(c))})var g=f(a.end)

a.run=function(a){d.each(c,function(d,c){b[c]=h(e[c],g,a)})}}}var l={aqua:[0,255,255,1],azure:[240,255,255,1],beige:[245,245,220,1],black:[0,0,0,1],blue:[0,0,255,1],brown:[165,42,42,1],cyan:[0,255,255,1],darkblue:[0,0,139,1],darkcyan:[0,139,139,1],darkgrey:[169,169,169,1],darkgreen:[0,100,0,1],darkkhaki:[189,183,107,1],darkmagenta:[139,0,139,1],darkolivegreen:[85,107,47,1],darkorange:[255,140,0,1],darkorchid:[153,50,204,1],darkred:[139,0,0,1],darksalmon:[233,150,122,1],darkviolet:[148,0,211,1],fuchsia:[255,

0,255,1],gold:[255,215,0,1],green:[0,128,0,1],indigo:[75,0,130,1],khaki:[240,230,140,1],lightblue:[173,216,230,1],lightcyan:[224,255,255,1],lightgreen:[144,238,144,1],lightgrey:[211,211,211,1],lightpink:[255,182,193,1],lightyellow:[255,255,224,1],lime:[0,255,0,1],magenta:[255,0,255,1],maroon:[128,0,0,1],navy:[0,0,128,1],olive:[128,128,0,1],orange:[255,165,0,1],pink:[255,192,203,1],purple:[128,0,128,1],violet:[128,0,128,1],red:[255,0,0,1],silver:[192,192,192,1],white:[255,255,255,1],yellow:[255,255,

0,1],transparent:[255,255,255,0]}})(jQuery)