Color

A Color object represents a RGB color. Using int instead of Color is completely fine.

Class Attributes

_random : random.Random

Used for random seeding, so it has it's own random.Random to do it.

To get a random color, use .random and to set seed use .set_seed

Examples

>>> # Colors are just int instances, so they can be freely converted between each other.
>>> color = Color(0x022587)
>>> color
<Color #022587>

>>> # Creating a color from rgb
>>> color = Color.from_rgb(120, 255, 0)
>>> color
<Color #78FF00>

>>> # Or creating a color from a tuple
>>> color = Color.from_rgb_tuple((100, 20, 255))
>>> color
<Color #6414FF>

>>> # Converting back to rgb
>>> color.as_rgb
(100, 20, 255)

>>> # Creating color from float color channels
>>> color = Color.from_rgb_float(1.0, 0.125, 0.862)
>>> color
<Color #FF1FDB>

>>> # Converting color back to float color channels
>>> color.as_rgb_float
(1.0, 0.12156862745098039, 0.8588235294117647)

>>> # Creating color from html color code
>>> color = Color.from_html('#ff0000')
>>> color
<Color #FF0000>

>>> # Converting color back to html color code
<Color #FF0000>
>>> color.as_html
'#FF0000'

>>> # str(color) is same as color.as_html
>>> str(color)
'#FF0000'

>>> # html color codes with length of 3 are working as well
>>> color = Color.from_html('#f00')
>>> color
<Color #FF0000>

Properties

as_float_tuple

Returns the color's red, green and blue channel's value as a tuple of floats.

Returns

rgb_tuple : tuple(float, float, float)

as_hsl_float_tuple

Returns the color in hue-saturation-lightness float tuple format.

Returns

hsl_float_tuple : tuple(float, float, float)

as_hsl_tuple

Returns the color in hue-saturation-lightness tuple format.

Returns

hsl_tuple : tuple(int, int, int)

as_html

Returns the color in HTML format.

Returns

color : str

as_rgb

Returns the color in red-green-blue tuple format.

Returns

rgb_tuple : tuple(int, int, int)

as_rgb_float

Returns the color's red, green and blue channel's value as a tuple of floats.

Returns

rgb_tuple : tuple(float, float, float)

as_rgb_float_tuple

Returns the color's red, green and blue channel's value as a tuple of floats.

Returns

rgb_tuple : tuple(float, float, float)

as_rgb_tuple

Returns the color in red-green-blue tuple format.

Returns

rgb_tuple : tuple(int, int, int)

as_tuple

Returns the color in red-green-blue tuple format.

Returns

rgb_tuple : tuple(int, int, int)

b

Returns the color's blue channel's value.

Returns

blue_value : int

blue

Returns the color's blue channel's value.

Returns

blue_value : int

g

Returns the color's green channel's value.

Returns

green_value : int

green

Returns the color's green channel's value.

Returns

green_value : int

r

Returns the color's red channel's value.

Returns

red_value : int

red

Returns the color's red channel's value.

Returns

red_value : int

Methods

from_float_tuple(rgb_tuple)

Converts the given red-green-blue float tuple to a color object.

ParameterTypeDescription
rgb_tuple

tuple(float, float, float)

A tuple of 3 floats which are in range [0.0, 1.0].

Returns

color : instance<cls>

Raises

ValueError

  • rgb_tuple has no length of 3.

from_hsl(hue, saturation, lightness)

Converts hue, saturation and lightness channels to a color object.

ParameterTypeDescription
hue

int

Hue channel.

saturation

int

Saturation channel.

lightness

int

Lightness channel.

Returns

color : instance<cls>

from_hsl_float(hue, saturation, lightness)

Converts hue, saturation and lightness float channels to a color object.

ParameterTypeDescription
hue

float

Hue channel.

saturation

float

Saturation channel.

lightness

float

Lightness channel.

Returns

color : instance<cls>

Raises

ValueError

Channel value out of expected range.

from_hsl_float_tuple(hsl_tuple)

Converts a hue-saturation-lightness float tuple to a color object.

ParameterTypeDescription
hsl_tuple

tuple(float, float, float)

A tuple of 3 floats which are in range [0.0, 1.0].

Returns

color : instance<cls>

Raises

ValueError

  • hsl_tuple has no length of 3.
  • Channel value out of expected range.

from_hsl_tuple(hsl_tuple)

Converts the given hue-saturation-lightness tuple to a color object.

ParameterTypeDescription
hsl_tuple

tuple(int, int, int)

A tuple of 3 integers ([0, 360], [0, 100], [0, 100]).

Returns

color : instance<cls>

Raises

ValueError

  • If hsl_tuple 's length is not 3.

from_html(value)

Converts a HTML color code to a color object.

ParameterTypeDescription
value

str

HTML color code.

Returns

color : instance<cls>

Raises

ValueError

The given value has invalid length or is not hexadecimal.

from_rgb(red, green, blue)

Converts red, green and blue channels to a color object.

ParameterTypeDescription
red

int

Red channel.

green

int

Green channel.

blue

int

Blue channel.

Returns

color : instance<cls>

from_rgb_float(red, green, blue)

Converts red, green and blue float channels to a color object.

ParameterTypeDescription
red

float

Red channel.

green

float

Green channel.

blue

float

Blue channel.

Returns

color : instance<cls>

from_rgb_float_tuple(rgb_tuple)

Converts the given red-green-blue float tuple to a color object.

ParameterTypeDescription
rgb_tuple

tuple(float, float, float)

A tuple of 3 floats which are in range [0.0, 1.0].

Returns

color : instance<cls>

Raises

ValueError

  • rgb_tuple has no length of 3.

from_rgb_tuple(rgb_tuple)

Converts the given red-green-blue tuple to a color object.

ParameterTypeDescription
rgb_tuple

tuple(int, int, int)

A tuple of 3 integers which are in range [0, 255].

Returns

color : instance<cls>

Raises

ValueError

  • If rgb_tuple 's length is not 3.
  • If a channel value out of expected range.

from_tuple(rgb_tuple)

Converts the given red-green-blue tuple to a color object.

ParameterTypeDescription
rgb_tuple

tuple(int, int, int)

A tuple of 3 integers which are in range [0, 255].

Returns

color : instance<cls>

Raises

ValueError

  • If rgb_tuple 's length is not 3.
  • If a channel value out of expected range.

random()

Returns a random color.

Returns

color : instance<cls>

set_seed(...)

Initialize the random number generator for the color type.

ParameterTypeOptionalDefaultDescription
seed

object

None

If seed is given as None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time.

If seed is an int, it is used directly.

Since Python 3.9 the seed must be one of the following types: NoneType, int, float, str, bytes, or bytearray.

version

int

2

Can be given either as 1, 2.

If given as 2(so by default), a str, bytes, bytearray object gets converted to an int and all of its bits are used.

If given as 1(provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

__repr__()

Returns the color's representation.

__str__()

Returns the color as a string. Same as .as_html.