Home > Web Front-end > JS Tutorial > body text

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

巴扎黑
Release: 2017-09-05 09:39:49
Original
1840 people have browsed it

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})`;
}
Copy after login

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})`;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!