I am working on an interface containing many SVG artworks, created in Adobe Illustrator. The colors for these SVGs are to be customizable by the user, and all the artworks originally use the same set of colors. The idea is that once the user changes the each color, it is reflected in all the SVG artworks.
Right now, I have the functionality for this working fine. I first use LESS to generate all the shades of colors in my CSS and assign them to classes.
Like this
@color3-base: #FF0000;
@color3-shade1: mix(@color3-base, @color2-base, 90%);
@color3-shade2: mix(@color3-base, @color2-base, 80%);
@color3-shade3: mix(@color3-base, @color2-base, 70%);
@color3-shade4: mix(@color3-base, @color2-base, 60%);
@color3-shade5: mix(@color3-base, @color2-base, 50%);
@color3-shade6: mix(@color3-base, @color2-base, 40%);
@color3-shade7: mix(@color3-base, @color2-base, 30%);
@color3-shade8: mix(@color3-base, @color2-base, 20%);
and this
.color3-base-fill {fill: @color3-base;}
.color3-shade1-fill {fill: @color3-shade1;}
.color3-shade2-fill {fill: @color3-shade2;}
.color3-shade3-fill {fill: @color3-shade3;}
.color3-shade4-fill {fill: @color3-shade4;}
.color3-shade5-fill {fill: @color3-shade5;}
.color3-shade6-fill {fill: @color3-shade6;}
.color3-shade7-fill {fill: @color3-shade7;}
.color3-shade8-fill {fill: @color3-shade8;}
I use the less.js method less.modifyVars
to update the value of my base colors, like the color @color3-base
. Any change to each color results in all the different shades of those colors used throughout the SVG artworks. All colors are generated perfectly and there is no problem there.
I use the solution provided here to load all my SVGs inline, so that they are the part of my DOM and can take affect from all the color shade changes taking place in my LESS CSS.
Now here, in these loaded SVGs, I have manually gone in and replaced the fill statements with class statements. Like fill="#FF0000"
with a class="color3-base-fill"
(or the class belonging to the particular fill shade I am replacing) so that it retrieves the fill value from the class, and can always take affect from the color changes happening through LESS. Because all our artworks use the same set of colors, and now have their fill statements manually replaced with classes that correspond with those colors, this works great. A change to each color using less.modifyVars
reflects cleanly on all the colors on all the artworks.
The problem is that we have a large number of SVG artwork files and I have to replace every single fill statement in the SVG manually with its corresponding shade class. This opens margin for a lot of human error, and this appears to be something that should be automated instead.
What I would ideally like is to be able to make these replacements automatically, when the SVGs are loaded. This would make any artwork we make in Adobe Illustrator instantly customizable upon loading. I tried different things to convert my SVGs to strings first so that I can replace the fill declarations to class declarations, but I haven't been successful. It seems like SVG is neither XML, nor HTML and it is hard to serialize it. My knowledge is limited at this point so any help in solving the above problem will be great!
Thank you!