Weeknotes № 12-13
Shapes and Gradients
In between all the workshops and family stuff I’ve made progress in a few different areas of the SwiftUI Field Guide.
I’ve been trying to implement gradients, which turned out to be a bit of a rabbit hole. First of all, currently all shapes are rendered using the canvas API (originally, the entire SwiftUI port was rendered in a single canvas). To implement different gradients, I wanted to switch that out to SVG rendering.
In canvas, you define paths using the Path2D
type which isn’t “inspectable”. In other words, I needed to replicate the Path2D
API in my own type, which we can then use to turn into the SVG drawing commands. Mostly, everything works as before, but I need to put in a bit more time to make animations work as well.
To draw gradients, SVG has builtin support for linear and radial gradients. However, I noticed that these don’t look quite the same as SwiftUI. In the end, a gradient is just a function that interpolates between two colors. SVG gradients use an RGB color space, and to interpolate between two colors, each of the components (red, green and blue) are interpolated separately. It took a while to figure out that SwiftUI uses a different color space. In fact, you can actually configure whether to use the device color space or a “perceptual” color space (with the perceptual color space being the default).
From my testing, it looks like the perceptual color space used is OKLab. After figuring that out, I searched for “OKLab SwiftUI” and found a tweet by (who else) Robb who seems to have figured out the same thing (more than one year before me, of course). I found this interactive review very helpful in understanding and visualizing the differences.
For now, I’m approximating a perceptual gradient by generating a linear RGB gradient with a number of extra “hidden” stops in between (which are interpolated in the OKLab color space). The implementation isn’t perfect but it’s a good start.
Early last December, I started to make calls to incorporate my new company. Most steps are done, but the German bureaucracy is not joking around on this one. I finally got my tax number a few weeks ago. However, for some opaque reason I couldn’t get a VAT ID After many calls to different agencies it took some steps to get the process unstuck (no one told me it was stuck, though). While I did apply for a new number now, the process might apparently take up to two months. I can’t send (or pay) any invoices outside of Germany until then, which is extremely annoying. It truly isn’t easy to start a business here. Luckily, my German is now good enough that I can at least talk to the people responsible for the individual steps. When they write letters, it’s nearly impossible to decipher them for someone like me, but in the end, they just seem to like to use complicated words and lots of jargon.