代码拉取完成,页面将自动刷新
import _HSLToRGB from './_HSLToRGB'
import { KEYWORDS, REG_HSL, REG_RGB, REG_HEX3, REG_HEX } from './utils/enum'
/**
* 返回将指定颜色(颜色英文名称、RGB格式或者 HSL 格式色值)转化成 16 进制色值
* ========================================================================
* @method toHex
* @since 0.4.0
* @param {String} color
* @return {String|Boolean}
*/
const toHex = (color) => {
let matches = []
let hex
let rgb
let r
let g
let b
if (!color) {
return false
}
hex = KEYWORDS[color] || color
if (REG_HEX.test(hex)) {
return hex
}
if (REG_HSL.test(hex)) {
matches = REG_HSL.exec(hex)
rgb = _HSLToRGB(
parseInt(matches[1], 10),
parseInt(matches[2], 10),
parseInt(matches[3], 10)
)
return toHex(`rgb(${rgb.join(',')})`)
} else {
matches = REG_RGB.exec(hex)
}
if (matches) {
r = matches[1]?.length === 1 ? '0' + matches[1] : Number(matches[1])
g = matches[2]?.length === 1 ? '0' + matches[2] : Number(matches[2])
b = matches[3]?.length === 1 ? '0' + matches[3] : Number(matches[3])
hex = [r.toString(16), g.toString(16), b.toString(16)].join('')
}
if (hex.length < 6) {
hex = hex.replace(REG_HEX3, '$1$1')
}
/* istanbul ignore else */
if (hex !== 'transparent' && hex.indexOf('#') < 0) {
hex = '#' + hex
}
return hex.toLowerCase()
}
export default toHex
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。