summaryrefslogtreecommitdiff
path: root/lib/extensions/generate_palette.py
blob: b87bc1793b38f561ca7ef239fd3c161ba97c508f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Authors: see git history
#
# Copyright (c) 2022 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

import os

import inkex

from ..i18n import _
from ..utils import guess_inkscape_config_path
from .base import InkstitchExtension


class GeneratePalette(InkstitchExtension):
    # Generate a custom color palette in object related order
    def __init__(self, *args, **kwargs):
        InkstitchExtension.__init__(self, *args, **kwargs)
        self.arg_parser.add_argument("-n", "--palette_name", type=str, default=None, dest="palette_name")
        self.arg_parser.add_argument("-f", "--palette_folder", type=str, default=None, dest="palette_folder")
        self.arg_parser.add_argument("-o", "--options", type=str, default=None, dest="page_options")
        self.arg_parser.add_argument("-i", "--info", type=str, default=None, dest="page_help")

    def effect(self):
        path = self.options.palette_folder
        brand = self.options.palette_name
        file_name = "InkStitch %s.gpl" % brand
        color_palette_name = '\nName: Ink/Stitch: %s' % brand

        if not brand:
            inkex.errormsg(_("Please specify a name for your color palette."))
            return

        if path:
            if not os.path.isdir(path):
                inkex.errormsg(_("Unkown directory path."))
                return
        else:
            path = os.path.join(guess_inkscape_config_path(), 'palettes')
            if not os.path.isdir(path):
                inkex.errormsg(_("Ink/Stitch cannot find your palette folder automatically. Please enter the path manually."))
                return

        elements = self.svg.selection.rendering_order()

        if not elements:
            inkex.errormsg(_("No element selected.\n\nPlease select at least one text element with a fill color."))
            return

        colors = self._get_color_from_elements(elements)

        if not colors:
            inkex.errormsg(_("We couldn't find any fill colors on your text elements. Please read the instructions on our website."))
            return

        colors = ['GIMP Palette', color_palette_name, '\nColumns: 4', '\n# RGB Value\t                    Color Name   Number'] + colors

        file_path = os.path.join(path, file_name)
        with open(file_path, 'w', encoding='utf-8') as gpl:
            gpl.writelines(colors)

    def _get_color_from_elements(self, elements):
        colors = []
        for element in elements:
            if element.TAG == 'g':
                colors.extend(self._get_color_from_elements(element.getchildren()))
                continue
            if 'fill' not in element.style.keys() or not isinstance(element, inkex.TextElement):
                continue

            color = inkex.Color(element.style['fill']).to_rgb()
            color_name = element.get_text().split(' ')
            if len(color_name) > 1 and color_name[-1].isdigit():
                number = color_name[-1]
                name = ' '.join(color_name[:-1])
            else:
                number = 0
                name = ' '.join(color_name)
            color = "\n%s\t%s\t%s\t%s    %s" % (str(color[0]).rjust(3), str(color[1]).rjust(3), str(color[2]).rjust(3), name.rjust(30), number)
            colors.append(color)

        return colors


if __name__ == '__main__':
    e = GeneratePalette()
    e.affect()