0

This is my first question on here, please be gentle. I just noticed in Safari, Chrome and Firefox's console, the color values from the following script always return an RGB value instead of a HEX value. Not that it's preventing the code from working or anything, I'm just curious to know why wouldn't the console return the HEX value that was sent? Does Javascript always translate color values to RGB? Is it a first class citizen over other paradigms?

function randomHEX() {
    var HEXvalues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
    var newHEX = "#";

    for (i = 0; i < 6; i++) {
        var nextIndex = Math.floor(Math.random() * HEXvalues.length); // grab a random value from the array
        newHEX += HEXvalues[nextIndex]; // increment values to newHEX
    }

    document.querySelector("#hexName").textContent = newHEX; // use newHEX to fill text of ID hexName
    document.querySelector(".container").style.backgroundColor = newHEX; // use newHEX to change background color of box
}

Safari console window

Any information would be appreciated.

Many thanks.


PS

Thank you to those who responded. My question though was why RGB? Why not default to the HEX number it received? Why does Javascript translate a HEX value to an RGB value with .style.backgroundColor?

Again, many thanks.

jerome
  • 23
  • 4

2 Answers2

0

Yes by default it is displayed as RGB. To convert it to HEX, it has been already answered here link

Paul Baiju
  • 419
  • 7
  • 20
0

This is not your issue. Your function makes HEX value correctly. You can check it by adding console.log(newHEX) at the end of your function.

element.style.color = HEX_string it will be converted into RGB value.

If you need to convert it back - check this question.

vovchisko
  • 2,049
  • 1
  • 22
  • 26