export default {
  // hex颜色转rgb颜色
  hexToRgb(str: string) {
    str = str.replace('#', '')
    const hxs = str.match(/../g) || []
    const res = []

    for (let i = 0; i < 3; i++) {
      res[i] = parseInt(hxs[i], 16)
    }

    return res
  },
  // rgb颜色转hex颜色
  rgbToHex(rgb: Array<number>) {
    const hexs = rgb.map(item => item.toString(16))
    for (let i = 0; i < 3; i++) {
      if (hexs[i].length == 1) hexs[i] = '0' + hexs[i]
    }
    return '#' + hexs.join('')
  },
  // 加深
  darken(color: string, level: number) {
    const rgbc = this.hexToRgb(color)
    for (let i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level))
    return this.rgbToHex(rgbc)
  },
  // 变淡
  lighten(color: string, level: number) {
    const rgbc = this.hexToRgb(color)
    for (let i = 0; i < 3; i++) rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i])
    return this.rgbToHex(rgbc)
  }
}
