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/gradient_blocks.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'lib/extensions/gradient_blocks.py') 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() -- cgit v1.2.3