Python and MatPlotLib: Understanding How We Encode Colour

The Human Eye

Although the electromagnetic spectrum has a vast span, the eye can only detect wavelengths from about 390 to 750 nm, the so-called visible range. Moreover the eye only consists of three types of colour sensors, Short (S), Medium (M) and Long (L) cones which detect Short wavelengths, Medium Wavelengths and Long Wavelengths respectively. These have a maximum at Blue (b), Green (g) and Red (r) respectively, the so called Primary Colours.

When a photon of a specific wavelength hits the cones of the eye, it is either detected or not detected and the eye does not know what wavelength or colour the photon is. The brain however deduces colour by contrasting the relative intensities of the three cone types S,M and L, for instance if a high intensity of light is only detected by S cones and nothing is detected by M or L cones, then the brain would translate the colour as Blue (b). Conversely if there is no light detected by the S and M cones and a high intensity of light detected by the L cones, then the brain would translate the colour as Red (r). The brain creates all other colours by using ratios detected by the three cones S, M and L.

Additive Colour Mixing Using Light Emitting Diodes (LEDs)

Light Emitting Diodes are cheap unpolarised light sources and can be made in Blue, Green and Red. The spectra below shows a typical spectrum.

The following image shows an overlay of a Red (r), Blue (b) and Green (g) LED. Note the area where the Green (g) and Red (r) overlap appears to be Yellow (y). If we look at the spectrum of the Red and Green LEDs above we can see that there is actually very little light at 580 nm. Yellow (y) light is not actually created, instead the eye detects a low intensity of light at the S cones and a high intensity of light at both the M cones and L cones and the brain translates this high intensity of light as Yellow (y).

This type of colour mixing is called Additive Colour mixing and the light examined is light generated from the three LED sources, the three primary colours, Red (r), Green (g) and Blue (b).

  • Intense Red (r) and Blue (b) appear to make the secondary colour Magenta (M).
  • Intense Green (g) and Blue (b appear to make the secondary colour Cyan (C).
  • Intense Red (r) and Green (g) appear to make the secondary colour Yellow (W).
  • Intense Red (r), Green (g) and Blue (b) combine to make White (W) which is the maximum intensity your eye can see.
  • The absence of any light appears blacK (k).

In every pixel of a computer monitor, there are three primary LEDs, red (r), green (g) and blue (b). The secondary colours yellow (y), cyan (c) and magenta (m) are a mix of two of the primary colours (r+g, g+b, r+b respectively) while white (w) is a mix of the three primary colours and black (k) is an absence of all three colours. The Matplotlib library recognises all of these single letter abbreviations r,g,b,y,c,m,w and k as well as the full colour names red, green, blue, yellow, cyan, magenta, white and black which can be used to encode colours using a single letter.

However these LEDs are typically not just merely on at full brightness or off. Instead they have a range of intensity levels. The convention is to use 8 Bit (0-255). We can instead construct a three value so called [r,g,b] array to designate each individual colour. This [r,g,b] array is commonly used in Programs such as Microsoft Paint and Microsoft Word however other programs such as the MatPlotlib library normalise this [r,g,b] array to create a [r,g,b] float array.

Single
Letter
String
Full
String
[r,g,b]
Float
Array
[r,g,b]
Array
Hex
rred[1,0,0][255/255,
0/255,0/255]
#ff0000
ggreen[0,1,0][0/255,255/255,0/255]#00ff00
bblue[0,0,1][0/255,0/255,0/255] #0000ff
yyellow[1,1,0][255/255,255/255,0/255] #ffff00
ccyan[0,1,1][0/255,255/255,255/255] #00ffff
mmagenta[1,0,1] [255/255,0/255,255/255] #ff00ff
kblack[0,0,0][0/255,0/255,0/255]#000000
wwhite[1,1,1][255/255,255/255,255/255]#ffffff

If we look at the Colour picker in Microsoft Word you can see the three Primary Colour Channels and how they all have a level of 255 for their respective colour channel.

The secondary colours have a level of 255 for 2 channels:

While white has a level of 255 for all 3 channels.

Black is defined as the absence of light where all three channels are off.

Intermediate values where each Channel has the same value are known as Shades. Alternatively if there is only a single channel, it is known as grey scale.

All other colours are made using various combinations and you can explore using the colour picker in Microsoft Word or another program.

[r,g,b] Array – 8 Bit Levels

Now let’s have a look at the binary system which is best represented by a light switch. It can have 2=21 positions (0 or 1). If we move to 2 switches then there are 22=4 combinations that can be made (00, 01, 10 and 11). If we have 3 switches then we have 23=8 positions and so on and so forth. This is 1 bit, 2 bit, 3 bit respectively.

Colours are quite often encoded in 8 bit. Under 8 bit encoding, there are 28=256 intensity levels for the red channel, 256 intensity levels for the green channel and 256 intensity levels for the blue channel which as mentioned is sufficient for the Human Eye. Because 0 order indexing is used, we assign one of these values to 0 and we go up to the maximum value of 256 but never reach it, so the range of values for each channel is from between 0 and 255.

In Python’s MatPlotLib the uint8 (0-255) values aren’t usually for plotting. Instead they are presented as floating point numbers between 0 and 1. In essence one needs to divide through by 255. For instance red would in uint8 form would be [255,0,0] and in float form it would be [255/255,0/255,0/255] or [1.00000, 0.00000, 0.00000].

[r,g,b,a] Array

Colours can also be encoded with an alpha value to alter the transparancy. This gives a [r,g,b,a] float array. The fourth channel selects the transparency where 0 stands for full transparency (invisible) and 1 represents full colour.

Hexadecimal

8 bit colours are also commonly encoded using 2×4 Bit values. In 4 Bit, there are four switches and thus 24=16 numbers of combinations. Each of these combinations is assigned a number starting from 0 and going to 9 and after the first 10, once we have ran out of numbers, we instead use a Capital letter on the keyboard so continue using A and finish at F.

Since 16×16=256 we can use two characters to represent each number. Because we have three channels we need 3×2=6 Characters to specify a colour. In this Hexadecimal form we also begin with a #. So the colour red for instance is #FF0000.

The three systems represented above are all just different ways of selecting a numeric value from 0 to 255. A numeric value is then applied to the red channel, green channel and blue channel.

8 Bit Numeric
[xxx,xxx,xxx]
Float
[x.xxxxx,x.xxxxx,x.xxxxx]
Hexadecimal
#xxxxxx
00.0000000
10.0039201
20.0078402
30.0117603
40.0156904
50.0196105
60.0235306
70.0274507
80.0313708
90.0352909
100.039220A
110.043140B
120.047060C
130.050980D
140.054900E
150.058820F
160.0627510
170.0666711
180.0705912
190.0745113
200.0784314
210.0823515
220.0862716
230.0902017
240.0941218
250.0980419
260.101961A
270.105881B
280.109801C
290.113731D
300.117651E
310.121571F
320.1254920
330.1294121
340.1333322
350.1372523
360.1411824
370.1451025
380.1490226
390.1529427
400.1568628
410.1607829
420.164712A
430.168632B
440.172552C
450.176472D
460.180392E
470.184312F
480.1882430
490.1921631
500.1960832
510.2000033
520.2039234
530.2078435
540.2117636
550.2156937
560.2196138
570.2235339
580.227453A
590.231373B
600.235293C
610.239223D
620.243143E
630.247063F
640.2509840
650.2549041
660.2588242
670.2627543
680.2666744
690.2705945
700.2745146
710.2784347
720.2823548
730.2862749
740.290204A
750.294124B
760.298044C
770.301964D
780.305884E
790.309804F
800.3137350
810.3176551
820.3215752
830.3254953
840.3294154
850.3333355
860.3372556
870.3411857
880.3451058
890.3490259
900.352945A
910.356865B
920.360785C
930.364715D
940.368635E
950.372555F
960.3764760
970.3803961
980.3843162
990.3882463
1000.3921664
1010.3960865
1020.4000066
1030.4039267
1040.4078468
1050.4117669
1060.415696A
1070.419616B
1080.423536C
1090.427456D
1100.431376E
1110.435296F
1120.4392270
1130.4431471
1140.4470672
1150.4509873
1160.4549074
1170.4588275
1180.4627576
1190.4666777
1200.4705978
1210.4745179
1220.478437A
1230.482357B
1240.486277C
1250.490207D
1260.494127E
1270.498047F
1280.5019680
1290.5058881
1300.5098082
1310.5137383
1320.5176584
1330.5215785
1340.5254986
1350.5294187
1360.5333388
1370.5372589
1380.541188A
1390.545108B
1400.549028C
1410.552948D
1420.556868E
1430.560788F
1440.5647190
1450.5686391
1460.5725592
1470.5764793
1480.5803994
1490.5843195
1500.5882496
1510.5921697
1520.5960898
1530.6000099
1540.603929A
1550.607849B
1560.611769C
1570.615699D
1580.619619E
1590.623539F
1600.62745A0
1610.63137A1
1620.63529A2
1630.63922A3
1640.64314A4
1650.64706A5
1660.65098A6
1670.65490A7
1680.65882A8
1690.66275A9
1700.66667AA
1710.67059AB
1720.67451AC
1730.67843AD
1740.68235AE
1750.68627AF
1760.69020B0
1770.69412B1
1780.69804B2
1790.70196B3
1800.70588B4
1810.70980B5
1820.71373B6
1830.71765B7
1840.72157B8
1850.72549B9
1860.72941BA
1870.73333BB
1880.73725BC
1890.74118BD
1900.74510BE
1910.74902BF
1920.75294C0
1930.75686C1
1940.76078C2
1950.76471C3
1960.76863C4
1970.77255C5
1980.77647C6
1990.78039C7
2000.78431C8
2010.78824C9
2020.79216CA
2030.79608CB
2040.80000CC
2050.80392CD
2060.80784CE
2070.81176CF
2080.81569D0
2090.81961D1
2100.82353D2
2110.82745D3
2120.83137D4
2130.83529D5
2140.83922D6
2150.84314D7
2160.84706D8
2170.85098D9
2180.85490DA
2190.85882DB
2200.86275DC
2210.86667DD
2220.87059DE
2230.87451DF
2240.87843E0
2250.88235E1
2260.88627E2
2270.89020E3
2280.89412E4
2290.89804E5
2300.90196E6
2310.90588E7
2320.90980E8
2330.91373E9
2340.91765EA
2350.92157EB
2360.92549EC
2370.92941ED
2380.93333EE
2390.93725EF
2400.94118F0
2410.94510F1
2420.94902F2
2430.95294F3
2440.95686F4
2450.96078F5
2460.96471F6
2470.96863F7
2480.97255F8
2490.97647F9
2500.98039FA
2510.98431FB
2520.98824FC
2530.99216FD
2540.99608FE
2551.00000FF

Spelling Differences Colour vs Color

It should be noted that there are two ways to spell colour. One with a u “colour” and one without a u “color”.

“color” is actually an older spelling and it was updated to the spelling “colour” in the UK post 1777, this is after the USA declaration of independence and as a consequence of declaring independence the USA did not keep in line with British standardisations* and thus “color” is generally preferred in the USA whereas as “colour” is preferred in the CANZUK countries. MatPlotLib only recognises the former spelling “color” so if you use the modern spelling “colour” like I as a Brit commonly do, then you’ll be frustrated like me when your code throws up an error.

* Ironically the UK and USA can’t even “standardise” the word “standardize”.

Colour Picker Tool

Note there a number of colour selection tools online for example:

People usually just use them to select a colour but I thought it worthwhile to explain the foundation behind how we select them.

Microsoft Office Standard Colours

Here are the Standard Colours in Microsoft Office.

Microsoft Word RGBHex
[192/255,0/255,0/255]#c00000
[255/255,0/255,0/255]#ff0000
[255/255,192/255,0/255]#ffbf00
[255/255,255/255,0/255]#ffff00
[146/255,208/255,80/255]#92d050
[0/255,176/255,80/255]#00b050
[0/255,176/255,240/255]#00b0f0
[0/255,112/255,96/255]#007060
[0/255,32/255,96/255]#002060
[112/255,48/255,160/255]#6f30a0

Subtractive Colour Mixing Using Inks

In print media Subtractive Colour Mixing is used instead of Additive Colour Mixing. For print media, the source of light comes via room light or the ultimate source of light on our planet, the sun. Print media usually involves paper which appears White (w). Paper appears White (w) because it is highly reflective and all the light incident on the paper is reflected. Dyes however absorb Light of certain wavelengths and reflect light of other wavelengths and this reflected light appears to make coloured items such as paintings and books.

The most commonly used dyes are Cyan (c), Magenta (m), Yellow (y) and Black (k). These dyes are shown at full concentration and diluted in water about 1,000 fold.

The transmission spectrum of these four diluted solutions is below.

As you see the dye that appears Yellow absorbs everything in the Blue (b) and lets through the light which is in the Green (g) and Red (r). The high intensity of Red (r) and Green (g) light passing through the dye make it appear Yellow (y).

  • Reflective Piece of paper with No Dye
    • White (w) = White (w)
  • Cyan (c) Dye
    • White (w) – Red (r) = Green (g) + Blue (b) = Cyan (c)
  • Yellow (y) Dye
    • White (w) – Blue (b) = Red (r) + Green (g) = Yellow (y)
  • Magenta (m) Dye
    • White (w) – Green (r) = Red (r) + Blue (b) = Magenta (m)
  • blacK (k) Dye
    • White (w) – Red (r) – Green (g) – Blue (b) = blacK (k)
Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.