📘 CodingMarble Learn

Colour, Image and Sound Coding

A screen makes colour from light with Red, Green and Blue (RGB). Each channel is a number from 0 to 255 (one byte), so a pixel takes 3 bytes and 16 777 216 colours are possible. Printers use ink (CMYK). HSL describes a colour by hue, saturation and lightness. A raster image is a grid of pixels; a vector image stores shapes and stays sharp at any size. Sound is coded by sampling: measuring the wave many times a second. Size = sample rate × bit depth × seconds × channels.

🎬 Step-by-step story

  1. A screen mixes light: red, green and blue together make white.
  2. Each colour gets a number from 0 to 255. Orange is (255, 128, 0), or #FF8000.
  3. Printers use ink: cyan, magenta, yellow and black (CMYK). HSL names a colour by its place on a colour ring.
  4. A raster picture is a grid of pixels. A vector picture stores a shape, such as one circle.
  5. Sound is a wave. The computer measures its height again and again: these are samples.
  6. Your turn: move the R, G and B sliders and watch the hex code, HSL and CMYK change.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why does yellow come from red and green light? It does not look like either.

Our eye has no yellow sensor. When the red and green sensors both fire, the brain calls it yellow. The screen just fires both.

Why 255 and not 100?

One byte has 8 bits, which count 0 to 255 (256 values). Computers like this size, so each channel uses exactly one byte.

Why is printed colour duller than my screen?

A screen makes its own light. Ink only reflects light and cannot be brighter than white paper. Some very bright screen colours are outside the range of CMYK ink.

Which is better, raster or vector?

Neither. Photos need raster. Logos and diagrams need vector. Choose by the job.

If sound is stored as separate measurements, why does it not sound broken?

There are 44 100 measurements every second, far too fast for the ear. The speaker joins them into a smooth wave.

How can I get grey on a screen?

Give R, G and B the same number, for example (128, 128, 128).

RGB: how screens make colour

Your eye has three kinds of colour sensors: for red, green and blue light. A screen copies this. Every tiny dot (pixel) has three small lights: R, G and B. By making each light brighter or dimmer, the pixel shows any colour.

This is called additive mixing, because adding more light makes the colour brighter.

Colour numbers: 0 to 255, bytes and hex codes

Each of R, G and B gets 1 byte (8 bits). A byte can hold 0 to 255. So one pixel needs 3 bytes = 24 bits. This is called 24-bit colour (or true colour).

Number of colours = 256 × 256 × 256 = 16 777 216.

Web pages write the three numbers in hexadecimal (base 16), two digits each: #RRGGBB. For example 255 is FF, 128 is 80, 0 is 00. So (255, 128, 0) = #FF8000 (orange). Equal numbers give grey: (100, 100, 100).

CMYK: how printers make colour

Paper makes no light. It only reflects it. Ink works by soaking up some colours of light. So printers use subtractive mixing with four inks: Cyan, Magenta, Yellow and Key (black).

Amounts are written as percentages. Orange (255, 128, 0) is about C 0%, M 50%, Y 100%, K 0%. Some bright screen colours cannot be printed exactly. That is why a printed picture can look a little duller.

HSL: describing colour the way people think

HSL gives a colour three easy numbers:

Artists like HSL because "make it a little lighter" means just changing L. Orange (255, 128, 0) is H 30°, S 100%, L 50%.

Raster and vector images

A raster (bitmap) image is a grid of pixels, each with its own colour code. Photos are raster. File size = width × height × bits per pixel. Zoom in and you see blocks, because there is no more detail than the pixels.

A vector image stores shapes with numbers: a circle with centre, radius and colour; a line with two end points. The computer draws the shape fresh at any size, so it never becomes blocky. The file is usually small.

RasterVector
Storespixelsshapes
Zoomed inblockysharp
Best forphotoslogos, maps, diagrams
ExamplesJPG, PNGSVG

Sampling: turning sound into numbers

Real sound is a smooth wave in the air. A computer can only keep numbers. So it uses sampling: many times every second it measures the height of the wave and stores that number.

More samples and more bits copy the wave better, but the file gets bigger. A sample rate of 44 100 Hz is enough because people hear up to about 20 000 Hz, and we need a little more than twice the highest sound to copy it well.

Size (bits) = sample rate × bit depth × seconds × channels.

Try it yourself

In the 3D: in the last step make pure yellow (255, 255, 0), then grey (all three equal). In the sampling step, slide the samples up and down and watch the orange line come closer to the blue wave.

At home: open any colour picker in a drawing app or on a web page. Type #FF0000, #00FF00, #0000FF, then #808080. Guess each colour before you press enter.

Key formulas and definitions

Worked examples

1. Write red (255, 0, 0) as a hex code.

255 = FF, 0 = 00, 0 = 00. So red = #FF0000.

2. How many bytes does a 200 × 100 image with 24-bit colour need?

Pixels = 200 × 100 = 20 000. Each pixel = 3 bytes. 20 000 × 3 = 60 000 bytes = 60 kB.

3. Change orange (255, 128, 0) to CMYK percentages.

K = 1 − 255/255 = 0. C = 1 − 255/255 = 0%. M = 1 − 128/255 = about 50%. Y = 1 − 0 = 100%. CMYK = (0, 50, 100, 0).

4. Find the HSL of orange (255, 128, 0).

Red is the largest and blue the smallest, so the hue lies between red (0°) and yellow (60°). Green is half-way up, so H is about 30°. The colour is pure (S = 100%), and L = (max + min) ÷ 2 = (255 + 0) ÷ 2 ÷ 255 = 50%. HSL = (30°, 100%, 50%).

5. Mono sound at 44 100 Hz, 16 bits, 10 seconds. How big is it?

44 100 × 16 × 10 = 7 056 000 bits. ÷ 8 = 882 000 bytes = 882 kB.

6. A school needs one logo for a pen and a huge flex banner. Which image type should it keep?

A vector file. It stays sharp at any size and the file is small. A raster logo would look blocky on the big banner.

Common mistakes

Practice quiz

1. Which colours does a screen mix?
2. How many bytes does one 24-bit pixel use?
3. Which image type stays sharp when enlarged?
4. In CMYK, K stands for:
5. Sampling a sound means:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between RGB and CMYK?

RGB mixes light and is used on screens; the more light, the brighter. CMYK mixes ink and is used in printing; the more ink, the darker.

What is the difference between raster and vector graphics?

Raster images are grids of pixels and get blocky when enlarged. Vector images store shapes and stay sharp at any size.

What is sampling in sound?

Sampling means measuring the height of a sound wave many times each second and saving every measurement as a number.

Learn first

Learn next

Related lessons

All Computer Science lessons