Friday, May 15, 2015

PaA - Colours and Computers


Background

Usually, working with computer graphics means that you have access to about 16 millions different colors. What makes this amazing is that you can select any of these colors by mixing three basic colours:

  • How much Red? From 0 to 255
  • How much Green? From 0 to 255
  • How much Blue? From 0 to 255
This is called RGB encoding. Here is an online tool to find out what RGB code matches any given color.

The RGB palette

Why 16 millions?

Here are some fun facts about colors. There are three colour channel: Red, Green and Blue. Each channel uses 1 byte of memory (which contains 8 bits). This is just enough to store a number between 0 and 255 (or \(2^8\) ). We'll denote colours as follow: $$color = (Red, Green, Blue)$$

For example, pure red is defined as such: $$red = (255, 0 , 0)$$ Pure white is a combination of all colours at the same time: $$white = (255, 255 , 255)$$ And Black is when there are no colours in any of the colour channel: $$black = (0, 0, 0)$$

To create colours in Python using the graphics library, use the following function:

my_red = color_rgb(255, 0, 0)

So, for each possible of value of Red (256 different values), there are 256 possible values for Green. That is a big enough number, 65,536 colors in fact. And for each of these 65,536 colors, there is 256 possible values of Blue. In other terms: $$n_{colours} = 256 \times 256 \times 256 = 256^3 = 16,777,216$$

Your eyes and brain can't even distinguish 16 million colours. You eye actually can distinguish only about 7 millions.

Learn by doing

The best way to get a grasp of RGB, which can be tricky, is to play with a colour picker. Click on colours that you like and look at the corresponding RGB triples of values. What is the RGB value for yellow?

Interesting artwork often selects colours that harmonize well. Here is a tool to generate RGB code of harmonious colours from a single RGB code.

Python trick: A random color

Random colours can be fun to use because you never know what you'll get each time that you run your script. Here is a code snippet to generate a random color.

from random import randint
from graphics import *

my_color = color_rgb(randint(0,255), randint(0,255), randint(0,255))

No comments:

Post a Comment