kwimage.im_color
¶
Module Contents¶
Classes¶
Used for converting a single color between spaces and encodings. |
Functions¶
|
|
|
Uses colormath to convert colors |
Attributes¶
- 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'))
- _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)
- classmethod _is_base255(Color, channels)[source]¶
there is a one corner case where all pixels are 1 or less
- 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)