AmoebeLabs / swiss-army-knife-card

The versatile custom Swiss Army Knife card for Home Assistant allows you to create your unique visualization using several graphical tools, styling options and animations.
224 stars 19 forks source link

Make pollen SVGs stylable #199

Closed AmoebeLabs closed 1 year ago

AmoebeLabs commented 1 year ago

The Problem To Be Solved

With the working SVG Injector, SVGs should be stylable now.

The Pollen SVGs contain local styling definitions, which can not be overridden from the parent SVG container.

Using the online SVG Viewer, the pollen_grass_high.svg looks like this:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30.65 44.5">
  <defs>
    <style>
      .cls-1{fill:#ef7f01;}
    </style>
  </defs>
  <g id="Layer_2" data-name="Layer 2">
    <g id="pollen_grass">
      <path class="cls-1" d="M0,10.39A74.69,74.69,0,0,1,5.77,26.21,66.2,66.2,0,0,1,7.59,43v.14A1.4,1.4,0,0,0,9,44.49a1.45,1.45,0,0,0,1.24-1.59A67.57,67.57,0,0,0,7,25.76,73.49,73.49,0,0,0,0,10.39Z"/>
      <path class="cls-1" d="M18.79,41.75q.53-8.7.59-17.41c.32-1,.65-1.91,1-2.86A76.65,76.65,0,0,1,30.65,2,75.84,75.84,0,0,0,19.39,20.37q0-10.2-.72-20.37.06,12-.76,23.9A108.4,108.4,0,0,0,14,35.77c-.15-4.32,0-8.74-.19-13.15a58.65,58.65,0,0,0-1.14-10.17,22.9,22.9,0,0,0-3.8-9.19A22.77,22.77,0,0,1,12,12.6a57.88,57.88,0,0,1,.47,10c-.2,6.71-1.08,13.47-.54,20.48,0,0,0,.08,0,.12a1.35,1.35,0,0,0,1.53,1.23h0A1.36,1.36,0,0,0,15,43.21a110.88,110.88,0,0,1,2.31-11.75q-.51,5.67-1.23,11.3a.37.37,0,0,0,0,.11,1.44,1.44,0,0,0,1.22,1.6,1.33,1.33,0,0,0,1.37-1.08,1.31,1.31,0,0,0,2.6-.29,73,73,0,0,1,2.61-15.46,54.13,54.13,0,0,1,6.22-14.22,53.55,53.55,0,0,0-7.46,13.69A74.17,74.17,0,0,0,18.79,41.75Z"/>
    </g>
  </g>
</svg>

You can see the local class definition in the <defs> section, and the usage of this class in the <path class="cls-1"> definition.

Furthermore, the cls-1 class is used in every pollen SVG, and thus overwrites this class every time it is injected. Each SVG should use a unique class, and unique CSS variables to specify the color.

Additional background:

Related Issues (if any)

(Optional): Suggested Solution

The class and styling problems can be solved by:

The pollen_tree_very_hig.svg looks like this, and can be styled, and can be injected. It contains the following id/class/style definitions using BEM naming:

So now we can:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 29.93 45.05">
  <g id="Layer_2" data-name="Layer 2">
    <g id="pollen_tree__very_high" style="fill:var(--pollen_tree-color--very-high,#e73f10);">
      <path class="pollen_tree--very-high" d="M28.36,26.22l-1.25-1.44a20.86,20.86,0,0,1,1.49-6.56c-1.24.8-3-.15-3.58-1.5a7.78,7.78,0,0,1-.08-4.36,2.27,2.27,0,0,1-3.05,0,3.29,3.29,0,0,1-1.17-2.62l-1.29-.21A14.61,14.61,0,0,1,17.37,0L11.3,8.44a1.32,1.32,0,0,1-1.22-1.08A23.66,23.66,0,0,1,7.94,12H6.55l-1.83,4.3a.25.25,0,0,0-.21-.26l-.65,1L2.39,17a10.26,10.26,0,0,0,.25,3.9A3,3,0,0,0,1.91,23c-.71-.2-1.31.93-.74,1.4H0a6.46,6.46,0,0,0,1.27,4.39l-.41.41L3,30.66a1.45,1.45,0,0,0-.14,2,4.22,4.22,0,0,0,1.94,1.18l6.31,2.41.76.29a1.2,1.2,0,0,1,.31.13s.08.1.13.15a1.2,1.2,0,0,0,.34.21c.51.23,1.13.18,1.61.5a1.14,1.14,0,0,1,.25.24,1.71,1.71,0,0,1,.25.8l.9,6.43c0,.09.89-.31,1-.36a2.07,2.07,0,0,0,.71-.76.85.85,0,0,0,0-.68,7.67,7.67,0,0,1-.17-1.17l-.05-.56c-.1-.9-.2-1.8-.29-2.7l-.07-.67c0-.13-.05-.29-.06-.43v-.17c0-.25.34-.19.53-.19a1.56,1.56,0,0,0,.43,0,14.32,14.32,0,0,0,2.28-.88,19.33,19.33,0,0,0,4.27-2.69,20.06,20.06,0,0,0,3.46-3.67,19,19,0,0,0,1.33-2.14c.21-.39.4-.78.58-1.18l.24-.56c0-.07.13-.46.18-.47ZM16,34.59c.09.7.17,1.41.28,2.11a1,1,0,0,1-1.9.28v0c-.06-.58-.09-1.17-.14-1.75A1.18,1.18,0,0,1,14,35a1,1,0,0,1-.21-.45,55.65,55.65,0,0,1-12-9.43A54.44,54.44,0,0,0,14,32.63c0-.74-.06-1.48-.09-2.23l-.08,0a41.17,41.17,0,0,1-6.51-5.78,25.6,25.6,0,0,1-4.67-7.23A25.11,25.11,0,0,0,8,23.89a39.46,39.46,0,0,0,5.82,4.23c-.06-2.62,0-5.23.05-7.84A.61.61,0,0,1,13.6,20a.4.4,0,0,1-.09-.2,2,2,0,0,0-.73-.6c-.35-.21-.74-.4-1.13-.61A13.75,13.75,0,0,1,9.4,17.17a7.29,7.29,0,0,1-1.75-2,3.27,3.27,0,0,1-.3-2.57A3.12,3.12,0,0,0,7.93,15a6.6,6.6,0,0,0,1.85,1.68,13.26,13.26,0,0,0,2.27,1.11c.4.15.81.3,1.24.48a5,5,0,0,1,.65.33l.05,0A129.42,129.42,0,0,1,15.63,3.49a128.28,128.28,0,0,0-.75,17,29.26,29.26,0,0,0,7.93-6.75,30.52,30.52,0,0,1-7.66,8.09.62.62,0,0,1-.22.1c.06,1.83.16,3.66.3,5.48a44.23,44.23,0,0,0,5.89-3.12,28.35,28.35,0,0,0,5.71-4.73A28.22,28.22,0,0,1,21.68,25a45.79,45.79,0,0,1-6.24,4.35l0,0c.1,1.08.2,2.15.32,3.22A103.41,103.41,0,0,1,27.38,26.2,101.56,101.56,0,0,0,16,34.59Z"/>
    </g>
  </g>
</svg>

Furthermore, some of the pollen SVGs are made up of different parts. The pollen grass SVG shows this. By using multiple class definitions, one can style each part separately:

The fill style for the left part would become: var(--pollen_grass_left-color--high, var(--pollen_grass-color--high, #ef7f01))

(Optional): Alternative Solutions