Convert Hex to RGBA

后端 未结 18 805
南方客
南方客 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:15

    Try

    // hex - str e.g. "#abcdef"; a - alpha range 0-1; result e.g. "rgba(1,1,1,0)"
    let hex2rgba= (hex,a)=> `rgb(${hex.substr(1).match(/../g).map(x=>+`0x${x}`)},${a})`
    

    /// hex - str e.g. "#abcdef"; a - alpha range 0-1; result e.g. "rgba(1,1,1,0)"
    let hex2rgba= (hex,a)=> `rgb(${hex.substr(1).match(/../g).map(x=>+`0x${x}`)},${a})`;
    
    function convert() {
      console.log(hex2rgba(inp.value,1));
    }
    <input id="inp" value="#abcdef" >
    <button onclick="convert()">convert</button>

    0 讨论(0)
  • 2020-12-04 12:15

    Adding to @ElDoRado1239

    For those that want to pass alpha value (typescript snippet):

    static hexToRGB(hex: string, alpha: number): string {
        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 (alpha) {
          return `rgba(${r}, ${g}, ${b}, ${alpha})`
        }
    
        return `rgba(${r}, ${g}, ${b})`;
      }
    
    0 讨论(0)
  • 2020-12-04 12:17
    //If you write your own code, remember hex color shortcuts (eg., #fff, #000)
    
    function hexToRgbA(hex){
        var c;
        if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
            c= hex.substring(1).split('');
            if(c.length== 3){
                c= [c[0], c[0], c[1], c[1], c[2], c[2]];
            }
            c= '0x'+c.join('');
            return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)';
        }
        throw new Error('Bad Hex');
    }
    
    hexToRgbA('#fbafff')
    
    /*  returned value: (String)
    rgba(251,175,255,1)
    */
    
    0 讨论(0)
  • 2020-12-04 12:17

    ES6 function for only handling 6 character hex with or without the '#':

    const hex2rgba = (hex, alpha = 1) => {
      const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
      return `rgba(${r},${g},${b},${alpha})`;
    };
    

    Usage:

    hex2rgba('#af087b', .5)   // returns: rgba(175,8,123,0.5)
    hex2rgba('af087b', .5)    // returns: rgba(175,8,123,0.5)
    hex2rgba('af087b')        // returns: rgba(175,8,123,1)
    
    0 讨论(0)
  • 2020-12-04 12:25

    If you want to convert hex to rgba then you can use this function,

    function hex2rgba_convert(hex,opacity){
     hex = hex.replace('#','');
     r = parseInt(hex.substring(0, hex.length/3), 16);
     g = parseInt(hex.substring(hex.length/3, 2*hex.length/3), 16);
     b = parseInt(hex.substring(2*hex.length/3, 3*hex.length/3), 16);
    
     result = 'rgba('+r+','+g+','+b+','+opacity/100+')';
     return result;
    }
    

    Here is details is hex to rgba

    0 讨论(0)
  • 2020-12-04 12:26

    Here is a function that returns rgb or rgba if you provide an alpha. The function also converts short hex color codes too.

    function:

    function hexToRgb(hex, alpha) {
       hex   = hex.replace('#', '');
       var r = parseInt(hex.length == 3 ? hex.slice(0, 1).repeat(2) : hex.slice(0, 2), 16);
       var g = parseInt(hex.length == 3 ? hex.slice(1, 2).repeat(2) : hex.slice(2, 4), 16);
       var b = parseInt(hex.length == 3 ? hex.slice(2, 3).repeat(2) : hex.slice(4, 6), 16);
       if ( alpha ) {
          return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')';
       }
       else {
          return 'rgb(' + r + ', ' + g + ', ' + b + ')';
       }
    }
    

    examples:

    hexToRgb('FF0000');// rgb(255, 0, 0)
    hexToRgb('#FF0000');// rgb(255, 0, 0)
    hexToRgb('#FF0000', 1);// rgba(255, 0, 0, 1)
    hexToRgb('F00');// rgb(255, 0, 0)
    hexToRgb('#F00');// rgb(255, 0, 0)
    hexToRgb('#F00', 1);// rgba(255, 0, 0, 1)
    
    0 讨论(0)
提交回复
热议问题