Convert Hex to RGBA

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

    And another one based around bit shifting.

    // hex can be a string in the format of "fc9a04", "0xfc9a04" or "#fc90a4" (uppercase digits are allowed) or the equivalent number
    // alpha should be 0-1
    const hex2rgb = (hex, alpha) => {
      const c = typeof(hex) === 'string' ? parseInt(hex.replace('#', ''), 16)  : hex;
      return `rgb(${c >> 16}, ${(c & 0xff00) >> 8}, ${c & 0xff}, ${alpha})`;
    };
    

提交回复
热议问题