Add apply palette extension (#2738)

* add apply palette extension
* thread catalog: apply palette: do not overwrite cutwork settings
pull/2776/head
Kaalleen 2024-03-11 14:08:56 +01:00 zatwierdzone przez GitHub
rodzic 3121bbaedf
commit 4749eca8fd
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
4 zmienionych plików z 81 dodań i 5 usunięć

Wyświetl plik

@ -3,8 +3,7 @@
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
from lib.extensions.troubleshoot import Troubleshoot
from .apply_palette import ApplyPalette
from .apply_threadlist import ApplyThreadlist
from .auto_run import AutoRun
from .auto_satin import AutoSatin
@ -30,6 +29,7 @@ from .install_custom_palette import InstallCustomPalette
from .jump_to_stroke import JumpToStroke
from .layer_commands import LayerCommands
from .lettering import Lettering
from .lettering_along_path import LetteringAlongPath
from .lettering_custom_font_dir import LetteringCustomFontDir
from .lettering_force_lock_stitches import LetteringForceLockStitches
from .lettering_generate_json import LetteringGenerateJson
@ -55,13 +55,13 @@ from .stitch_plan_preview import StitchPlanPreview
from .stitch_plan_preview_undo import StitchPlanPreviewUndo
from .stroke_to_lpe_satin import StrokeToLpeSatin
from .test_swatches import TestSwatches
from .troubleshoot import Troubleshoot
from .update_svg import UpdateSvg
from .zigzag_line_to_satin import ZigzagLineToSatin
from .zip import Zip
from.lettering_along_path import LetteringAlongPath
__all__ = extensions = [ApplyThreadlist,
__all__ = extensions = [ApplyPalette,
ApplyThreadlist,
AutoRun,
AutoSatin,
BreakApart,

Wyświetl plik

@ -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

Wyświetl plik

@ -97,6 +97,11 @@ class _ThreadCatalog(Sequence):
return palette
def apply_palette(self, stitch_plan, palette):
for color_block in stitch_plan:
if color_block.color.chart:
# do not overwrite cutwork settings
continue
for color_block in stitch_plan:
nearest = palette.nearest_color(color_block.color)

Wyświetl plik

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension translationdomain="inkstitch" xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Apply Palette</name>
<id>org.{{ id_inkstitch }}.apply_palette</id>
<param name="extension" type="string" gui-hidden="true">apply_palette</param>
<param name="tabs" type="notebook">
<page name="options" gui-text="Options">
<param name="palette" type="optiongroup" appearance="combo" gui-text="Select color palette">
{%- for item in threadcatalog %}
<item value="{{ item }}">{{ item }}</item>
{%- endfor %}
</param>
</page>
<page name="info" gui-text="Help">
<label>This extension applies nearest colors from chosen color palette to the elements in this document.</label>
<spacer />
<label>Get more information on our website</label>
<label appearance="url">https://inkstitch.org/docs/thread-color/#apply-palette</label>
</page>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="{{ menu_inkstitch }}" translatable="no">
<submenu name="Thread Color Management" />
</submenu>
</effects-menu>
</effect>
<script>
{{ command_tag | safe }}
</script>
</inkscape-extension>