Skip to content

Colours in goopylib!

Bhavye Mathur edited this page Dec 10, 2020 · 1 revision

Colour is very important to make a good looking application and so I've compiled a list of colours that goopylib provides by default and how can create your own.

Goopylib takes a large turn from libraries like Tkinter where the colour 'green' refers to the highly saturated rgb(0, 255, 0) or red being rgb(255, 0, 0). In truth, these colours look horrible and are very rarely useful while making applications.


Colour Functions

Whenever I used goopylib for a project, I made sure to add good-looking colours to the colours.py file. Let's take a look at this simple program which I used for taking images of colours with text on them for this very Wiki:

from goopylib.objects.imports import *
from goopylib.colours import *  # This is how you import colours

window = Window("Test Window", width=150, height=110)

colour = ColourRGB(255, 0, 0)  # Set the colour here

Rectangle(Point(10, 10), Point(100, 30), outline_width=0, fill=colour).draw(window)
Text(Point(55, 20), text=colour, font_colour=WHITE, font_size=12, font_style="bold").draw(window)

while True:
    update(24)

Here you can replace the numbers inside ColourRGB(r, g, b) to anything between 0 & 255.

Other Colour Formats

But, perhaps you aren't familiar with RGB? Goopylib also provides other colour formats that you can use:

colour_rgb = ColourRGB(255, 0, 0)
colour_hex = ColourHex("#ff0000")
colour_cmyk = ColourCMYK(0, 100, 100, 0)

These will all give you the same results. Take note that you can only enter values between 0 & 100 for CMYK colours and values between #000000 and #ffffff for hex colours.

Random Colours

Another function goopylib has is RandomColourRGB(). And it does exactly that! It gives you a random colour:

rand_rgb = RandomColourRGB()  # Returns a Random RGB Colour
rand_cmyk = RandomColourCMYK()  # Returns a Random CMYK Colour
rand_hex = RandomColourHex()  # Returns a Random Hex Colour 

# or
rand_greyscale = RandomGreyscale()  # Gives a Random RGB Grey Colour

You can even specify values for these functions. Let's say you only wanted a Greyscale colour from 100 to 200 (by default, this is 0 to 255):

rand_greyscale = RandomGreyscale(start=100, end=200)  # You can also supply only the start argument or only the end argument

# another example

rand_colour = RandomColourRGB(red=70) # This will randomize only the blue & green values and keep red at 70
# or 
rand_colour = RandomColourRGB(blue=60, green=100)  # This will randomize only the red value while keeping blue=60, and green=100

You could do the very same with CMYK & Hex colours too:

rand_hex = RandomColourHex(green="ff", red="a5")  # Only Blue is randomized
# or
rand_cmyk = RandomColourCMYK(c=56, k=90)  # The M & K are randomized

Colour Gradients

Goopylib offers two functions called ColourGradient() and ColourGradient2D(). These functions take colours as an input and return a list of colours that form a gradient between them. Here is a very simple implementation of this:

number_of_divisions = 17  # This specifies how many colours to put between the start & end

for y_pos in range(30, 770, 45):   # To draw multiple examples of the gradient

    # Here, we supply random colours for the start & end values
    gradient = ColourGradient(colour_start=RandomColourRGB(), colour_end=RandomColourRGB(), divisions=number_of_divisions)

    current_colour = 0
    for x_pos in range(30, 30 + 45 * number_of_divisions, 45):
        Rectangle(Point(x_pos - 20, y_pos - 20), Point(x_pos + 20, y_pos + 20), fill=gradient[current_colour]).draw(window)
        current_colour += 1

while True:
    window.update_win()

This code outputs:

1-D Colour Gradient Example

Here, we can see that the ColourGradient() function takes 3 inputs: start_colour which defaults to white if nothing is provided, end_colour which defaults to black if nothing is provided and divisions which defaults to 10. The Colour Gradient is formed smoothly between the start & end colours with divisions number of divisions. In this case, we have just created multiple colour gradients and filled rectangles with them.

Now look at this 2D Gradient where we have reduced the size of the rectangles so that they are barely visible as singular rectangles:

from goopylib.objects.imports import *
from goopylib.colours import *

window = Window("Test Window", width=800, height=800)

rect_size = 5
gap = 0
gradient = ColourGradient2D(RandomColourRGB(), RandomColourRGB(), RandomColourRGB(), RandomColourRGB(),
                            divisions_x=len(range(30, 770, (gap + rect_size * 2))),
                            divisions_y=len(range(30, 770, (gap + rect_size * 2))))

for i, y_pos in enumerate(range(30, 770, (gap + rect_size) * 2)):
    for j, x_pos in enumerate(range(30, 770, (gap + rect_size) * 2)):
        Rectangle(Point(x_pos - rect_size, y_pos - rect_size),
                  Point(x_pos + rect_size, y_pos + rect_size), fill=gradient[j][i], outline_width=0).draw(window)

while True:
    window.update_win()

2-D Colour Gradient Example

Wow! That is beautiful! The change we have also made here is reducing the size of every rectangle and reducing the gap between them so that we can get a smooth gradient. As you can see, the 2D Gradient Function takes in 4 inputs start_colour1 (top-left), end_colour1 (top-right), start_colour2 (bottom-left), and end_colour2 (bottom-right). Along with this, we also give it the number of divisions in both the x & y directions.

Now also note in both of these examples, we have given the gradient random colours, but you could just as easily give it your custom colours too!


Maths with Colours

Let's say you wanted to do some math with colours... for whatever project that might require that. Well, colours in Goopylib are compatible with two different types of math: with Floats or Integers, and with other Colours.

Let's take a look at a simple colour, and add a bit of red to it:

red_colour = ColourRGB(175, 0, 0)
redder_colour = red_colour + ColourRGB(50, 0, 0)

print(redder_colour)

This code will output: #e10000 and this is clearly in the hex format. This is how Goopylib returns colours by default, to show us the output in RGB, we can use:

print(redder_colour.rgb())

This outputs rgb 225, 0, 0. The rgb() function can be used on any colour type (Hex, RGB, and CMYK).

Now, let us try adding an integer to a colour:

green_colour = ColourRGB(0, 150, 0)
new_colour = green_colour + 50

print(new_colour.rgb())

This outputs rgb 50, 50, 200. When operating with integers (or floats) and colours, the operation is performed with every RGB value in the colour with the integer. When performing operations with colours on other colours, the operation is performed with the red value of the first colour and the second colour, the blue value of the first and second, and the same for green.

The exception to this are the right-shift (>>) and left-shift (<<) operators. When performed with integers, the RGB values of the colours shift to the given directions:

blue = ColourRGB(0, 0, 255)
green = blue << 2

print(green.rgb())

This outputs rgb 255, 0, 0 and as you can see, it has shifted the blue value 2 places to the left and the same with the green & red value though you cannot see these as they are both 0.

A useful operator to remember is -. Not subtraction, but a negation. Negative colours are simply just inverted colours. So negating Black would give you White:

black = ColourRGB(0, 0, 0)
white = -black

You should also remember that if an operation is performed on the colour and the values exceed 255 in the RGB format, then that value is set to 255 and if the value becomes negative, it is set to 0. If a float value occurs (such as in division), the int() of that is taken.

The operations defined for colours are: +, -, *, /, **, //, %, <<, >>, ^, &, |, ~

You can also use certain Python functions & operators: abs(), bool(), bytes(), in, and, or


A List of Colours

Just to get you started, here is a list of predefined colours in goopylib with their names. If you want to help out goopylib by defining more colours for others to use, download the colour.py file and find the location where all the colours are defined (line 343), and add your colours!

Blacks, Greys, & Whites

Grey Shades

Blue Greys

Blue-Grey Shades

Reds & Pinks

Red & Pink Shades

Oranges & Browns

Orange Shades

Brown Shades

Yellows

Yellow Shades

Blues & Turquoises

Turquoise Shades

Blue Shades

Greens

Green Shades

Olive Green Shades

Purples & Violets

Purple Shades

Violet Shades

Clone this wiki locally