diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2024-03-11 14:08:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-11 14:08:56 +0100 |
| commit | 4749eca8fdfe1b3fe362cacd409147ff89f35629 (patch) | |
| tree | 9536e6f9177086289d5c921a769e1ec7dc3d805e /lib/extensions/apply_palette.py | |
| parent | 3121bbaedf7e76d92a029fad067828014f9f5fde (diff) | |
Add apply palette extension (#2738)
* add apply palette extension
* thread catalog: apply palette: do not overwrite cutwork settings
Diffstat (limited to 'lib/extensions/apply_palette.py')
| -rw-r--r-- | lib/extensions/apply_palette.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/extensions/apply_palette.py b/lib/extensions/apply_palette.py new file mode 100644 index 00000000..ce6c8f5c --- /dev/null +++ b/lib/extensions/apply_palette.py @@ -0,0 +1,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 |
