Charts are the black hole of programming.
I was warned, but I wanted to tackle charts anyway. That was my first mistake.
Humans tend to understand graphical information better than text on average. But presenting information visually in a compelling way is not only not easy. It’s hard.
If I wasn’t convinced, my recent experience left me with little doubt.
The process of adding simple charts took me down a number of rabbit holes to explore… and get lost in.
This post is intended to document three major takeaways from the process:
- Charts are hard; budget your time accordingly
- Purely random colors can be jarring; complementary colors can be derived using the Golden Ratio
- The math behind converting colors to / from RGB, HSV, HSL, and Hexidecimal representations is both fascinating, and something that should not need to be redefined every time.
However, I ran into some strange errors trying to incorporate it into the project, so I went with the MVP approach and used the basic package.
I plan to refactor in the future to use the React version as it seems to have better support for components, but in the mean time, I got it working.
I used the patternomaly package which helps to address accessibility concerns by including patterns on charts.
At this point, I don’t have a designer helping me with the app. Nor do I have a style guide that can support dozens of colors.
With that being the case, I should have just done the easiest thing possible to randomize colors for my chart. That would have looked something like:
This would have totally worked. The problem was that I wanted something slightly more … elegant. Thus, began my descent into rabbit hole number one: color theory.
I read close a dozen articles about color theory before finding Martin Ankerl’s post How to Create Random Colors Programmatically. In it, Martin outlines why purely random colors aren’t ideal and how to achieve complements with the Golden Ratio. Here’s one of his snippets
Okay, now we’re getting places! The problem was that function
hsv_to_rgb(). How did it work?
Queue rabbit hole number two.
When it comes to computers, colors are just numbers. But which numbers we use and how they relate was a source of hours of learning this week. Starting with the the wikipedia page on HSL and HSV and branching out from there.
Now that I had a bit of background on how the different color systems related, I needed to be able to go from one to the next.
Two resources were intensely valuable on this front:
- The Converting To RGB section on the Wikipedia page
- RapidTables is a site that will convert these numbers for you one at a time. They also include the derivations on their page for the interested parties (like me!).
With those references, I was able to write my own
hsv_to_rgb (as well as several others – see my full list at the end of the post) — and understand why it worked the way it did. At least for the most part!
Now that I had a my own color conversion functions, I could write a function that took an input of the number of color/pattern combinations I needed returned and would generate complements.
Going back and forth between different color types is something that comes up with some regularity. While there are libraries out there that will handle these calculations for you, I wanted to understand how they worked.
So, I wrote out functions for several common conversions. Here’s a gist with some of the common conversions I found particularly useful.