As we saw in the previous section, colour is essentially a 3 dimensional quantity. It can be represented by 3 values, red, green and blue. Each unique combination of red, green and blue creates a unique colour.
You could think of these values as axes in a 3 dimensional space, where each point in space is a particular colour. We call this space the RGB colour space.
There are other ways to represent colours. They typically use 3 values, but rather than red, green and blue they use a different set of values (we call this a different colour space). Pillow supports several types of colour space:
Pillow also supports:
In RGB space, colour is represented by 3 numbers that specify the amount of red, green and blue that make up the colour. In this section will use percentages, where 0% means none of that colour is present, 100% means the maximum amount of that colour is present. So for example:
rgb(100%, 50%, 0%)
Indicates a colour that contains the maximum amount of red. 50% of the maximum green, and no blue. That would give an orange colour.
RGB is a natural way to represent colours on a computer, because it models how a computer screen works. As we saw previously, the eye senses colour by the amount of red, green and blue light present. This means that we can simulate almost any visible colour by mixing the correct amount of red, green and blue light. In a computer screen, each pixel has three tiny elements (typically light-emitting diodes) than acts as a variable source of red, green and blue light. By illuminating these sources by the correct amount, we can set each pixel to any colour.
We call RGB an additive model, because it works by adding separate red, green and blue lights sources.
Here are a selection of example colours:
This table shows every combination of red, green and blue values of 0%, 50% and 100%.
When we print an image, we normally apply different coloured inks to white paper to create the colours we require.
A printed page works quite differently to a computer screen. A screen starts is dark by default, and adds red, green and blue light to create colours. A printed page doesn't generate light, it simply reflects light. A blank page starts off white (assuming it is white paper viewed under white light), and the ink on the page absorbs different amounts of red, green and blue light, to leave the required colour. We call this a subtractive model.
Many printing processes work by adding several layers of different coloured translucent inks. Here is an illustration:
Because the ink is translucent, light passes through the ink, and reflects off the white paper underneath. The ink acts as a colour filter, removing certain colours.
We normally use cyan, magenta and yellow ink. Cyan ink filters out red light, leaving just green and blue (cyan is a mixture of green and blue, so when we remove the red from white light, we are left with cyan light). Magenta ink filters out green light (magenta is a mixture of red and blue). Yellow ink filters out blue light (yellow is a mixture of red and green).
If we put one layer of ink on top of another, light passes through both layers and reflects off the page. For example:
In this case, magenta ink has been layered over cyan ink. The cyan ink removes the red light, the magenta ink removes the green light, so we end up with a blue colour in the page. In fact, by layering different amounts of cyan, magenta and yellow ink on the page, it is possible to remove different amounts of red, green and blue from the reflected light, which allows you to print any colour.
If you own a colour inkjet printer or similar, you will probably know that it has 4 colours, cyan, magenta, yellow and black. The K part of CMYK stands for Key, which is an old printing term for the black plate in a traditional printing press.
In theory, we shouldn't need a separate black ink, because mixing cyan, magenta and yellow ink together should create black. But in reality, there are several advantages to using black ink:
Many colour printing processes - from documents that people might print on a home or office printer, through to newspapers or magazines - often feature significant amounts of black text. CMY printing generally doesn't create black text of suitable quality, so the best solution is usually to add separate black colour specifically for text and other 100% black features.
Black ink is also considerably cheaper to produce than coloured ink.
Any RGB colour that has equal amounts of red, green and blue, will display as a shade of grey, for example:
In the greyscale colours space, the colour is specified by a single value. The colour is displayed as if the red, green and blue values were all equal. You can think of greyscale as a subset of RGB that includes only the pure grey colours.
The greyscale colour space can only show shades of grey. It is useful for images that don't include any colour, for example:
This image
A greyscale image only stores 1 value per pixel (the grey level), whereas an RGB image store 3 values per pixel.
1-bit images are an extreme example of a greyscale colour space. They only allow each pixel to be either black or white, so a pixel value can be stored in a single bit (that is, 8 pixels can be stored in 1 byte). These are sometimes used for images that contain only black text and black line drawings, so there are no grey values.
The HSL colour space (and the closely related HSV space) store colours as 3 values:
This diagram shows the colour wheel of hue values. The hue can be expressed as an angle between 0 and 360 degrees, where 0 degrees is red, 120 degrees is green, and 240 degrees is blue:
Here is the effect of changing the saturation between 0% and 100% (with a hue of green and lightness of 50%). Notice that the basic colour remains the same:
Finally, here is the effect of changing the lightness between 0% and 100% (with a hue of green and saturation of 50%). Again, the basic colour remains the same:
HSL colours are useful in art and design applications, because they allow sets of related colours to be created very easily.
The HSB (hue, saturation, brightness) colour space uses the same definition of hue as HSL. Saturation and brightness behave differently, but can achieve the same range of colours as HSL.
The HSL model works well with the additive model (as used by RGB). HSB is based on a subtractive colour model, but generally CMYK is more useful for subtractive colours. HSB tends not to be used as often as HSL, so we will not cover it in detail.
You may also see the term HSV (hue, saturation, value). This is an alternative name for HSB.
Luminance chrominance colour spaces consist of 3 components:
There are several colour spaces that work in this way. Pillow supports YCbCr and L*a*b.
The two chrominance components contain similar information to the hue and saturation components of HSL, but it is coded differently. For example, in L*a*b, the *a component represents the position of the colour between magenta and green, the *b component represents the position of the colour between yellow and blue.
Luminance chrominance colour spaces have several uses:
If you found this article useful, you might be interested in the book Computer Graphics in Python, or other books, by the same author.
<<PrevCopyright (c) Axlesoft Ltd 2020