From 2235ec6571601e12be7eb3c74907668fb68bebd4 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Fri, 11 Jul 2025 22:14:52 +0200 Subject: 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) --- lib/extensions/break_apart.py | 2 +- lib/extensions/gradient_blocks.py | 16 ++++++++++++---- lib/extensions/params.py | 4 ++-- 3 files changed, 15 insertions(+), 7 deletions(-) (limited to 'lib/extensions') diff --git a/lib/extensions/break_apart.py b/lib/extensions/break_apart.py index 21806251..05f9375a 100644 --- a/lib/extensions/break_apart.py +++ b/lib/extensions/break_apart.py @@ -39,7 +39,7 @@ class BreakApart(InkstitchExtension): elements.append(EmbroideryElement(node)) for element in elements: - if not element.get_style("fill", "black"): + if not element.fill_color: continue # we don't want to touch valid elements diff --git a/lib/extensions/gradient_blocks.py b/lib/extensions/gradient_blocks.py index 79665a10..364ff2b2 100644 --- a/lib/extensions/gradient_blocks.py +++ b/lib/extensions/gradient_blocks.py @@ -5,8 +5,8 @@ from math import degrees, pi -from inkex import (DirectedLineSegment, Group, LinearGradient, Path, - PathElement, Transform, errormsg) +from inkex import (Color, ColorError, DirectedLineSegment, Group, + LinearGradient, Path, PathElement, Transform, errormsg) from shapely import geometry as shgeo from shapely.affinity import rotate from shapely.ops import split @@ -67,7 +67,7 @@ class GradientBlocks(InkstitchExtension): for i, shape in enumerate(fill_shapes): if shape.area < 15: continue - color = attributes[i]['color'] + color = verify_color(attributes[i]['color']) style['fill'] = color is_gradient = attributes[i]['is_gradient'] angle = degrees(attributes[i]['angle']) @@ -146,7 +146,7 @@ def gradient_shapes_and_attributes(element, shape, unit_multiplier): split_line = rotate(split_line, angle, origin=split_point, use_radians=True) offset_line = split_line.parallel_offset(1, 'right') polygon = split(shape, split_line) - color = stop_styles[i]['stop-color'] + color = verify_color(stop_styles[i]['stop-color']) # does this gradient line split the shape offset_outside_shape = len(polygon.geoms) == 1 for poly in polygon.geoms: @@ -178,6 +178,14 @@ def gradient_shapes_and_attributes(element, shape, unit_multiplier): return polygons, attributes +def verify_color(color): + try: + Color(color) + except ColorError: + return "black" + return color + + if __name__ == '__main__': e = GradientBlocks() e.run() diff --git a/lib/extensions/params.py b/lib/extensions/params.py index cb38585e..0bba13fe 100755 --- a/lib/extensions/params.py +++ b/lib/extensions/params.py @@ -721,9 +721,9 @@ class Params(InkstitchExtension): elif node.tag in EMBROIDERABLE_TAGS and not node.get_path(): pass else: - if element.get_style("fill", 'black') and not element.get_style("fill-opacity", 1) == "0": + if element.fill_color is not None and not element.get_style("fill-opacity", 1) == "0": classes.append(FillStitch) - if element.get_style("stroke") is not None: + if element.stroke_color is not None: classes.append(Stroke) if len(element.path) > 1: classes.append(SatinColumn) -- cgit v1.2.3