假设背景为白色,将带alpha的HEX转换为非alpha



我基本上想在js 中编写scss混合函数

const base = '#86bc25'
const fade = 13 // => 13% =>ff to dd in Hex so it would be #86bc25dd
const newColor = someFunction(base,fade) // should turn out to be #216076

为什么我知道预期结果?在透明彩色xD 上使用了颜色选择器

找到了解决方案:

const oldColor = '#004862' // to 216076
const fade = 0.13
function fadeColor(oldColor,fade){
let r = parseInt(oldColor.substring(1,3),16)
let g = parseInt(oldColor.substring(3,5),16)
let b = parseInt(oldColor.substring(5,7),16)
r = (Math.round((fade*255 + (1-fade)*r)).toString(16)).split('.')[0]
g = (Math.round((fade*255 + (1-fade)*g)).toString(16)).split('.')[0] 
b = (Math.round((fade*255 + (1-fade)*b)).toString(16)).split('.')[0]
return (r.length===1?'0'+r:r)+(g.length===1?'0'+g:g)+(b.length===1?'0'+b:b)
}
console.log(fadeColor(oldColor,fade))

最新更新