Watching the latest episode of Handmade Hero on YouTube by Casey Muratori, there was an interesting application for generating data a priori (i.e. during the compilation of the program). For high performance or real-time critical applications it is advisable to do as many calculations as possible while compiling the program. This saves important CPU cycles at runtime, which can be used for other calculations.
Around the 40 minutes mark, Casey talks about the visualization of memory allocations using different colors in order to figure out how his game that he is developing openly makes use of main memory. He continues to reason about how to get the largest possible distance between the individual RGB colors to separate them better visually:
One problem with RGB color space is that the spectral colors red, green and blue are aligned in a 3D grid where each color spans one dimension inside this color space.
This property makes it difficult to evenly space out each (r,g,b) color triplet so that their distance is as large as possible to each other for a given number of distinct colors. One common solution to this problem is to use another color space where all the visible colors can be accessed in a linear fashion. Is is much simpler to even out several distinct points on a line than in 3D space.
The HSV color space shown on the right is an alternative representation of the RGB color space where all the visible colors are arranged in a radial slice around the central axis of neutral colors from black at the bottom to white at the top. If we now virtually roll up the radial slice, we get a linear sequence of the different color levels from red to green to blue. We cam also easily transform a given color triplet from one color space to another.
In the Handmade Hero video linked above, Casey was asking for a fixed number of
distinct debug colors to visualize the memory consumption of his game. In fact,
we can generate this list of colors already during compile-time as all
necessary informations are known beforehand. The generation of arbitrary data
during compile-time such as lookup tables (LUTs) was historically a tedious job
using C++. However, with the advent of C++11/C++14 this task became much
Following is a short template function that is instantiated with a fix number N of colors. Inside the function a LUT is generated by dividing the radial HSV color slice in N different parts and then calculating the RGB color triplet pendants:
On the calling site we can now easily create an arbitrary list of debug colors using:
Note that we cannot use
constexpr is currently not
implemented in the
<cmath> header. However, this issue has been addressed
already and may be implemented in a future version of C++.
I’m available for software consultancy, training and mentoring. Please contact me, if you are interested in my services.