Colour schemes in generativepy

By 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

See also

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

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style 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 decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula 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 latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest