Color
A Color object represents a RGB color. Using int instead of Color is completely fine.
Class Attributes
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
(rgb_tuple)
from_float_tupleConverts the given red-green-blue float tuple to a color object.
Parameter | Type | Description |
---|---|---|
rgb_tuple |
| A tuple of |
Returns
color : instance<cls>
Raises
ValueError
rgb_tuple
has no length of3
.
(hue, saturation, lightness)
from_hslConverts hue, saturation and lightness channels to a color object.
Parameter | Type | Description |
---|---|---|
hue |
| Hue channel. |
saturation |
| Saturation channel. |
lightness |
| Lightness channel. |
Returns
color : instance<cls>
(hue, saturation, lightness)
from_hsl_floatConverts hue, saturation and lightness float channels to a color object.
Parameter | Type | Description |
---|---|---|
hue |
| Hue channel. |
saturation |
| Saturation channel. |
lightness |
| Lightness channel. |
Returns
color : instance<cls>
Raises
ValueError
Channel value out of expected range.
(hsl_tuple)
from_hsl_float_tupleConverts a hue-saturation-lightness float tuple to a color object.
Parameter | Type | Description |
---|---|---|
hsl_tuple |
| A tuple of |
Returns
color : instance<cls>
Raises
ValueError
hsl_tuple
has no length of3
.- Channel value out of expected range.
(hsl_tuple)
from_hsl_tupleConverts the given hue-saturation-lightness tuple to a color object.
Parameter | Type | Description |
---|---|---|
hsl_tuple |
| A tuple of |
Returns
color : instance<cls>
Raises
ValueError
- If
hsl_tuple
's length is not3
.
(value)
from_htmlConverts a HTML color code to a color object.
Parameter | Type | Description |
---|---|---|
value |
| HTML color code. |
Returns
color : instance<cls>
Raises
ValueError
The given value has invalid length or is not hexadecimal.
(red, green, blue)
from_rgbConverts red, green and blue channels to a color object.
Parameter | Type | Description |
---|---|---|
red |
| Red channel. |
green |
| Green channel. |
blue |
| Blue channel. |
Returns
color : instance<cls>
(red, green, blue)
from_rgb_floatConverts red, green and blue float channels to a color object.
Parameter | Type | Description |
---|---|---|
red |
| Red channel. |
green |
| Green channel. |
blue |
| Blue channel. |
Returns
color : instance<cls>
(rgb_tuple)
from_rgb_float_tupleConverts the given red-green-blue float tuple to a color object.
Parameter | Type | Description |
---|---|---|
rgb_tuple |
| A tuple of |
Returns
color : instance<cls>
Raises
ValueError
rgb_tuple
has no length of3
.
(rgb_tuple)
from_rgb_tupleConverts the given red-green-blue tuple to a color object.
Parameter | Type | Description |
---|---|---|
rgb_tuple |
| A tuple of |
Returns
color : instance<cls>
Raises
ValueError
- If
rgb_tuple
's length is not3
. - If a channel value out of expected range.
(rgb_tuple)
from_tupleConverts the given red-green-blue tuple to a color object.
Parameter | Type | Description |
---|---|---|
rgb_tuple |
| A tuple of |
Returns
color : instance<cls>
Raises
ValueError
- If
rgb_tuple
's length is not3
. - If a channel value out of expected range.
()
randomReturns a random color.
Returns
color : instance<cls>
(...)
set_seedInitialize the random number generator for the color type.
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
seed |
|
| If If
| |
version |
|
| Can be given either as If given as If given as |
()
__repr__Returns the color's representation.
()
__str__Returns the color as a string. Same as .as_html
.