edubun
English

RGB and hex codes

With 256 steps for each of red, green and blue, a screen can address 256 × 256 × 256 = 16,777,216 colours, and every one of them is a point inside a cube.

Loading the 3D model

Drag to turn. Scroll or pinch to zoom.

Drag to turn the solid. Click a cube in the RGB view, or the coloured disk or upright slice in the HSL view, to pick that colour. The panel below the model gives the hex code, the HSL values, the harmony colours and the contrast ratio against the background you choose.

Three numbers, three axes

In the sRGB colour space used by the web, a colour is written as three channel values: red, green and blue. In everyday code each channel is a whole number from 0 to 255, which is the range that fits in eight bits. A channel at 0 means that light is off. A channel at 255 means it is fully on. So rgb(255 0 0) is the strongest red the screen can show, rgb(0 0 0) is black and rgb(255 255 255) is white.

Three numbers that each run over a range make a box. Put red along one edge, green up another and blue along the third, and every colour has a place inside a cube. The model above draws that cube with 512 small blocks, eight steps along each edge. Black sits in one corner. White sits in the opposite corner. The three corners next to black are pure red, pure green and pure blue. The three corners next to white are cyan, magenta and yellow, each made of two full channels. Turn on the labels and follow the edges from black to white: every path passes through one primary and one secondary colour.

The dashed line from black to white is the grey axis. Every point on it has equal red, green and blue, such as rgb(128 128 128). Colours far from that line are vivid. Colours close to it are greyish. This is the first hint of the idea behind the HSL model in the next unit: distance from the grey axis is a measure of how colourful something is.

Reading a hex code

CSS also lets you write the same three numbers in hexadecimal, after a # sign. Hexadecimal counts in sixteens using the digits 0 to 9 and the letters a to f, where a is 10 and f is 15. Two hex digits cover 16 × 16 = 256 values, exactly one channel. So a six-digit code is three pairs: red, green, blue. CSS Color 4 notes that the case of the letters does not matter, so #00ff00 and #00FF00 are the same colour.

The code #2273c3 split into its three channels. The first digit of each pair counts sixteens, the second counts ones.

To read a pair, multiply the first digit by 16 and add the second. For c3 that is 12 × 16 + 3 = 195. For ff it is 15 × 16 + 15 = 255, the maximum. For 80 it is 8 × 16 = 128, almost exactly halfway. A three-digit code such as #f80 is shorthand: each digit is doubled, so #f80 means #ff8800. Type a code into the Hex box under the model and the marker jumps to that point in the cube.

BackgroundThe idea of building any colour from three filtered images was shown in public in 1861. At a lecture at the Royal Institution in London, James Clerk Maxwell presented a photograph of a tartan ribbon. The photographer Thomas Sutton had taken it three times, through red, green and blue filters. Each picture was then projected through the same filter it was taken with, and the three projections were laid on top of each other on a screen. The plates were far from perfect, but the method of three filtered channels is the one every digital camera and screen still uses.
Colourrgb()Hex
Blackrgb(0 0 0)#000000
Whitergb(255 255 255)#ffffff
Redrgb(255 0 0)#ff0000
Yellowrgb(255 255 0)#ffff00
Cyanrgb(0 255 255)#00ffff
Middle greyrgb(128 128 128)#808080
#ff0000Red, rgb(255 0 0)
#00ffffCyan, rgb(0 255 255)
#ff00ffMagenta, rgb(255 0 255)
#ffff00Yellow, rgb(255 255 0)
#808080Middle grey, rgb(128 128 128)
#000Black, short for #000000
Key points
  • sRGB colours are three channels, each 0 to 255.
  • The RGB cube has black and white in opposite corners and a grey axis between them.
  • A hex code is three pairs of base-16 digits: red, green, blue.
  • 256³ = 16,777,216 colours.
  1. How many values can one 8-bit channel take?
  2. How many colours can 8-bit RGB address?
  3. What is the hex pair c3 in decimal?
  4. What is the red channel of #a8581b in decimal?
  5. Write white as a six-digit hex code.
  6. Which colour is #00ffff?
    • Cyan
    • Yellow
    • Magenta
    • Blue
  7. What does the short code #f80 mean?
    • #ff8800
    • #f80000
    • #0f0800
    • #f8f8f8
  8. In the RGB cube, which corner is opposite black?
    • White
    • Red
    • Blue
    • Grey
  9. Which of these lies on the grey axis of the cube?
    • rgb(90 90 90)
    • rgb(90 0 90)
    • rgb(255 90 90)
    • rgb(0 0 90)
  10. Are #00ff00 and #00FF00 the same colour in CSS?
    • Yes
    • No
    • Only in some browsers
    • Only in print
  11. In what year did Maxwell show the three-filter colour photograph?
Sources for this course (14)
  • W3C, Web Content Accessibility Guidelines (WCAG) 2.2, relative luminance (0.04045 threshold, note on the pre-May-2021 value 0.03928)
  • contrast ratio (L1 + 0.05) / (L2 + 0.05)
  • range 1:1 to 21:1, SC 1.4.1 Use of Color, 1.4.3 Contrast (Minimum) 4.5:1 and 3:1 for large-scale text (18 pt, or 14 pt bold)
  • 1.4.6 Contrast (Enhanced) 7:1 and 4.5:1, 1.4.11 Non-text Contrast 3:1, WCAG 2.1 published June 2018 (w3.org/TR/WCAG22/)
  • W3C, CSS Color Module Level 4, hex notation, hsl with 0deg red, 120deg green, 240deg blue, hsl(60deg 100% 50%) yellow vs hsl(240deg 100% 50%) blue at the same HSL lightness, hue spacing not perceptually uniform, HSL as a cylindrical model (w3.org/TR/css-color-4/)
  • OpenStax College Physics 2e, 26.3 Color and Color Vision: about 6 million cones of three types, red + green gives yellow, no unique set of primaries (openstax.org/books/college-physics-2e/pages/26-3-color-and-color-vision)
  • OpenStax Introduction to Behavioral Neuroscience, 6.2 The Retina: RGB pixels, CMY inks plus black, red-green colour blindness in about 8% of males (openstax.org/books/introduction-behavioral-neuroscience/pages/6-2-the-retina)
  • Smithsonian Libraries, Color in a New Light, The Science of Color: Newton's prism experiments from the 1660s, Opticks 1704, Le Blon's Coloritto (London, about 1722) and the distinction between material colours and coloured light (library.si.edu/exhibition/color-in-a-new-light/science)
  • The Newton Project (University of Oxford)
  • Opticks 1704, Book I Part II Prop. VI: colour circle divided into seven parts in musical proportion, white at the centre (newtonproject.ox.ac.uk/view/texts/normalized/NATP00034)
  • History of Information and Wikimedia Commons file record, Maxwell's 1861 Royal Institution demonstration, tartan ribbon photographed by Thomas Sutton through red, green and blue filters and projected together (historyofinformation.com/detail.php?id=3666)
  • LibreTexts, Coalinga College Drawing Basics 8.4 Color Theory: complementary, analogous, split-complementary, triadic, tetradic (human.libretexts.org)
  • RMIT Colour Theory, Interactive colour wheel and colour relationships (rmit.pressbooks.pub/colourtheory1)
  • University of Delaware Library, Graphic Design guide, Color Schemes, 60/30/10 rule (guides.lib.udel.edu/design/color).

Checked: 2026-10-17