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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 |
class Colors:
"""A collection of named colors that acts like an array, a dictionary
and a namespace.
`Colors` is indexed by a type `ColorKey`, which may be an
`int`, `slice`, `str`, `Color`, or`Tuple[int, int, int]]`.
"""
def __init__(self, *palettes, canonicalize_gray='gray', default='black'):
from . import color
class Color(color.Color):
COLORS = self
self.__dict__['Color'] = Color
self._canonicalize_gray = canonicalize_gray
self._name_to_rgb = {}
self._rgb_to_name = {}
self._palettes = [self._add_palette(s) for s in palettes]
self._canonical_to_rgb = {
self._canonical_name(k): v for k, v in self._name_to_rgb.items()
}
self._default = self.get(str(default)) or next(iter(self._rgb_to_name))
def get(self, key: ColorKey, default: Optional[Color] = None):
"""Return the value for `key` if it exists, else `default`."""
try:
return self[key]
except KeyError:
return default
def items(self) -> Iterator[Tuple[str, Color]]:
"""Return an iterator of name, Color pairs. Some colors might repeat"""
return self._colors.items()
def values(self) -> Iterator[Color]:
"""Return an iterator of Colors. Some colors might repeat"""
return self._colors.values()
def keys(self) -> Iterator[str]:
"""Return an iterator of strings, unique color names"""
return self._colors.keys()
def closest(self, color: Color) -> Color:
"""
Return the closest named color to `color`. This can be quite slow,
particularly if there are many colors.
"""
if isinstance(color, list):
color = tuple(color)
if color in self._rgb_to_name:
return color
return min((c.distance2(color), c) for c in self.values())[1]
def __call__(self, *args, **kwds):
return self.Color(*args, **kwds)
def __getitem__(self, name: ColorKey) -> Color:
"""Try to convert string item into a color"""
if isinstance(name, (int, slice)):
return self._color_list[name]
if isinstance(name, tuple):
return self.Color(*name)
canonical = self._canonical_name(name)
try:
return self._canonical_to_rgb[canonical]
except KeyError:
pass
raise KeyError(name)
def __setitem__(self, name, rgb):
raise KeyError(name)
def __contains__(self, x: ColorKey) -> bool:
"""Return true if this ColorKey appears in the table canonically"""
return self._canonical_name(x) in self._canonical_to_rgb
def __getattr__(self, name: str) -> Color:
if name.startswith('_'):
return super().__getattribute__(name)
try:
return self[name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
if name.startswith('_'):
return super().__setattr__(name, value)
raise AttributeError(name)
def __len__(self) -> int:
return len(self._color_list)
def __iter__(self) -> Iterator[Color]:
return iter(self._color_list)
def __eq__(self, x) -> bool:
return __class__ == x.__class__ and self._name_to_rgb == x._name_to_rgb
def __ne__(self, x) -> bool:
return not (self == x)
def __add__(self, x):
cg, d = self._canonicalize_gray, self._default
c = x if isinstance(x, __class__) else __class__(x)
palettes = self._palettes + c._palettes
return __class__(*palettes, canonicalize_gray=cg, default=d)
def __radd__(self, x):
other = __class__(
x, canonicalize_gray=self._canonicalize_gray, default=self._default
)
return other + self
def _add_palette(self, palette):
if isinstance(palette, str):
if '.' not in palette:
palette = '.' + palette
if palette.startswith('.'):
palette = 'nc.palette' + palette
palette = importlib.import_module(palette)
if not isinstance(palette, dict):
palette = palette.__dict__
if 'COLORS' in palette:
colors = palette['COLORS']
primary_names = palette.get('PRIMARY_NAMES', ())
else:
colors = palette
palette = {'COLORS': palette}
primary_names = ()
colors = {k: self.Color(v) for k, v in colors.items()}
if not palette.get('PRESERVE_CAPITALIZATION'):
colors = {k.capitalize(): v for k, v in colors.items()}
for sub, rep in self._replacements:
colors = {sub(rep, k): v for k, v in colors.items()}
self._name_to_rgb.update(colors)
def best_name(names):
names.sort(key=lambda n: (len(n), n.lower()))
pnames = (n for n in names if n in primary_names)
return next(pnames, names[0])
names = {}
for n, c in colors.items():
names.setdefault(c, []).append(n)
self._rgb_to_name.update((k, best_name(v)) for k, v in names.items())
return palette
def _canonical_name(self, name):
name = name.lower()
if self._canonicalize_gray:
name = name.replace('grey', 'gray')
return ''.join(i for i in name if i in _ALLOWED)
@cached_property
def _colors(self):
return {k: self.Color(*v) for k, v in self._name_to_rgb.items()}
@cached_property
def _color_list(self):
return list(self._colors.values())
@cached_property
def _replacements(self):
if not (gt := self._canonicalize_gray):
return ()
gt = 'gray' if gt is True else gt.lower()
gf = 'grey' if gt == 'gray' else 'gray'
if gt not in ('gray', 'grey'):
raise ValueError('Don\'t understand canonicalize_gray=%s' % gt)
regular = re.compile(r'\b%s\b' % gf).sub, gt
upper = re.compile(r'\b%s\b' % gf.capitalize()).sub, gt.capitalize()
return regular, upper
|