hbutils.color.utils

Overview:

Useful color utilities based on color model

visual_distance

hbutils.color.utils.visual_distance(c1: Union[hbutils.color.model.Color, str], c2: Union[hbutils.color.model.Color, str]) → float[source]
Overview:

Get distance of 2 colors.

Arguments:
  • c1 (Color): First color.

  • c2 (Color): Second color.

Returns:
  • distance (float): Distance of the colors.

Examples::
>>> from hbutils.color import visual_distance, Color
>>> visual_distance(
...     '#ff0000',
...     '#00ff00'
... )
2.5495097567963922
>>> visual_distance(
...     '#778800',
...     '#887700'
... )
0.16996731711975946

rnd_colors

hbutils.color.utils.rnd_colors(count, lightness=0.5, saturation=1.0, alpha=None, init_dis=4.0, lr=0.95, ur=1.5, rnd=None) → Iterator[hbutils.color.model.Color][source]
Overview:

Generating random colors which are not similar.

Parameters:
  • count – Count of colors.

  • lightness – Lightness of the colors (in HLS color space), default is 0.5.

  • saturation – Saturation of the colors (in HLS color space), default is 1.0.

  • alpha – Alpha of the colors, default is None.

  • init_dis – Initial distance of colors, default is 4.0.

  • lr – Lower ratio when generating, default is 0.95.

  • ur – Upper ratio when generating, default is 1.5.

  • rnd – Random object to be used, default is random.Random(0).

Returns:
  • colors (Iterator[Color]): A iterator of colors.

Examples::
>>> from hbutils.color import rnd_colors
>>> for c in rnd_colors(12):
...     print(c)
#ff00ee
#00ff00
#009cff
#ff006c
#c9ff00
#00f3ff
#d100ff
#ffaf00
#00ff6c
#4100ff
#ff5300
#46ff00
>>> for c in rnd_colors(12, 0.8, 0.9):
...     print(c)
#fa9ef4
#9efaa1
#9eb4fa
#faa69e
#c5fa9e
#9ed6fa
#f09efa
#faf89e
#9ef9fa
#c09efa
#fabe9e
#9efaca

linear_gradient

hbutils.color.utils.linear_gradient(colors: Union[Sequence[Union[hbutils.color.model.Color, str]], Sequence[Tuple[float, Union[hbutils.color.model.Color, str]]]]) → Callable[[float], hbutils.color.model.Color][source]
Overview:

Linear gradient of the colors.

Parameters:

colors – Colors to gradient.

Returns:

A mapping function for gradient.

Examples::
  • Simple Linear Gradientation

>>> from hbutils.color import linear_gradient
>>>
>>> f = linear_gradient(('red', 'yellow', 'lime'))
>>> f(0)
<Color red>
>>> f(0.25)
<Color #ff8000>
>>> f(1 / 3)
<Color #ffaa00>
>>> f(0.5)
<Color yellow>
>>> f(2 / 3)
<Color #aaff00>
>>> f(0.75)
<Color #80ff00>
>>> f(1)
<Color lime>
  • Complex Linear Gradientation

>>> f = linear_gradient(((-0.2, 'red'), (0.7, '#ffff0044'), (1.1, 'lime')))
>>> f(-0.2)
<Color red, alpha: 1.000>
>>> f(0)
<Color #ff3900, alpha: 0.837>
>>> f(0.25)
<Color #ff8000, alpha: 0.633>
>>> f(1 / 3)
<Color #ff9700, alpha: 0.565>
>>> f(0.5)
<Color #ffc600, alpha: 0.430>
>>> f(2 / 3)
<Color #fff600, alpha: 0.294>
>>> f(0.7)
<Color yellow, alpha: 0.267>
>>> f(0.75)
<Color #dfff00, alpha: 0.358>
>>> f(0.8)
<Color #bfff00, alpha: 0.450>
>>> f(1)
<Color #40ff00, alpha: 0.817>
>>> f(1.1)
<Color lime, alpha: 1.000>