颜色工具函数
作者:互联网
颜色转换
import { normal } from 'color-blend'
/**
* @description 将16进制色号转换成rgb:{r,g,b,a}
* @param {string} color
* @param {float} Alpha
* @return {object}
*/
function colorRgbObj(color, Alpha = 1) {
color = color.toLowerCase()
var pattern = /^#([0-9|a-f]{3}|[0-9|a-f]{6})$/u
if (color && pattern.test(color)) {
// 处理六位的颜色值
var colorNew = []
for (var i = 1; i < 7; i += 2) {
colorNew.push(parseInt('0x' + color.slice(i, i + 2)))
}
return { r: colorNew[0], g: colorNew[1], b: colorNew[2], a: Alpha }
}
return color
}
/**
* scss mix 函数
* @param {string} colorA 16进制色号
* @param {string} colorB 16进制色号
* @param {*} Alpha
* @returns rgba
*/
export function colorMixer(colorA, colorB, Alpha) {
var rgbA = colorRgbObj(colorA, 1 - Alpha)
var rgbB = colorRgbObj(colorB, 1 - Alpha)
const { r, g, b, a } = normal(rgbA, rgbB)
return `rgba(${r},${g},${b},${a})`
}
/**
* @description 将16进制色号转换成rgba
* @param {string} color
* @param {float} Alpha
* @return {string}
*/
export function colorRgb(color, Alpha = 1) {
color = color.toLowerCase()
var pattern = /^#([0-9|a-f]{3}|[0-9|a-f]{6})$/u
if (color && pattern.test(color)) {
if (color.length === 4) {
// 将三位转换为六位
color = '#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3]
}
// 处理六位的颜色值
var colorNew = []
for (var i = 1; i < 7; i += 2) {
colorNew.push(parseInt('0x' + color.slice(i, i + 2)))
}
return `RGBA(${colorNew.join(',')} , ${Alpha})`
}
return color
}
标签:颜色,函数,color,param,return,var,Alpha,工具,colorNew 来源: https://www.cnblogs.com/gongxiansheng/p/16547288.html