I'm working on a project that deals with pseudo random event-driven color changes and needed a solution to generate hexadecimal codes for web use.
...
For further actions, you may consider blocking this person and/or reporting abuse
This is a great read for beginners. I also wondered how to make a random color generator before in my early days.
Also, you don't need to add an
id
to the<body>
tag; you can directly refer to it by writingdocument.body
. 😉TL;DR - you don't want to use that
@dood also every #ID is in DOM is accessible via
window[id]
however it's diacouraged to use that because it was implemnted in browsers due historic and compatibility reasons. More info here. It can be removed in future and make your page not working. Check answers and comments too. What I wanted to say is thatwindow.body
could be accessible because of same reason (I'm not sure).Oh, wow. I never even knew that you could access an
id
from thewindow
object. Thanks for making me learn something new today! I am genuinely surprised and amused by this.Thanks for the heads up. Does using id's to link js to the DOM hurt performance (at scale)?
I saw a tweet about this last week! Using
document.querySelector('body')
is actually more performant than usingdocument.body
. So I wonder if doingdocument.querySelector('#some-id')
would still be faster thandocument.body
! :)I wonder if there's a jsPerf for that by now. I'm really curious to see which method is more performant.
Yes, it's definitely going to hurt performance at scale. Though you shouldn't worry about too much unless it's completely necessary to optimize and squeeze every single bit of efficiency for your CPU.
Optimization is good, but as soon as you allow it to become premature optimization, that's when you should reconsider your priorities. You wouldn't want to allow the latter to get in the way of making actual progress in software development.
A while ago I played around with something similar. I needed to generate a bunch of random colors but that always looked awful, so i ended up writing this little snippet:
gist.github.com/herrfugbaum/7a6530... (I didn't get the embedding working 😂)
It generates random pastel colors, which are easier on the eyes in my opinion. The trick to "pastelify" is to just take each individual value add 255 (or FF) and divide it by two ✨
This is awesome! Now I get to spend this afternoon understanding the code. Thank you.
Have you considered using .toString(16)? Also, what about just using actual CSS integer RGB values instead of hex? Seems like either could simplify this. :)
toString(16) has the problem of numbers like 14 ending up being one character long, so you need a left pad function. rgb color values are the best solution, imo (or hsl)
Interesting problem! Here's my attempt of solving it in more functional programming style, without converting it into a convoluted one-liner. codepen.io/dabrorius/pen/MdPXxB
EDIT:
Scratch that! There's a simpler implementation: codepen.io/dabrorius/pen/xNyJMO
Nice work! Just one thing...I think your max on the hexSeed needs to be 16 or you won't ever get an F.
Thanks for pointing that out.
Just noticed, your first image is wrong - it specifies the pattern as RBG, it should be RGB
Man! I proof read it and a major problem still slipped through. Thanks for the heads up. Fixing it up.