js 色号排序 hex16进制颜色与rgb互转

function rgbToHex(r, g, b) {
    return ((r << 16) | (g << 8) | b).toString(16);
  }

function hexToRgba(hex, opacity = 1) {
    return {
      r: parseInt("0x" + hex.slice(1, 3)),
      g: parseInt("0x" + hex.slice(3, 5)),
      b: parseInt("0x" + hex.slice(5, 7))
    }

  }

function getColorLevel(color) {
    const {
      r,
      g,
      b
    } = hexToRgba(color)
    return r * 0.299 + g * 0.587 + b * 0.114
  }
原文地址:https://www.cnblogs.com/wlor/p/14210714.html