kwplot.mpl_color module¶
DEPRECATED: use kwimage.Color instead
- class kwplot.mpl_color.Color(color, alpha=None, space=None)[source]¶
Bases:
NiceRepr
Used for converting a single color between spaces and encodings. This should only be used when handling small numbers of colors(e.g. 1), don’t use this to represent an image.
move to colorutil?
- Parameters
space (str) – colorspace of wrapped color. Assume RGB if not specified and it cannot be inferred
CommandLine
xdoctest -m ~/code/kwimage/kwimage/im_color.py Color
Example
>>> print(Color('g')) >>> print(Color('orangered')) >>> print(Color('#AAAAAA').as255()) >>> print(Color([0, 255, 0])) >>> print(Color([1, 1, 1.])) >>> print(Color([1, 1, 1])) >>> print(Color(Color([1, 1, 1])).as255()) >>> print(Color(Color([1., 0, 1, 0])).ashex()) >>> print(Color([1, 1, 1], alpha=255)) >>> print(Color([1, 1, 1], alpha=255, space='lab'))
- classmethod named_colors()[source]¶
- Returns
names of colors that Color accepts
- Return type
List[str]
Example
>>> import kwimage >>> named_colors = kwimage.Color.named_colors() >>> color_lut = {name: kwimage.Color(name).as01() for name in named_colors} >>> # xdoctest: +REQUIRES(module:kwplot) >>> import kwplot >>> kwplot.autompl() >>> canvas = kwplot.make_legend_img(color_lut) >>> kwplot.imshow(canvas)
- classmethod distinct(num, existing=None, space='rgb', legacy='auto', exclude_black=True, exclude_white=True)[source]¶
Make multiple distinct colors
References
https://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors
Example
>>> # xdoctest: +REQUIRES(module:matplotlib) >>> from kwimage.im_color import * # NOQA >>> from kwimage.im_color import _draw_color_swatch >>> import kwimage >>> colors1 = kwimage.Color.distinct(10, legacy=False) >>> swatch1 = _draw_color_swatch(colors1, cellshape=9) >>> colors2 = kwimage.Color.distinct(10, existing=colors1) >>> swatch2 = _draw_color_swatch(colors1 + colors2, cellshape=9) >>> # xdoctest: +REQUIRES(module:kwplot) >>> import kwplot >>> kwplot.autompl() >>> kwplot.imshow(swatch1, pnum=(1, 2, 1), fnum=1) >>> kwplot.imshow(swatch2, pnum=(1, 2, 2), fnum=1)
- distance(other, space='lab')[source]¶
Distance between self an another color
Example
import kwimage self = kwimage.Color((0.16304347826086973, 0.0, 1.0)) other = kwimage.Color(‘purple’)
- hard_coded_colors = {
‘a’: (1.0, 0.0, 0.16), ‘b’: (1.0, 0.918918918918919, 0.0), ‘c’: (0.0, 1.0, 0.0), ‘d’: (0.0, 0.9239130434782604, 1.0), ‘e’: (0.16304347826086973, 0.0, 1.0)
}
# Find grays names = kwimage.Color.named_colors() grays = {} for name in names:
color = kwimage.Color(name) r, g, b = color.as01() if r == g and g == b:
grays[name] = (r, g, b)
print(ub.repr2(ub.sorted_vals(grays), nl=-1))
- for k, v in hard_coded_colors.items():
self = kwimage.Color(v) distances = [] for name in names:
other = kwimage.Color(name) dist = self.distance(other) distances.append(dist)
idxs = ub.argsort(distances)[0:5] dists = list(ub.take(distances, idxs)) names = list(ub.take(names, idxs)) print(‘k = {!r}’.format(k)) print(‘names = {!r}’.format(names)) print(‘dists = {!r}’.format(dists))