diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2025-07-11 22:14:52 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-11 22:14:52 +0200 |
| commit | 2235ec6571601e12be7eb3c74907668fb68bebd4 (patch) | |
| tree | 61d85cb615a1b2327283e89f09592e1eb85ad91c /tests | |
| parent | 7bd72274780ccaeae8bbae6e761341079df21ecb (diff) | |
Fix issue with bad color names (#3816)
* fix issue with bad color names and define element colors at one place and reuse
* fix bad tartan color
* verify color in gradient block
* add thread color tests
* use default color behavior for elements linked to non-existing definitions (gradients)
* Added mypy change for tests (authored by: CapellanCitizen)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_threads_color.py | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/test_threads_color.py b/tests/test_threads_color.py index a5442359..075312e6 100644 --- a/tests/test_threads_color.py +++ b/tests/test_threads_color.py @@ -1,3 +1,8 @@ +from inkex import LinearGradient, Rectangle, Stop, SvgDocumentElement +from inkex.tester.svg import svg + +from lib.elements import EmbroideryElement +from lib.elements.utils import node_to_elements from lib.threads.color import ThreadColor @@ -14,3 +19,123 @@ def test_init_color_from_string_hex(): def test_init_color_from_string_hex_icc(): color = ThreadColor("#AABBCC icc-color(Some-Profile, 0.1, 0.2, 0.3, 0.4)") assert color.rgb == (170, 187, 204) + + +def test_invalid_color(): + # defaults to black + color = ThreadColor("bad_color") + assert color.rgb == (0, 0, 0) + + +def test_fill_color(): + root: SvgDocumentElement = svg() + rect = Rectangle(attrib={ + "width": "10", + "height": "10", + "style": "fill:red;" + }) + root.add(rect) + + # test with red color + element = EmbroideryElement(rect) + assert element.fill_color == [255, 0, 0] + + # test with invalid color (defaults to black) + rect.style = "fill:bad_color;" + element = EmbroideryElement(rect) + assert element.fill_color == "black" + + +def test_stroke_color(): + root: SvgDocumentElement = svg() + rect = Rectangle(attrib={ + "width": "10", + "height": "10", + "style": "fill:none;stroke:red;" + }) + root.add(rect) + + # test with red color + element = EmbroideryElement(rect) + assert element.stroke_color == [255, 0, 0] + + # test with invalid color + rect.style = "fill:none;stroke:bad_color;" + element = EmbroideryElement(rect) + assert element.stroke_color is None + + +def test_gradient_colors(): + root: SvgDocumentElement = svg() + + defs = root.defs + linear_gradient = LinearGradient( + attrib={ + "id": "good_gradient" + } + ) + stop1 = Stop( + attrib={ + "style": "stop-color: #ff0000;" + } + ) + stop2 = Stop( + attrib={ + "style": "stop-color: bad_color;" + } + ) + linear_gradient.add(stop1) + linear_gradient.add(stop2) + defs.add(linear_gradient) + + rect = Rectangle(attrib={ + "width": "10", + "height": "10", + "style": "fill:url(#good_gradient)" + }) + rect.set("inkstitch:fill_method", "linear_gradient_fill") + root.add(rect) + + [element] = node_to_elements(root[1]) + stitch_groups = element.embroider(None) + + assert stitch_groups[0].color == [255, 0, 0] + assert stitch_groups[1].color == [0, 0, 0] + + +def test_tartan_colors(): + root: SvgDocumentElement = svg() + rect = Rectangle(attrib={ + "width": "20", + "height": "20", + }) + root.add(rect) + + rect.set('inkstitch:fill_method', 'tartan_fill') + rect.set('inkstitch:fill_underlay', False) + rect.set( + 'inkstitch:tartan', + '{"symmetry": true, "equal_warp_weft": true, "rotate": 0.0, "scale": 100, "offset_x": 0.0, "offset_y": 0.0,' + '"palette": "(#ffff00)/5.0 (#00ffff)/5.0", "output": "embroidery", "stitch_type": "legacy_fill",' + '"row_spacing": 1.0, "angle_warp": 0.0, "angle_weft": 90.0, "min_stripe_width": 1.0, "bean_stitch_repeats": 0}' + ) + + [element] = node_to_elements(root[0]) + stitch_groups = element.embroider(None) + + assert stitch_groups[0].color == 'yellow' + assert stitch_groups[1].color == 'cyan' + + # Set second color to an invalid value. Tartan will disable the color stripe for rendering. + rect.set( + 'inkstitch:tartan', + '{"symmetry": true, "equal_warp_weft": true, "rotate": 0.0, "scale": 100, "offset_x": 0.0, "offset_y": 0.0,' + '"palette": "(#ffff00)/5.0 (bad_color)/5.0", "output": "embroidery", "stitch_type": "legacy_fill",' + '"row_spacing": 1.0, "angle_warp": 0.0, "angle_weft": 90.0, "min_stripe_width": 1.0, "bean_stitch_repeats": 0}' + ) + + [element] = node_to_elements(root[0]) + stitch_groups = element.embroider(None) + + assert stitch_groups[0].color == 'yellow' + assert len(stitch_groups) == 1 |
