【前端】js方法 hex转rgba

【前端】js方法 hex转rgba

//hex转rgba

javascript 复制代码
 //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)'
javascript 复制代码
来自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

javascript 复制代码
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;
}
相关推荐
tokepson5 小时前
Mysql下载部署方法备份(Windows/Linux)
linux·服务器·windows·mysql
wearegogog1236 小时前
基于 MATLAB 的卡尔曼滤波器实现,用于消除噪声并估算信号
前端·算法·matlab
韩师学子--小倪6 小时前
fastjson与gson的toString差异
java·json
Drawing stars6 小时前
JAVA后端 前端 大模型应用 学习路线
java·前端·学习
品克缤6 小时前
Element UI MessageBox 增加第三个按钮(DOM Hack 方案)
前端·javascript·vue.js
nbsaas-boot6 小时前
SQL Server 存储过程开发规范(公司内部模板)
java·服务器·数据库
小二·6 小时前
Python Web 开发进阶实战:性能压测与调优 —— Locust + Prometheus + Grafana 构建高并发可观测系统
前端·python·prometheus
小沐°6 小时前
vue-设置不同环境的打包和运行
前端·javascript·vue.js
行百里er6 小时前
用 ThreadLocal + Deque 打造一个“线程专属的调用栈” —— Spring Insight 的上下文管理术
java·后端·架构
玄〤7 小时前
黑马点评中 VoucherOrderServiceImpl 实现类中的一人一单实现解析(单机部署)
java·数据库·redis·笔记·后端·mybatis·springboot