Skip to content

hex 转 rgba

js
/**
 * @param {string} hex The value of hex.
 * @param {number} transparency Transparency value.
 * @return {string} The color value represented by rgba.
 */
const hex2rgba = (hex, transparency = 0.8) => {
  let red = 0;
  let green = 0;
  let blue = 0;

  let h = hex.substring(1);
  if (h.length === 3) h = [...h].map((x) => x + x).join("");

  const hexValues = h.split("").map((item) => parseInt(item, 16));

  red = hexValues[0] * 16 + hexValues[1];
  green = hexValues[2] * 16 + hexValues[3];
  blue = hexValues[4] * 16 + hexValues[5];

  return `rgba(${red}, ${green}, ${blue}, ${transparency})`;
};
2025( )
今日 8.33%
本周 42.86%
本月 48.39%
本年 4.11%
Powered by Snowinlu | Copyright © 2024- | MIT License