如何实现颜色渐变、RGB与HEX之间的相互转换?
- 内容介绍
- 文章标签
- 相关推荐
本文共计93个文字,预计阅读时间需要1分钟。
javascriptfunction gradientColor(startColor, endColor, percentage) { const startRGB=parseInt(startColor.slice(1), 16); const endRGB=parseInt(endColor.slice(1), 16); const startR=(startRGB >> 16) & 0xFF; const startG=(startRGB >> 8) & 0xFF; const startB=startRGB & 0xFF; const endR=(endRGB >> 16) & 0xFF; const endG=(endRGB >> 8) & 0xFF; const endB=endRGB & 0xFF; const colorR=Math.round(startR + (endR - startR) * percentage); const colorG=Math.round(startG + (endG - startG) * percentage); const colorB=Math.round(startB + (endB - startB) * percentage); return `#${colorR.toString(16).padStart(2, '0')}${colorG.toString(16).padStart(2, '0')}${colorB.toString(16).padStart(2, '0')}`;}
本文共计93个文字,预计阅读时间需要1分钟。
javascriptfunction gradientColor(startColor, endColor, percentage) { const startRGB=parseInt(startColor.slice(1), 16); const endRGB=parseInt(endColor.slice(1), 16); const startR=(startRGB >> 16) & 0xFF; const startG=(startRGB >> 8) & 0xFF; const startB=startRGB & 0xFF; const endR=(endRGB >> 16) & 0xFF; const endG=(endRGB >> 8) & 0xFF; const endB=endRGB & 0xFF; const colorR=Math.round(startR + (endR - startR) * percentage); const colorG=Math.round(startG + (endG - startG) * percentage); const colorB=Math.round(startB + (endB - startB) * percentage); return `#${colorR.toString(16).padStart(2, '0')}${colorG.toString(16).padStart(2, '0')}${colorB.toString(16).padStart(2, '0')}`;}

