kwimage.im_color

Module Contents

Classes

Color

Used for converting a single color between spaces and encodings.

Functions

_lookup_colorspace_object(space)

_colormath_convert(src_color, src_space, dst_space)

Uses colormath to convert colors

Attributes

BASE_COLORS

TABLEAU_COLORS

TABLEAU_COLORS

XKCD_COLORS

XKCD_COLORS

CSS4_COLORS

kwimage.im_color._lookup_colorspace_object(space)[source]
kwimage.im_color._colormath_convert(src_color, src_space, dst_space)[source]

Uses colormath to convert colors

Example

>>> # xdoctest: +REQUIRES(module:colormath)
>>> import kwimage
>>> src_color = kwimage.Color('turquoise').as01()
>>> print('src_color = {}'.format(ub.repr2(src_color, nl=0, precision=2)))
>>> src_space = 'rgb'
>>> dst_space = 'lab'
>>> lab_color = _colormath_convert(src_color, src_space, dst_space)
>>> print('lab_color = {}'.format(ub.repr2(lab_color, nl=0, precision=2)))
lab_color = (78.11, -70.09, -9.33)
>>> rgb_color = _colormath_convert(lab_color, 'lab', 'rgb')
>>> print('rgb_color = {}'.format(ub.repr2(rgb_color, nl=0, precision=2)))
rgb_color = (0.29, 0.88, 0.81)
>>> hsv_color = _colormath_convert(lab_color, 'lab', 'hsv')
>>> print('hsv_color = {}'.format(ub.repr2(hsv_color, nl=0, precision=2)))
hsv_color = (175.39, 1.00, 0.88)
class kwimage.im_color.Color(color, alpha=None, space=None)[source]

Bases: ubelt.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'))
__nice__(self)[source]
_forimage(self, image, space='rgb')[source]

Experimental function.

Create a numeric color tuple that agrees with the format of the input image (i.e. float or int, with 3 or 4 channels).

Parameters
  • image (ndarray) – image to return color for

  • space (str, default=rgb) – colorspace of the input image.

Example

>>> img_f3 = np.zeros([8, 8, 3], dtype=np.float32)
>>> img_u3 = np.zeros([8, 8, 3], dtype=np.uint8)
>>> img_f4 = np.zeros([8, 8, 4], dtype=np.float32)
>>> img_u4 = np.zeros([8, 8, 4], dtype=np.uint8)
>>> Color('red')._forimage(img_f3)
(1.0, 0.0, 0.0)
>>> Color('red')._forimage(img_f4)
(1.0, 0.0, 0.0, 1.0)
>>> Color('red')._forimage(img_u3)
(255, 0, 0)
>>> Color('red')._forimage(img_u4)
(255, 0, 0, 255)
>>> Color('red', alpha=0.5)._forimage(img_f4)
(1.0, 0.0, 0.0, 0.5)
>>> Color('red', alpha=0.5)._forimage(img_u4)
(255, 0, 0, 127)
ashex(self, space=None)[source]
as255(self, space=None)[source]
as01(self, space=None)[source]

self = mplutil.Color(‘red’) mplutil.Color(‘green’).as01(‘rgba’)

classmethod _is_base01(channels)[source]

check if a color is in base 01

classmethod _is_base255(Color, channels)[source]

there is a one corner case where all pixels are 1 or less

classmethod _hex_to_01(Color, hex_color)[source]

hex_color = ‘#6A5AFFAF’

_ensure_color01(Color, color)[source]

Infer what type color is and normalize to 01

classmethod _255_to_01(Color, color255)[source]

converts base 255 color to base 01 color

classmethod _string_to_01(Color, color)[source]

mplutil.Color._string_to_01(‘green’) mplutil.Color._string_to_01(‘red’)

classmethod named_colors(cls)[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(Color, num, space='rgb')[source]

Make multiple distinct colors

classmethod random(Color, pool='named')[source]
kwimage.im_color.BASE_COLORS[source]
kwimage.im_color.TABLEAU_COLORS = [['blue', '#1f77b4'], ['orange', '#ff7f0e'], ['green', '#2ca02c'], ['red', '#d62728'],...[source]
kwimage.im_color.TABLEAU_COLORS[source]
kwimage.im_color.XKCD_COLORS[source]
kwimage.im_color.XKCD_COLORS[source]
kwimage.im_color.CSS4_COLORS[source]