Skip to content

Instantly share code, notes, and snippets.

@gotno
Last active November 29, 2023 22:52
Show Gist options
  • Save gotno/b117279f5dc358ea1bfdc9949d656afc to your computer and use it in GitHub Desktop.
Save gotno/b117279f5dc358ea1bfdc9949d656afc to your computer and use it in GitHub Desktop.
NVGcolor getModuleBodyColor(NSVGimage* svgHandle) {
// educated guess as to what a reasonable minimum area should be,
// based on testing a bunch of different modules
float largestArea{11000.f};
// default in case we can't find anything reasonable
NVGcolor bodyColor = nvgRGBA(255, 255, 255, 255);
for (NSVGshape* shape = svgHandle->shapes; shape; shape = shape->next) {
// skip invisible shapes
if (!(shape->flags & NSVG_FLAGS_VISIBLE)) continue;
// "Tight bounding box of the shape [minx,miny,maxx,maxy]."
float area =
(shape->bounds[2] - shape->bounds[0]) * (shape->bounds[3] - shape->bounds[1]);
// skip if this shape is smaller than the largest so far
if (area < largestArea) continue;
unsigned int color;
switch (shape->fill.type) {
case NSVG_PAINT_COLOR:
color = shape->fill.color;
break;
case NSVG_PAINT_LINEAR_GRADIENT:
case NSVG_PAINT_RADIAL_GRADIENT:
color = shape->fill.gradient->stops[0].color;
break;
default:
continue;
}
// skip transparent colors
if (((color >> 24) & 0xff) < 255) continue;
largestArea = area;
bodyColor = nvgRGBA(
(color >> 0) & 0xff,
(color >> 8) & 0xff,
(color >> 16) & 0xff,
(color >> 24) & 0xff
);
}
return bodyColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment