pycharmers.utils.color_utils module

pycharmers.utils.color_utils.detect_color_code_type(color)[source]

Detect Color Code type

Parameters

color (tuple / str) – color code.

Examples

>>> from pycharmers.utils import detect_color_code_type
>>> detect_color_code_type("#FFFFFF")
'hex'
>>> detect_color_code_type((255,255,255))
'rgb'
>>> detect_color_code_type((0,0,0,1))
'rgba'
pycharmers.utils.color_utils.hex2rgb(hex, max_val=1)[source]

Convert color code from hex to rgb

pycharmers.utils.color_utils.hex2rgba(hex, max_val=1)[source]

Convert color code from hex to rgba

pycharmers.utils.color_utils.rgb2hex(rgb, max_val=1)[source]

Convert color code from rgb to hex

pycharmers.utils.color_utils.rgb2rgba(rgb, max_val=1)[source]

Convert color code from rgb to rgba

pycharmers.utils.color_utils.rgba2hex(rgba, max_val=1)[source]

Convert color code from rgba to hex

pycharmers.utils.color_utils.rgba2rgb(rgba, max_val=1)[source]

Convert color code from rgba to rgb

pycharmers.utils.color_utils.toHEX(color, max_val=1)

Convert color code to HEX

Parameters

color (tuple / str) – color code.

Examples

>>> from pycharmers.utils import toHEX
>>> toHEX("#FFFFFF")
#FFFFFF
>>> toHEX((255, 255, 255), max_val=255)
#FFFFFF
>>> toHEX((1, 1, 1, 1), max_val=1)
#FFFFFF
pycharmers.utils.color_utils.toRGB(color, max_val=1)

Convert color code to RGB

Parameters

color (tuple / str) – color code.

Examples

>>> from pycharmers.utils import toRGB
>>> toRGB("#FFFFFF")
(1.0, 1.0, 1.0)
>>> toRGB((255, 255, 255), max_val=255)
(255, 255, 255)
>>> toRGB((1, 1, 1, 1), max_val=1)
(1.0, 1.0, 1.0)
pycharmers.utils.color_utils.toRGBA(color, max_val=1)

Convert color code to RGBA

Parameters

color (tuple / str) – color code.

Examples

>>> from pycharmers.utils import toRGBA
>>> toRGBA("#FFFFFF")
(1.0, 1.0, 1.0, 1)
>>> toRGBA((255, 255, 255), max_val=255)
(255, 255, 255, 1)
>>> toRGBA((1, 1, 1, 1), max_val=1)
(1, 1, 1, 1)
pycharmers.utils.color_utils.choose_text_color(color, max_val=255, is_bgr=False)[source]

Select an easy-to-read text color from the given color.

Parameters
  • color (tuple / str) – color code.

  • max_val (int) – Maximum value.

References

WCAG

Examples

>>> from pycharmers.utils import choose_text_color
>>> from pycharmers.opencv import (cv2BLACK, cv2RED, cv2GREEN, cv2YELLOW, cv2BLUE, cv2MAGENTA, cv2CYAN, cv2WHITE)
>>> colors = locals().copy()
>>> for name,color in colors.items():
...     if name.startswith("cv2") and isinstance(color, tuple):
...         print(f"{name.lstrip('cv2'):<7}: {str(color):<15} -> {choose_text_color(color=color, max_val=255, is_bgr=True)}")
pycharmers.utils.color_utils.generateLightDarks(color, variation, diff=10, reverse=False)[source]

Generate light and dark colors.

Parameters
  • color (tuple) – Color [0,255]

  • variation (int) – How many colors to create.

  • diff (int) – How much to change

  • reverse (bool) – If True, sort in descending order.

Returns

colors.

Return type

colors (list)

Examples

>>> from pycharmers.utils import generateLightDarks
>>> generateLightDarks(color=(245,20,25), variation=3, diff=10)
[(235, 10, 15), (245, 20, 25), (255, 30, 35)]
>>> generateLightDarks(color=(245, 20, 25), variation=3, diff=-10)
[(225, 0, 5), (235, 10, 15), (245, 20, 25)]