bgracontrols icon indicating copy to clipboard operation
bgracontrols copied to clipboard

Set the color of svg icons in the PopulateImageList method

Open SSinteZ opened this issue 4 years ago • 2 comments

There is no way to set the color of svg icons in the PopulateImageList method. Useful for creating light and dark app theme

SSinteZ avatar Nov 03 '21 13:11 SSinteZ

Hi, is not that easy I think, at least you must iterate each color in the XML of the svg image, and apply a function into it to make it darker or lighter.

lainz avatar Nov 06 '21 16:11 lainz

I can see 4 ways to do it:

  • iterate through the XML (not practical indeed)
  • iterate through the SVG elements (I've just added an IterateElements method to do that). Here is a sample callback function:
procedure TForm1.ChangeFill(AElement: TSVGElement; AData: pointer;
  var ARecursive: boolean);
begin
  if AElement is TSVGDefine then
    ARecursive:= false
  else
  begin
   if not AElement.isFillNone then AElement.fillColor := CSSSkyBlue;
   if not AElement.isStrokeNone then AElement.strokeColor := CSSOrange;
  end;
end; 

// used like this:
svg.IterateElements(@ChangeFill, nil, true);
  • to use the currentColor variable in the SVG and to define it at the top level when drawing the SVG (I've just added a Color property to TBGRASVG class)
  • render the SVG in a temporary image and apply a filter to change its color (as done in TBGRASVGTheme)

circular17 avatar Nov 07 '21 10:11 circular17