Inkscape.org
Beginners' Questions Is it possibile to use an indexed palette?
  1. #1
    ilpiero ilpiero @ilpiero

    Hi All,

    I've been a user of inkscape for years and i think i'ts one the best graphic tools ever.

    I'm doing a set of icons, each one is a separate .svg file, from time to time i'm asked to try to switch the color "theme" of the icons (something like "make it a lit colder" "change the outline to blue" or semothing like this), Since I'm working with a limited color set, it would be great if i could edit just the palette and see the outcome on all the svg files... something like a .css.

    Do you know if something like this is possible?

    Thanks,

    Marco

     

     

     

     

  2. #2
    Tyler Durden Tyler Durden @TylerDurden

    Inkscape can certainly use CSS style attributes. Forum member Xav has an exceptional set of Inkscape tutorials, including a number focused on CSS:

    http://www.peppertop.com/blog/?p=1563

  3. #3
    Marco Riva Marco Riva @zerocinquanta

    Hi @ilpiero . Maybe my tutorial can help you also. Crea la tua palette/ create your palette

  4. #4
    ilpiero ilpiero @ilpiero

    Thank you both,

    those are very useful resources. I understood perfectly how to create the palette!

    The bit that i still miss now is the replacement of a color, since the palette is not embedded into the SVG.

    The closest clue about it was here https://fullcirclemagazine.org/issue-141/  it work, but it requires to mangle the SVG a little with a text editor, maybe not so easy for a complex drawing.

    Thanks,

    Marco

     

  5. #5
    Kirstie Kirstie @Kirstie🌷
    *

    Hi ilpiero,
    There is an autopalette function. You create it for one svg file.
    (As you are working on icons, all seperate svg files, I am not sure if it is enough for you.
    I suppose you could put them in one svg file and when the colours are updated, export the icons to seperate svg files again.
    There is a plugin for this too, Inkporter. But let's first see if you know about the autopalette.)

    When you have an object with a colour, you go to the fill&stroke menu.
    To the right there is a button called palette swatch next to the question mark.
    If you click on it, this selected colour will be added to your auto palet.

    To find this autopalette, click in your Inkscape window on the right just above the palet bar on the little triangle.
    There is a list of all the palettes.
    At the top of the list you find the auto palette.
    Every time you change a colour in this autopalet, all colours linked to this colour will update.

    Is that the function you were looking for?

    *Edit: In English it is called swatch, where I said auto palette (the Dutch name I think, I am Dutch). So the name of the button is swatch.

     

  6. #6
    Xav Xav @Xav👹

    Yes, Kirstie's approach (i.e. using Swatches) is what you want. Personally I find Inkscape's UI a little frustrating for this as there's no button for adding a new Swatch directly in the Fill & Stroke dialog. Here's my recommendation for how to work with it when starting with a blank file:

    1. Choose a palette (but not Auto, at this stage) using the pop-up at the right of the colour swatches at the bottom of the screen.
    2. Draw a load of objects. For speed, draw one, then drag it around the screen while pressing Space from time to time.
    3. Set each object to a different fill colour from the palette.
    4. Select all the objects (Ctrl-A), then open the Fill & Stroke dialog.
    5. On the Fill tab, select the Swatch button (7th one along, just before the question mark).
    6. It should show the text "Multiple Styles", but behind the scenes it's creating a swatch for each fill colour in your selection.
    7. Deselect everything, then select a single object and take a look at the Swatch section of the dialog. You should see an entry for each of the colours from the previous steps.
    8. Now you can delete everything and set the palette to "Auto" before drawing your design.
    9. Any time you edit one of the swatches in the Fill & Stroke dialog, all the objects using that colour will be updated.

    With no "+" button in the Swatches section, the only practical way I know to create a new swatch is to create a new object, set its fill (or stroke) using the normal Flat Colour option, and then select the Swatches section with the object still selected. The object can then be deleted. If anyone knows a quicker or easier way then I'd love to hear it.

    This topic has revealed to me that I seem to have skipped Swatches entirely in my articles! I'll try to address that omission at some point in the future.

     

  7. #7
    Kirstie Kirstie @Kirstie🌷

    Hi Xav,
    Thank you for the extra explanation.

    It is possible to leave the default Inkscape palette at the bottom.
    And then you open an extra window with swatches: menu > view > swatches.
    With the tiny triangle in this window you can choose a palet, for instance auto, or a different one.

    I have a problem with the different names: palettes and swatches.
    But they refer to the same things, lists with colours (and sometimes also gradients that do not work very well).
    The palette is at the bottom of the screen, and swatches are windowed versions of the same.

    Yes, this topic is less known in Inkscape. It would nice if there was some instruction available. We have this topic now, that is something.

  8. #8
    Xav Xav @Xav👹

    Thanks @Kirstie, I'd completely overlooked the extra swatches dialog.

    Unfortunately it still doesn't make it a lot easier to create a new swatch. As far as I can tell you still need to set the colour you want on a new object, then open the Fill & Stroke dialog and switch to the Swatches panel to add it to the list. To me, automatic swatches feel like doing things the wrong way round: I would rather set up a few fixed swatches first, then draw my objects and apply the colours from the swatches I've defined, but be able to add a new swatch whenever I need to just by clicking a "+" button.

    As to the naming, I tend to think of a swatch as being an individual "chip" or "sample" of a colour. Several swatches together form a palette. But the terms are a little fuzzy and interchangeable.

     

  9. #9
    Tyler Durden Tyler Durden @TylerDurden

    My impression is that the OP would like to change the style across documents.

    ilpiero

    I'm doing a set of icons, each one is a separate .svg file... <snip>... it would be great if i could edit just the palette and see the outcome on all the svg files... something like a .css.

  10. #10
    ilpiero ilpiero @ilpiero

    Wow!

    Yes It would be great if the palette could be edited separately from the drawing, but in this case you would need two files to complete the image.

    I can work around the multi-file problem by editing the SVGs with a python script or something like this, once the color is called "by name" most of the work is done.

    Thanks!

    Marco

  11. #11
    Xav Xav @Xav👹

    If you're able to post process the files with a language and library that makes it practical to manipulate XML files, then the problem becomes a lot easier. Here's the approach I would take.

    1. Create a "swatches.svg" file by drawing rectangles for all the colours you want, and creating swatches as I described above. You can delete the objects if you want, but I think it's clearer if you create them some way outside the page area, and leave them there.
    2. For each swatch, edit the name in the swatches pane of the Fill & Stroke dialog. The name you choose will be used as the ID in the XML file. I would give each name a prefix to make it easier to automate the later steps, but that's not essential.
    3. When creating a new icon, start by dragging and dropping the "swatches.svg" file into the Inkscape window.
    4. At the resultant dialog, select "Include SVG image as editable object(s) in the current file". This will make your swatches available as part of the new document.
    5. Switch to the "Auto" palette at the bottom to reveal your swatches and use them when drawing.
    6. For existing files you can do the same as steps 4 & 5, but you'll then need to select each part of the existing drawing and set its colour from the swatches.

    Now each icon has its own copy of the swatches - but sharing common names/IDs across all the files. You can move, share and copy individual files and they will continue to work in isolation.

    When you need to change the palette, do the following:

    1. Open the "swatches.svg" file and make changes to the swatch colours. Do not change the names/IDs.
    2. In your language/library of choice, write some code to do the following:
      1. Open the "swatches.svg" file for reading.
      2. Open the icon file for reading/writing (typically the name of this file would be passed on the command line).
      3. Using an XML library, extract all the <linearGradient> elements from the <defs> section of the swatches file.
      4. Filter this collection to return only the swatches. This is where a prefix on the name/ID makes things easier. Inkscape prefixes its own IDs with "linearGradient" so you might be able to get away with just filtering all those out of the collection, but it would be safer to either use a list of known swatch names or use a prefix of your own to make sure you're just left with the elements you want.
      5. For each swatch in this collection, replace the element with the same ID in the icon file with the copy from the swatches file. I.e. copy the colours from the (updated) swatches document into the icon document, replacing the previous swatch.
      6. If no matching element can be found in the icon document, insert a copy of the swatch into the <defs> section anyway, to bring the file up to date with the latest palette you're using.
      7. Save the modified icon file (preferably with a different name, just in case something goes wrong).

    Run this application against each icon file (e.g. via a loop in your command line shell) and the job's done. Each new icon remains as a stand-alone file that can be individually edited or moved without needing the swatches file, but you can trivially update them all each time the palette needs to change.

     

  12. #12
    ilpiero ilpiero @ilpiero

    That was more or less my idea... I was wondering why Inkscape is using  gradient elements to define the colors... perhaps is it to work around some limitation of the svg format?

    Thanks!

    Marco

     

  13. #13
    Xav Xav @Xav👹
    ilpiero

    I was wondering why Inkscape is using gradient elements to define the colors... perhaps is it to work around some limitation of the svg format?

    SVG offers no native way to have named colours, apart from the single "currentColor" special-case. SVG has always been compatible with CSS, so you could use the historic CSS named colours ("red", "blue", "goldenrod"!), but couldn't define your own - and it wasn't certain that all SVG renderers would honour these named values anyway. It's only recently, with the addition of SVG custom properties (aka CSS variables), that it's possible to define named colours in CSS - and custom properties is such a general purpose addition to CSS, covering far more than just colours, that it's not likely to be available in Inkscape for some time, if ever.

    Historically, therefore, the Inkscape devs were faced with no way to define new named colours in CSS, nor any way to do it in SVG. So they adopted the rather clever idea of using single-stop gradients as a means to map a single colour (set in the stop) to a named object (the ID of the gradient). It does mean that swatches may not be compatible with every SVG renderer, as some may make the assumption that a gradient must contain at least two stops. But I don't think that's much of an issue these days.

     

Inkscape Inkscape.org Inkscape Forum Beginners' Questions Is it possibile to use an indexed palette?