Colour properties in generativepy
Martin McBride, 2021-11-15
Tags generativepy tutorial colour colour channel
Categories generativepy generativepy tutorial

A Color
object has several properties:
r
,g
,b
anda
obtain the red, green, blue and alpha values.h
,s
, andl
obtain the hue, saturation and lightness values. The colour is converted from RGB to HSL to do this.
The values returned are in the range 0.0 to 1.0.
Color
objects can also return the colour value as a set of integers in the range 0 to 255. So for example:
- A colour value of 0.0 is converted to an integer 0.
- A colour value of 0.5 is converted to an integer 127.
- A colour value of 1.0 is converted to an integer 255.
The function as_rgb_bytes
returns the RGB values as a tuple of 3 ints, scaled as described above.
The function as_rgba_bytes
returns the RGBA values as a tuple of 4 ints in a similar way.
The function as_rgbstr
returns the RGB values as string quantity of the form 'rgb(255, 127, 0)'
.
Here is some code that prints the results of these features:
from generativepy.color import Color # Create a colour and print its properties color = Color(1.0, 0.5, 0.0) print(color.r) print(color.g) print(color.b) print(color.a) print(color.h) print(color.s) print(color.l) print(color.as_rgbstr()) print(color.as_rgb_bytes()) print(color.as_rgba_bytes())
This code is available on github in tutorial/colour/colour_properties.py.
If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.
Prev