summaryrefslogtreecommitdiffstats
path: root/share/palettes/soc2gpl.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:29:01 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 16:29:01 +0000
commit35a96bde514a8897f6f0fcc41c5833bf63df2e2a (patch)
tree657d15a03cc46bd099fc2c6546a7a4ad43815d9f /share/palettes/soc2gpl.py
parentInitial commit. (diff)
downloadinkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.tar.xz
inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.zip
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'share/palettes/soc2gpl.py')
-rwxr-xr-xshare/palettes/soc2gpl.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/share/palettes/soc2gpl.py b/share/palettes/soc2gpl.py
new file mode 100755
index 0000000..c159d7e
--- /dev/null
+++ b/share/palettes/soc2gpl.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+#
+# Copyright 2018 (c) Martin Owens
+#
+"""
+Convert LibreOffice SOC Palette into a GIMP Palette file
+"""
+
+import os
+import sys
+import logging
+
+from argparse import ArgumentParser
+
+import lxml.etree
+
+def write_palette(nom, filename, colors):
+ """Write colours to a GIMP palette format"""
+ print('''GIMP Palette
+Name: {name}
+Columns: 3
+# generated by {prog}
+# original file {filename}'''.format(name=nom, filename=filename, prog=sys.argv[0]))
+ for color_name, color in colors:
+ print("{red: >3} {green: >3} {blue: >3}\t{name}".format(red=color[0], green=color[1], blue=color[2], name=color_name))
+
+def process_soc(filename):
+ """Generator that returns each colour as a tuple of colours and names"""
+ with open(filename, 'r') as fhl:
+ doc = lxml.etree.parse(fhl)
+ root = doc.getroot()
+ for color in root:
+ attr = dict([(attr.split('}')[-1], value) for attr, value in color.attrib.items()])
+ if 'name' in attr and 'color' in attr:
+ color = attr['color']
+ color = (int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16))
+ yield (attr['name'], color)
+
+def main():
+ """Main"""
+ parser = ArgumentParser(description=__doc__)
+ parser.add_argument('-n', '--name', help='Name of the palette', default=None)
+ parser.add_argument('socfile', help='The soc file to convert')
+ arg = parser.parse_args()
+
+ if not os.path.isfile(arg.socfile):
+ logging.error("Cannot find file %s", arg.socfile)
+ sys.exit(1)
+
+ if arg.name:
+ name = arg.name
+ else:
+ name = os.path.basename(arg.socfile).rsplit('.')[0]
+
+ write_palette(name, arg.socfile, process_soc(arg.socfile))
+
+if __name__ == '__main__':
+ main()