news 2026/6/6 6:27:13

【前端】js方法 hex转rgba

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【前端】js方法 hex转rgba

【前端】js方法 hex转rgba

//hex转rgba

//hex转rgba const hex2Rgba = (bgColor, alpha = 1) => { let color = bgColor.slice(1); // 去掉'#'号 let rgba = [ parseInt("0x" + color.slice(0, 2)), parseInt("0x" + color.slice(2, 4)), parseInt("0x" + color.slice(4, 6)), alpha ]; return "rgba(" + rgba.toString() + ")"; }; //用法 hex2Rgba('#ffffff', 1) //'rgba(255,255,255,1)'
来自u-charts.js // hex 转 rgba function hexToRgb(hexValue, opc) { var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; var hex = hexValue.replace(rgx, function(m, r, g, b) { return r + r + g + g + b + b; }); var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); if(!rgb){ return hexValue; } var r = parseInt(rgb[1], 16); var g = parseInt(rgb[2], 16); var b = parseInt(rgb[3], 16); return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')'; }

十进制转hex

const getred = (color) => { const red = (color & 0xff0000) >> 16; return red; }; const getgreen = (color) => { const green = (color & 0x00ff00) >> 8; return green; }; const getblue = (color) => { const blue = color & 0x0000ff; return blue; }; // 十进制转hex const Rgb2Hex = (color) => {= const r = getred(color); const g = getgreen(color); const b = getblue(color); const hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); return hex; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/6 6:25:06

如何将League Gothic字体集成到Web项目:CSS @font-face终极教程

如何将League Gothic字体集成到Web项目&#xff1a;CSS font-face终极教程 【免费下载链接】league-gothic A revival of an old classic, Alternate Gothic #1 项目地址: https://gitcode.com/gh_mirrors/le/league-gothic League Gothic是一款经典的无衬线字体复兴版本…

作者头像 李华