在javascript中将颜色代码转换为十六进制代码
Convert color code to hexadecimal code in javascript
我正在制作一个请求用户个人资料的程序,并以用户选择的相同颜色打印名称。问题是我得到的颜色代码是这样的:0xff1ba5f5
,要打印颜色我需要十六进制颜色代码。有什么办法可以把这种颜色代码转换成十六进制代码吗?
很可能是RGBA。根据您需要如何使用它,this site 应该会为您指明正确的方向。
基本上,它已经是十六进制了,如果你把它前面的 0x 去掉。值为 0xrrggbbaa。其中 RGBA 分别由 rr、gg、bb 和 aa 表示。 RGB 定义颜色(红色、绿色和蓝色),而 A 代表 Alpha 或透明度。
试试这个
const hexString = `0xff1ba5f5`
const rgbaString = hexString.slice(2)
console.log(rgbaString); // ff1ba5f5
const rgbaHex = hexString.match(/\w{2}/g)
console.log(rgbaHex); // array of hex
const hexColor = `#${rgbaHex.slice(1,4).join("")}`;
console.log(hexColor); // hex color another way
const rgba = `rgba(${rgbaHex.slice(1).map(hex => parseInt(hex,16))})`
console.log(rgba); // this can actually be used in HTML
现在您可以将其输入 How to convert rgba to a transparency-adjusted-hex?
的代码之一
var rgbToHex = function(rgb) {
var hex = Number(rgb).toString(16);
if (hex.length < 2) {
hex = "0" + hex;
}
return hex;
};
var a = rgbToHex(0xff1ba5f5)
console.log(a)
我正在制作一个请求用户个人资料的程序,并以用户选择的相同颜色打印名称。问题是我得到的颜色代码是这样的:0xff1ba5f5
,要打印颜色我需要十六进制颜色代码。有什么办法可以把这种颜色代码转换成十六进制代码吗?
很可能是RGBA。根据您需要如何使用它,this site 应该会为您指明正确的方向。
基本上,它已经是十六进制了,如果你把它前面的 0x 去掉。值为 0xrrggbbaa。其中 RGBA 分别由 rr、gg、bb 和 aa 表示。 RGB 定义颜色(红色、绿色和蓝色),而 A 代表 Alpha 或透明度。
试试这个
const hexString = `0xff1ba5f5`
const rgbaString = hexString.slice(2)
console.log(rgbaString); // ff1ba5f5
const rgbaHex = hexString.match(/\w{2}/g)
console.log(rgbaHex); // array of hex
const hexColor = `#${rgbaHex.slice(1,4).join("")}`;
console.log(hexColor); // hex color another way
const rgba = `rgba(${rgbaHex.slice(1).map(hex => parseInt(hex,16))})`
console.log(rgba); // this can actually be used in HTML
现在您可以将其输入 How to convert rgba to a transparency-adjusted-hex?
的代码之一var rgbToHex = function(rgb) {
var hex = Number(rgb).toString(16);
if (hex.length < 2) {
hex = "0" + hex;
}
return hex;
};
var a = rgbToHex(0xff1ba5f5)
console.log(a)