1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
from inkex import LinearGradient, Rectangle, Stop, SvgDocumentElement
from inkex.tester.svg import svg
from lib.elements import EmbroideryElement
from lib.elements import node_to_elements
from lib.threads.color import ThreadColor
def test_init_color_from_string_rgb():
color = ThreadColor("rgb(170, 187, 204)")
assert color.rgb == (170, 187, 204)
def test_init_color_from_string_hex():
color = ThreadColor("#AABBCC")
assert color.rgb == (170, 187, 204)
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
|