Convert Hex to RGBA

后端 未结 18 971
南方客
南方客 2020-12-04 11:36

My fiddle - http://jsbin.com/pitu/1/edit

I wanted to try an easy hex to rgba conversion. Ever browser I\'ve used renders colors using rgb as default so when using th

18条回答
  •  囚心锁ツ
    2020-12-04 12:27

    Pure JS solution if it helps:

    function hexToRGB(hex,alphaYes){
     var h = "0123456789ABCDEF";
     var r = h.indexOf(hex[1])*16+h.indexOf(hex[2]);
     var g = h.indexOf(hex[3])*16+h.indexOf(hex[4]);
     var b = h.indexOf(hex[5])*16+h.indexOf(hex[6]);
     if(alphaYes) return "rgba("+r+", "+g+", "+b+", 1)";
     else return "rgb("+r+", "+g+", "+b+")";
    }
    

    "alphaYes" is "true" or "false" depending upon whether you want the alpha or not.

    Preview

提交回复
热议问题