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 and a obtain the red, green, blue and alpha values.
  • h, s, and l 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)




This code is available on github in tutorial/colour/

If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.


Popular tags

2d arrays abstract data type alignment and angle animation arange arc array arrays behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function len line linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce repeat rgb rotation roundrect scaling scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip