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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
import colorsys
import re
from inkex import Color
from pyembroidery.EmbThread import EmbThread
class ThreadColor(object):
hex_str_re = re.compile('#([0-9a-z]{3}|[0-9a-z]{6})', re.I)
def __init__(self, color, name=None, number=None, manufacturer=None, description=None, chart=None):
'''
avoid error messages:
* set colors with a gradient to black
* currentColor/context-fill/context-stroke: should not just be black, but we want to avoid
error messages until inkex will be able to handle these css properties
'''
if isinstance(color, str) and color.startswith(('url', 'currentColor', 'context')):
color = None
elif isinstance(color, str) and color.startswith('rgb'):
color = tuple(int(value) for value in color[4:-1].split(','))
if color is None:
self.rgb = (0, 0, 0)
elif isinstance(color, EmbThread):
self.name = color.description
self.number = color.catalog_number
self.manufacturer = color.brand
self.description = color.description
self.chart = color.chart
self.rgb = (color.get_red(), color.get_green(), color.get_blue())
return
elif isinstance(color, str):
self.rgb = Color.parse_str(color)[1]
elif isinstance(color, (list, tuple)):
self.rgb = tuple(color)
elif self.hex_str_re.match(color):
self.rgb = Color.parse_str(color)[1]
else:
raise ValueError("Invalid color: " + repr(color))
self.name = name
self.number = number
self.manufacturer = manufacturer
self.description = description
self.chart = chart
def __json__(self):
jsonified = self._as_dict()
jsonified["visible_on_white"] = self.visible_on_white._as_dict()
return jsonified
def _as_dict(self):
return dict(name=self.name,
number=self.number,
manufacturer=self.manufacturer,
description=self.description,
chart=self.chart,
rgb=self.rgb,
hex=self.to_hex_str(),
)
def __eq__(self, other):
if isinstance(other, ThreadColor):
return self.rgb == other.rgb
else:
return self == ThreadColor(other)
def __hash__(self):
return hash(self.rgb)
def __ne__(self, other):
return not (self == other)
def __repr__(self):
return "ThreadColor" + repr(self.rgb)
def to_hex_str(self):
return "#%s" % self.hex_digits
@property
def pyembroidery_thread(self):
return {
"name": self.name,
"id": self.number,
"manufacturer": self.manufacturer,
"description": self.description,
"chart": self.chart,
"rgb": int(self.hex_digits, 16),
}
@property
def hex_digits(self):
return "%02X%02X%02X" % tuple([int(x) for x in self.rgb])
@property
def rgb_normalized(self):
return tuple(channel / 255.0 for channel in self.rgb)
@property
def font_color(self):
"""Pick a color that will allow text to show up on a swatch in the printout."""
hls = colorsys.rgb_to_hls(*self.rgb_normalized)
# We'll use white text unless the swatch color is too light.
if hls[1] > 0.7:
return (1, 1, 1)
else:
return (254, 254, 254)
@property
def visible_on_white(self):
"""A ThreadColor similar to this one but visible on white.
If the thread color is white, we don't want to try to draw white in the
simulation view or print white in the print-out. Choose a color that's
as close as possible to the actual thread color but is still at least
somewhat visible on a white background.
"""
hls = list(colorsys.rgb_to_hls(*self.rgb_normalized))
# Capping lightness should make the color visible without changing it
# too much.
if hls[1] > 0.85:
hls[1] = 0.85
color = colorsys.hls_to_rgb(*hls)
# convert back to values in the range of 0-255
color = tuple(value * 255 for value in color)
return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer, description=self.description, chart=self.chart)
def visible_on_background(self, background_color):
"""A ThreadColor similar to this one but visible on given background color.
Choose a color that's as close as possible to the actual thread color but is still at least
somewhat visible on given background.
"""
hls = list(colorsys.rgb_to_hls(*self.rgb_normalized))
background = ThreadColor(background_color)
background_hls = list(colorsys.rgb_to_hls(*background.rgb_normalized))
difference = hls[1] - background_hls[1]
if abs(difference) < 0.1:
if hls[1] > 0.5:
hls[1] -= 0.1
else:
hls[1] += 0.1
color = colorsys.hls_to_rgb(*hls)
# convert back to values in the range of 0-255
color = tuple(value * 255 for value in color)
return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer,
description=self.description, chart=self.chart)
return self
@property
def darker(self):
hls = list(colorsys.rgb_to_hls(*self.rgb_normalized))
# Capping lightness should make the color visible without changing it
# too much.
hls[1] *= 0.75
color = colorsys.hls_to_rgb(*hls)
# convert back to values in the range of 0-255
color = tuple(value * 255 for value in color)
return ThreadColor(color, name=self.name, number=self.number, manufacturer=self.manufacturer, description=self.description, chart=self.chart)
|