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.
Program Design and Thinking:
I know that hexadecimal color codes represent red green and blue values in base 16. Which are defined by the following pattern:
I started thinking about what type and data structure to use in the implementation. I cam up with a general idea using a sets to define the base set and a general rule to build a six value hexadecimal color code.
- Where H is the set of hex values - These will be defined as strings.
-
The set 'Hexadecimal Color Code' - uses
+=, the operator from JavaScript to concatenate a range of values from H
At this point I was able to start writing some code. I will let the code speak for itself in the embed below.
Discussion and Insight:
As a Computer Science student and total nerd I have a question about this code.
- Can these values be generated totally numerically, then parsed as strings after generation? I know in C/C++ (my coding comfort zone) you can do this. With my current knowledge of JavaScript I have yet to discover a method.
Any other insights or comments would be appreciated.
The Code
Taking it Further:
After I implemented this I started thinking about doing more complex and potentially useful things with hex colors. For example, one could build on this idea and implement the code in such a way that only the R, G, or B get generated or a certain combination of the three.
Anyways, I hope some discussion arises from this, and this post was interesting.
If you enjoyed this content and want to support a poor freelancing student so I can eat and produce more content in the future, I accept donations via PayPal
















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. :)