Skip to content

Commit

Permalink
update to support inkscape 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
olibia committed Jun 13, 2020
1 parent 5bce99f commit 9358db2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
46 changes: 27 additions & 19 deletions generate_palette.inx
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Generate</name>
<id>hardpixel.eu.generate_palette</id>
<dependency type="executable" location="extensions">generate_palette.py</dependency>
<dependency type="executable" location="extensions">simplestyle.py</dependency>

<param name="description" type="description" indent="-1">Select a set of objects and create a custom color palette.</param>
<param name="properties-title" type="description" appearance="header">Palette Properties</param>
<param name="name-label" type="description" indent="-1">Palette Name</param>
<param name="name" type="string" gui-text=""></param>
<param name="property-label" type="description" indent="-1">Color Property</param>
<param name="property" type="enum" gui-text="">
<item value="fill">Fill Color</item>
<item value="stroke">Stroke Color</item>
<item value="both">Both</item>
</param>
<param name="options-title" type="description" appearance="header">Options</param>
<param name="default" type="boolean" gui-text="Include default grays">false</param>
<param name="replace" type="boolean" gui-text="Replace existing palette">false</param>
<param name="help-text" type="description" indent="-1">ℹ️ Don't forget to restart Inkscape </param>

<label>Select a set of objects and create a custom color palette.</label>
<label appearance="header">Palette Properties</label>

<label>Palette Name</label>
<param name="name" type="string" gui-text=" "></param>

<vbox>
<label>Color Property</label>
<param name="property" type="optiongroup" appearance="combo" gui-text=" ">
<option value="fill">Fill Color</option>
<option value="stroke">Stroke Color</option>
<option value="both">Both</option>
</param>
</vbox>

<label appearance="header">Options</label>
<param name="default" type="bool" gui-text="Include default grays">false</param>
<param name="replace" type="bool" gui-text="Replace existing palette">false</param>

<spacer />

<hbox>
<image>info.svg</image>
<label>Don't forget to restart Inkscape</label>
</hbox>

<effect needs-live-preview="false">
<object-type>all</object-type>
Expand All @@ -29,6 +37,6 @@
</effect>

<script>
<command reldir="extensions" interpreter="python">generate_palette.py</command>
<command location="inx" interpreter="python">generate_palette.py</command>
</script>
</inkscape-extension>
52 changes: 35 additions & 17 deletions generate_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,46 @@
import os
import sys
import inkex
import simplestyle

__version__ = '2.0'

inkex.localize()

def log(text):
inkex.debug(text)
inkex.utils.debug(text)

def abort(text):
inkex.errormsg(_(text))
exit()


class GeneratePalette(inkex.Effect):

def __init__(self):
inkex.Effect.__init__(self)

self.OptionParser.add_option('-n', '--name', action='store', type='string', dest='name', help='Palette name')
self.OptionParser.add_option('-p', '--property', action='store', type='string', dest='property', help='Color property')
self.OptionParser.add_option('-d', '--default', action='store', type='inkbool', dest='default', help='Default grays')
self.OptionParser.add_option('-r', '--replace', action='store', type='inkbool', dest='replace', help='Replace existing')
self.arg_parser.add_argument(
'-n', '--name',
type=str,
dest='name',
help='Palette name'
)

self.arg_parser.add_argument(
'-p', '--property',
type=str,
dest='property',
help='Color property'
)

self.arg_parser.add_argument(
'-d', '--default',
type=inkex.Boolean,
dest='default',
help='Default grays'
)

self.arg_parser.add_argument(
'-r', '--replace',
type=inkex.Boolean,
dest='replace',
help='Replace existing'
)

def get_palettes_path(self):
if sys.platform.startswith('win'):
Expand Down Expand Up @@ -66,22 +83,24 @@ def get_default_colors(self):
return colors if self.options.default else []

def get_node_prop(self, node, property):
style = simplestyle.parseStyle(node.attrib['style'])
return style[property]
attr = node.attrib.get('style')
style = dict(inkex.Style.parse_str(attr))

return style.get(property, 'none')

def get_node_index(self, args):
id, node = args
return self.options.ids.index(id)

def get_formatted_color(self, color):
rgb = simplestyle.parseColor(color)
rgb = inkex.Color(color).to_rgb()
rgb = "{:3d} {:3d} {:3d}".format(*rgb)

return "%s %s" % (rgb, color)

def get_selected_colors(self):
colors = []
selected = list(self.selected.items())
selected = list(self.svg.selected.items())

selected.sort(key=self.get_node_index)

Expand Down Expand Up @@ -133,7 +152,6 @@ def effect(self):

self.write_palette()


if __name__ == '__main__':
palette = GeneratePalette()
palette.affect()
palette.run()
1 change: 1 addition & 0 deletions info.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9358db2

Please sign in to comment.