summaryrefslogtreecommitdiff
path: root/lib/extensions/apply_palette.py
blob: ce6c8f5c174b94c557557f91d09a3cbaa592866c (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
# Authors: see git history
#
# Copyright (c) 2024 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

from ..elements import FillStitch
from ..threads import ThreadCatalog, ThreadColor
from .base import InkstitchExtension


class ApplyPalette(InkstitchExtension):
    '''
    Applies colors of a color palette to elements
    '''
    def __init__(self, *args, **kwargs):
        InkstitchExtension.__init__(self, *args, **kwargs)
        self.arg_parser.add_argument("-o", "--tabs")
        self.arg_parser.add_argument("-t", "--palette", type=str, default=None, dest="palette")

    def effect(self):
        # Remove selection, we want all the elements in the document
        self.svg.selection.clear()

        if not self.get_elements():
            return

        palette_name = self.options.palette
        palette = ThreadCatalog().get_palette_by_name(palette_name)

        # Iterate through the color blocks to apply colors
        for element in self.elements:
            nearest_color = palette.nearest_color(ThreadColor(element.color))
            if isinstance(element, FillStitch):
                element.node.style['fill'] = nearest_color.to_hex_str()
            else:
                element.node.style['stroke'] = nearest_color.to_hex_str()

        metadata = self.get_inkstitch_metadata()
        metadata['thread-palette'] = palette_name