November 29, 2023

body colors

Went off on a little tangent today and figured out how to semi-reliably pull the “main” color from an SVG, to use as the color of the non-textured faces of the mesh the SVG is painted on (ie the back and sides of a module, as seen above). Here’s how it works, for the curious.

SVG parsing in Rack is handled by nanosvg. A parsed SVG results in a list of shapes. Thanks to a helpful comment in the source, we know that each shape has a list of bounds which is a Tight bounding box of the shape [minx,miny,maxx,maxy]. From there, I simply iterate through shapes, calculate the area from the bounds, and take the color of the largest non-transparent area.

Is this a perfect solution? Absolutely not. But, at the very least, I get some color from the module’s palette. And more often than not, the shape with the largest area will be the background color of the panel or a color that stretches to the edges (for instance, with Befaco’s red ports) and therefore works well as the color for the sides of the mesh.