I'm making a program that requests a user's profile, and prints the name in the same colour as selected by the user. The problem is that the colour code I'm getting is this: 0xff1ba5f5
, and that to print the colour I need the hexadecimal colour code. Is there any way to convert this kind of colour code to hexadecimal code?
Asked
Active
Viewed 195 times
-1
-
2Welcome to Stack Overflow! Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) - you will be voted down for shjowing no effort – mplungjan Jun 13 '20 at 17:41
3 Answers
1
It's likely RGBA. Depending on how you need to use it, this site should point you in the right direction.
Basically, it already is hex, if you take 0x off the front of it. The values are 0xrrggbbaa. Where you have RGBA represented by rr, gg, bb, and aa respectively. RGB define the color (Red, Green, and Blue), while A represents the Alpha, or transparency.

TJBeanz
- 146
- 5
-
1Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – mplungjan Jun 13 '20 at 17:42
1
Try this
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
Now you can feed this into one of the codes at How to convert rgba to a transparency-adjusted-hex?

mplungjan
- 169,008
- 28
- 173
- 236
1
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)