Colour class¶
- class colourettu.Colour(my_colour='#FFF', normalized_rgb=False)[source]¶
Base class for dealing with colours.
- Parameters
my_colour (str, list, or tuple) – string of hex representation of colour you are creating, or a 3 item list or tuple of the red, green, and blue channels of the colour you are creating. Default is “#FFF” (white).
normalized_rgb (bool) – whether the values for the red, green, and blue channels are normalized (i.e. values scaled from 0 to 1) or not (i.e. values scaled from 0 to 255). Default is False.
Colours are created by calling the
Colour
class. Colour values can be provided via 3 or 6 digit hex notation, or providing a list or a tuple of the Red, Green, and Blue values (as integers, ifnormalized_rgb=False
, or as floating numbers between 0 and 1 ifnormalized_rgb=True
).import colourettu from colourettu import Colour c1 = Colour() # defaults to #FFF c2 = Colour("#eee") # equivalent to #EEEEEE c3 = Colour("#456bda") c4 = Colour([3, 56, 129]) c5 = Colour((63, 199, 233)) c6 = Colour([0.242, 0.434, 0.165], normalized_rgb=True)
The value of each channel can be pulled out:
>>> c4.red() 3 >>> c4.green() 56 >>> c4.blue() 129
You can also get the colour back as either a hex value, or a rgb tuple:
>>> c2.hex() '#EEEEEE' >>> c2.rgb() (238, 238, 238)
Colours are considered equal is the values of the R, G, and B channels match.
>>> c1 == c2 False >>> c2 == Color([238, 238, 238]) True
- blue()[source]¶
Returns the value of the blue channel of the Colour.
- Returns
value of the blue channel of the colour
- Return type
int
- contrast(my_other_colour)[source]¶
Calls the
contrast()
on the colour defined.
- green()[source]¶
Returns the value of the green channel of the Colour.
- Returns
value of the green channel of the colour
- Return type
int
- hex()[source]¶
Returns the HTML-style hex code for the Colour.
- Returns
the colour as a HTML-sytle hex string
- Return type
str
- luminance()[source]¶
Calls the
luminance()
on the colour defined.
- normalized_rgb()[source]¶
Returns a tuples of the normalized values of the red, green, and blue channels of the Colour.
- Returns
- the rgb values of the colour (with values normalized between
0.0 and 1.0)
- Return type
tuple
Note
Uses the formula:
\[\begin{split}r_{norm} = \begin{cases} \frac{r_{255}}{12.92}\ \qquad &\text{if $r_{255}$ $\le$ 0.03928} \\ \left(\frac{r_{255} + 0.055}{1.055}\right)^{2.4} \quad &\text{otherwise} \end{cases}\end{split}\]