Images and colors

How does a computer store an image?

An image looks like a two dimensional array of pixels, nCols wide by nRows high, but it is generally stored in the computer as a one dimensional array of pixel values. The index into this array of a given pixel is:

row * nCols + col
In the illustration to the right, you can move your mouse over the image to select a single pixel. You can also select a pixel by moving your mouse over the linear array below the image. Each element of this array corresponds to exactly one pixel of the image.

How humans see colors

The human retina actually contains three different kinds of color receptor cells, each of which has a range of sensitivity to different parts of the visible light spectrum (which ranges from about 400 nanometers to 700 nm in wavelength). The illustration on the right shows an approximation to the color sensitivity of each of these types of receptors.

We approximate this in a computer by three numbers at each pixel: one for red, green and blue, respectively. But this is an imperfect approximation. In fact there are many combinations of [r,g,b] that the human eye cannot properly perceive, because our three types of color receptors overlap across the spectrum, as you can see by moving your mouse across the illustration to the right.

Complicating things even further, the "red", "green", and "blue" components of your computer screen each actually contain a mix of light frequencies.

Representing shapes with polygons


A unit cube is the cube that goes from -1.0 to +1.0 in all three dimensions. Its eight vertices are [-1,-1,-1], [-1,1,+1] ... [+1,+1,+1].

It is fairly easy to represent a unit cube with triangles.

Move your mouse over the illustration on the right to see one way to make a cube out of twelve triangles.


A cylinder can be approximated by triangles by constructing an approximation to a round tube and two approximately circular end caps.

Both the tube and the end caps will not truly be round, but rather will be built around an N-sided polygon which only approximates a circle. As N gets larger, the approximation will become better, while the cost of storing and computing the shape will go up.

You can see the appearance of different approximations by moving your mouse around the illustration to the right.


There are many ways to approximate a sphere with triangular meshes. One way is by building a longitude/latitude grid.

Another way to approximate a sphere with polygons is to subdivide each of the six faces of a unit cube into a mesh, and then "inflate" the cube.

This inflation is done by scaling the length of each vertex [x,y,z] of the cube so that (x2 + y2 + z2) is equal for all vertices.

After this is done, all vertices will be the same distance from the origin, and the cube will deform into a sphere.

This method has the advantage that it is very simple to implement, while producing relatively similar sized polygons everywhere on the sphere.

Finally, if you want the result to consist only of triangles, you can split each of the squares into two triangles along one of its long diagonals.