Home  >  Article  >  Web Front-end  >  Detailed tutorial on how to convert color decimal to rgba format using JS

Detailed tutorial on how to convert color decimal to rgba format using JS

巴扎黑
巴扎黑Original
2017-09-05 09:39:491838browse

This article mainly introduces the method of JS to convert decimal color into rgba format, involving the relevant operation skills of javascript for color numerical conversion. Friends in need can refer to the following

Examples of this article Developed a JS method to convert the decimal color into rgba format. Share it with everyone for your reference, the details are as follows:

When we know an integer such as (a color value between 0~256x256x256x256), and want to obtain its 3 primary colors plus a transparency, that is, alpha, red, green, blue, you can use the following methods:

Method one:


##

function getColor(number) {
  let color = number;
  const blue = parseInt(color % 0x100, 10);
  color = color >>> 8;
  const green = parseInt(color % 0x100, 10);
  color = color >>> 8;
  const red = parseInt(color % 0x100, 10);
  const alpha = (parseInt(color >>> 8, 10) / 0xFF).toFixed(1);
  return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
}

Method two:


function getColor(number) {
  const alpha = number >> 24 & 0xff;
  const red = number >> 16 & 0xff;
  const green = number >> 8 & 0xff;
  const blue = number & 0xff;
  return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
}

The above is the detailed content of Detailed tutorial on how to convert color decimal to rgba format using JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn