十進制色值轉十六進制

  • 輸入十進制數字, 默認顏色

    deciToHex(0) ==> #000000code

const deciToHex = (color, defaultColor) => {
  // 指定個非法的值不生效
  if (color && color[0] === '#') {
    return color
  }
  var c = defaultColor || 'none'
  color = parseInt(color)
  if (color === undefined || color > 16777215 || color < 0 || color === null) {
    return c
  }
  c = color.toString(16)
  c = leftZeroPad(c, 6)
  c = '#' + c
  return c
}
// 左填充
const leftZeroPad = (val, minLength) => {
  var MANY_ZEROS = '000000000000000000'
  if (typeof (val) !== 'string') {
    val = String(val)
  }
  return (MANY_ZEROS.substring(0, minLength - val.length)) + val
}
相關文章
相關標籤/搜索