Colour schemes in generativepy
Martin McBride, 2022-06-04
Tags generativepy tutorial colour colour scheme lerp
Categories generativepy generativepy tutorial

Colour schemes allows you to create a set of named colours that can be easily reused. This is a good way to keep colours consistent across a larger project.
There are a small number of defined colour schemes in the color module, or you can define your own as we will show here.
Using a colour scheme
This simple code uses the built-in colour scheme ArtisticColorScheme
:
from generativepy.drawing import make_image, setup from generativepy.color import Color, ArtisticColorScheme from generativepy.geometry import Square # Select a colour scheme cs = ArtisticColorScheme() def draw_color_scheme(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color("white")) # Fill a square with red Square(ctx).of_corner_size((50, 50), 200).fill(cs.RED) # Fill a square with a lighter version of RED, stroke with a darker version Square(ctx).of_corner_size((300, 50), 200).fill(cs.RED.light1).stroke(cs.RED.dark1, 10) make_image("colour-scheme.png", draw_color_scheme, 600, 300)
Here is the resulting image:
The code can be found on github in tutorial/colour/colour_schemes.py
To use the colour scheme we simply import it from the color
module, then create a colour scheme object cs
.
To fill a square with the red colour from the colour scheme, we pass cs.RED
into the fill
function.
For the second square, we fill it with a lighter shade of the same red, using cs.RED.light1
. We could also use light2
ot light3
to create even lighter shades. We then stroke it with a darker shade of the same red, using cs.RED.dark1
. Again, we could have used dark2
or dark3
to get even darker shades.
Defining your own colour scheme
Here is how we can define our won colour scheme:
from generativepy.drawing import make_image, setup from generativepy.color import Color, ArtisticColorScheme from generativepy.geometry import Square # Create a user defined colour scheme and use it class UserColorScheme: def __init__(self): self._RED = Color(0.5, 0, 0.25) self._BLUE = Color(0, 0, 0.5) self._GREEN = Color(0, 0.5, 0.25) @property def RED(self): return self._RED @property def BLUE(self): return self._BLUE @property def GREEN(self): return self._GREEN cs = UserColorScheme() def draw_color_scheme_user(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, background=Color("white")) # Fill a square with red Square(ctx).of_corner_size((50, 50), 200).fill(cs.RED) # Fill a square with a lighter version of RED, stroke with a darker version Square(ctx).of_corner_size((300, 50), 200).fill(cs.RED.light1).stroke(cs.RED.dark1, 10) make_image("colour-scheme_user.png", draw_color_scheme_user, 600, 300)
Our UserColorScheme
is just a class with a property for each colour we need. There is no standard set of colour names, you can create any colours you like. You would most likely want to define UserColorScheme
in a separate file, so it can be shared with other code. We have included it in the same file to keep the example smaller.
The drawing code remains the same because (as it happens) the code only uses RED, which is present in both colour schemes. However, this colour scheme has a bluer shade of red, so the resulting image looks different:
The code can be found on github in tutorial/colour/colour_schemes_user.py