css颜色方法

170 阅读1分钟

**

  • @description 随机生成十六进制颜色
  • @returns {string} */ xport const randomHexColor = () => { return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toSt } **
  • @description 随机生成rgb颜色
  • @returns {string} */ xport const randomRgbColor = () => { const r = Math.floor(Math.random() * 256) const g = Math.floor(Math.random() * 256) const b = Math.floor(Math.random() * 256) return rgb(${r},${g},${b}) } **
  • @description 随机生成rgba颜色
  • @returns {string} */ xport const randomRgbaColor = () => { const r = Math.floor(Math.random() * 256) const g = Math.floor(Math.random() * 256) const b = Math.floor(Math.random() * 256) const alpha = Math.random() return rgb(${r},${g},${b},${alpha}) } **
  • @description 十六进制转为rgb
  • @returns {string} */ xport const hex2Rgb = (hex) => { var rgb = [] if (/^#[0-9A-F]{3}/i.test(hex)) { let sixHex = '#' hex.replace(/[0-9A-F]/ig, function(kw) { sixHex += kw + kw }) hex = sixHex } if (/^#[0-9A-F]{6}/i.test(hex)) { hex.replace(/[0-9A-F]{2}/ig, function(kw) { rgb.push(eval('0x' + kw)) }) return rgb(${rgb.join(',')}) } else { console.log(Input ${hex} is wrong!) return 'rgb(0,0,0)' } } **
  • @description rgb转为十六进制
  • @returns {string} */ xport const rgb2Hex = (rgb) => { if (/^rgb((\d{1,3},){2}\d{1,3})/i.test(rgb)) { let hex = '#' rgb.replace(/\d{1,3}/g, function(kw) { kw = parseInt(kw).toString(16) kw = kw.length < 2 ? 0 + kw : kw hex += kw }) return hex } else { console.log(`Input {rgb} is wrong!`) return '#000' } }