DEV Community

What the Hex!? (how to generate random hex color codes in javascript)

Emmy | Pixi on February 13, 2020

For my most recent Flatiron School project, I wanted to be able to programmatically change the background color of one of the elements on the page,...
Collapse
 
khauri profile image
Khauri • Edited

Nice work! I love seeing the thought processes behind figuring out how to make code better. It's like watching someone piece together a mystery in front of you.

I'm sure you know this as well, but for anyone who doesn't, if you prefix a number with 0x you can use hex values in your code. (There's also 0b for binary and 0o for octal).

Something else to note is that when using Math.floor1 since Math.random generates a random number between 0 and 1, but never exactly 1, you have to add 1 to your maximum or else it will never generate that value. So in this case, you should go up to 16 or you'll never get the value f:

Math.floor(Math.random() * 16)
// or 
Math.floor(Math.random() * 0x10)

1 I didn't read the article well enough and assumed Math.floor was being used

Collapse
 
thecodepixi profile image
Emmy | Pixi

Thanks, Khauri! I actually didn't know about the 0x thing. That's super cool! And ++ to the Math.floor() point. It's one of those things I always forget. I used Math.round() in its place but do know that that's not the best solution.

Collapse
 
thecodepixi profile image
Emmy | Pixi • Edited

Yeah someone else pointed this out to me. Math.floor vs Math.round is one of those things I always forget. Also, excellent work on the alt method! Just as a note, with that solution, it is possible you will get hex codes that are less than 6 digits in length, which won't work as color codes. You'll still need a way to check for the length of the code.

Collapse
 
pachicodes profile image
Pachi πŸ₯‘

Just wanted to say I am proud of you for writing this post!
It is great and I must go do something similar now πŸ˜‚ maybe with cats πŸ€”

Collapse
 
thecodepixi profile image
Emmy | Pixi

I demand colorful cats!

Collapse
 
pachicodes profile image
Pachi πŸ₯‘

O M G !!! How do I do colorful cats? Idk but I must find out!

Thread Thread
 
thecodepixi profile image
Emmy | Pixi

I have no idea but I believe in your ability to figure it out haha

Thread Thread
 
pachicodes profile image
Pachi πŸ₯‘

Thank you for your trust! 🌈😽😼

 
thecodepixi profile image
Emmy | Pixi

Just to clarify, in the article, the intention is to generate individual digits and convert them to hex one at a time until you have a string of 6 hexadecimal digits. If you just generate any decimal number between 0 and 16777215 and then convert that number to hexadecimal, it will sometimes output hexadecimal numbers shorter than 6 digits (i tested and got a few 4 and 5 digit results) which cannot be used as a hex color code and will cause the function to break if used to assign a code to an element on the page. You still need a way to check for length of 6 on your output. That said, your code does work a lot of the time. But sometimes it doesn't. For me, it broke about 1/20 times when I tested it.

Collapse
 
xanderyzwich profile image
Corey McCarty

You could also scale your random by the max value #ffffff and you wouldn't need a loop.

Collapse
 
thecodepixi profile image
Emmy | Pixi

Someone else pointed this out, and it does work quite often, but you do still need to check for a length of 6 on your output for it to be a valid hex color code. Testing that code, it does occasionally output numbers of 4 or 5 digits in length which will break the intended functionality.

Collapse
 
xanderyzwich profile image
Corey McCarty

I believe that there is a function to pad the left side of the string with zeroes up to a given length (or you could do it yourself)

Collapse
 
halented profile image
Hal Friday

so fun

Collapse
 
thecodepixi profile image
Emmy | Pixi

I know, right!? I want to sit and click that lil square on codepen forever haha

Collapse
 
helleworld_ profile image
DesirΓ© πŸ‘©β€πŸŽ“πŸ‘©β€πŸ«

Loving it!

Collapse
 
beerwerd profile image
Beerwerd

Your code is shorter but harder to understand.

Collapse
 
shan810 profile image
Shantanu Borgohain

Very Nice!
But, I could see the function is excluding the value f every time. Do you have a fix for that?