js16进制颜色值转rgba
时间: 2024-04-24 18:27:10 浏览: 160
16进制和rgba颜色的相互转换
4星 · 用户满意度95%
要将JS中的16进制颜色值转换为RGBA格式,可以使用以下代码:
```javascript
function hexToRgba(hex) {
const rgba = \[\];
hex = hex.replace('#', '').padEnd(8, 'F');
for (let i = 0; i < hex.length; i += 2) {
rgba.push(parseInt(hex.slice(i, i + 2), 16));
}
return rgba;
}
```
这个函数将接受一个16进制颜色值作为参数,并返回一个包含RGBA值的数组。例如,如果你调用`hexToRgba('#FF0000')`,它将返回`\[255, 0, 0, 255\]`,表示红色的RGBA值。\[3\]
#### 引用[.reference_title]
- *1* *3* [JS 颜色16进制、rgba相互转换](https://blog.csdn.net/WwLK123/article/details/131196873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [js将十六进制颜色转rgb以及加深减淡颜色值](https://blog.csdn.net/chensong8331/article/details/104563718)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文