:py:mod:`kwimage.im_color` ========================== .. py:module:: kwimage.im_color Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: kwimage.im_color.Color Functions ~~~~~~~~~ .. autoapisummary:: kwimage.im_color._lookup_colorspace_object kwimage.im_color._colormath_convert Attributes ~~~~~~~~~~ .. autoapisummary:: kwimage.im_color.BASE_COLORS kwimage.im_color.TABLEAU_COLORS kwimage.im_color.TABLEAU_COLORS kwimage.im_color.XKCD_COLORS kwimage.im_color.XKCD_COLORS kwimage.im_color.CSS4_COLORS .. py:function:: _lookup_colorspace_object(space) .. py:function:: _colormath_convert(src_color, src_space, dst_space) Uses colormath to convert colors .. rubric:: 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) .. py:class:: Color(color, alpha=None, space=None) Bases: :py:obj:`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 .. rubric:: 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')) .. py:method:: __nice__(self) .. py:method:: _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. .. rubric:: 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) .. py:method:: ashex(self, space=None) .. py:method:: as255(self, space=None) .. py:method:: as01(self, space=None) self = mplutil.Color('red') mplutil.Color('green').as01('rgba') .. py:method:: _is_base01(channels) :classmethod: check if a color is in base 01 .. py:method:: _is_base255(Color, channels) :classmethod: there is a one corner case where all pixels are 1 or less .. py:method:: _hex_to_01(Color, hex_color) :classmethod: hex_color = '#6A5AFFAF' .. py:method:: _ensure_color01(Color, color) Infer what type color is and normalize to 01 .. py:method:: _255_to_01(Color, color255) :classmethod: converts base 255 color to base 01 color .. py:method:: _string_to_01(Color, color) :classmethod: mplutil.Color._string_to_01('green') mplutil.Color._string_to_01('red') .. py:method:: named_colors(cls) :classmethod: :returns: names of colors that Color accepts :rtype: List[str] .. rubric:: 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) .. py:method:: distinct(Color, num, space='rgb') :classmethod: Make multiple distinct colors .. py:method:: random(Color, pool='named') :classmethod: .. py:data:: BASE_COLORS .. py:data:: TABLEAU_COLORS :annotation: = [['blue', '#1f77b4'], ['orange', '#ff7f0e'], ['green', '#2ca02c'], ['red', '#d62728'],... .. py:data:: TABLEAU_COLORS .. py:data:: XKCD_COLORS .. py:data:: XKCD_COLORS .. py:data:: CSS4_COLORS