kwimage.im_color

Module Contents

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

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)

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)
_forimage(self, image, space='rgb')

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)
as255(self, space=None)
as01(self, space=None)

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

classmethod _is_base01(channels)

check if a color is in base 01

classmethod _is_base255(Color, channels)

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

classmethod _hex_to_01(Color, hex_color)

hex_color = ‘#6A5AFFAF’

_ensure_color01(Color, color)

Infer what type color is and normalize to 01

classmethod _255_to_01(Color, color255)

converts base 255 color to base 01 color

classmethod _string_to_01(Color, color)

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

classmethod named_colors(cls)
Returns:names of colors that Color accepts
Return type:List[str]
classmethod distinct(Color, num, space='rgb')

Make multiple distinct colors

classmethod random(Color, pool='named')
kwimage.im_color.BASE_COLORS
kwimage.im_color.TABLEAU_COLORS = [['blue', '#1f77b4'], ['orange', '#ff7f0e'], ['green', '#2ca02c'], ['red', '#d62728'], ['purple', '#9467bd'], ['brown', '#8c564b'], ['pink', '#e377c2'], ['gray', '#7f7f7f'], ['olive', '#bcbd22'], ['cyan', '#17becf']]
kwimage.im_color.TABLEAU_COLORS
kwimage.im_color.XKCD_COLORS
kwimage.im_color.XKCD_COLORS
kwimage.im_color.CSS4_COLORS