Ai
1 Star 0 Fork 12

NextZero/dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
toRGB.js 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
Yaohaixiao 提交于 2023-12-04 19:37 +08:00 . fix: 修复 HSL 格式转 hex 的错误
import _HSLToRGB from './_HSLToRGB'
import toHex from './toHex'
import { REG_RGB, REG_HEX, REG_HSL } from './utils/enum'
/**
* 返回将指定颜色(颜色英文名称、16 进制或者 HSL 色值)转化成 RGB 色值
* ========================================================================
* @method toRGB
* @since 0.4.0
* @param {String} color
* @return {String|Boolean}
*/
const toRGB = (color) => {
let matches = []
let hex
let rgb
let r
let g
let b
if (!color) {
return false
}
if (REG_RGB.test(color)) {
return color
}
if (REG_HSL.test(color)) {
matches = REG_HSL.exec(color)
rgb = _HSLToRGB(
parseInt(matches[1], 10),
parseInt(matches[2], 10),
parseInt(matches[3], 10)
)
r = rgb[0]
g = rgb[1]
b = rgb[2]
} else {
/* istanbul ignore else */
if (!REG_RGB.test(color)) {
hex = toHex(color)
}
matches = REG_HEX.exec(hex)
r = parseInt(matches[1], 16)
g = parseInt(matches[2], 16)
b = parseInt(matches[3], 16)
}
return 'rgb(' + [r, g, b].join(', ') + ')'
}
export default toRGB
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/nextzero/dom.js.git
git@gitee.com:nextzero/dom.js.git
nextzero
dom.js
dom.js
main

搜索帮助