The Human Eye in JavaScript – Or Why Ain’t The Sky Violet?

Visible light

So there’s a famous question: why is the sky blue? Now usually the answer we are told is that bluer light scatters more than redder light in Rayleigh scattering off gas molecules in the atmosphere, which is the dominant effect which gives the sky any colour at all.

Rayleigh scattering refers to scattering off particles smaller than the wavelength of the light. We’re talking wavelengths of hundreds of nanometres for visible light, scattering off gas molecules which are hundreds of picometres in size, i.e. 1000 times smaller. The closer the wavelength is to size of the scattering particles, the easier and so more likely scattering is. Hence shorter, bluer wavelengths scatter more often.

Rayleigh scattering
Rayleigh scattering scatters shorter wavelength light more strongly

There is also Mie scattering off things like small water droplets which are of a similar size to the wavelength of the light; this does not depend so strongly on the wavelength, explaining why clouds are grey or white.

An Infamous Question

xkcd comic: how come the sky isn't violet?
Why isn’t the sky violet? [xkcd]

But if that’s a famous question, then there’s an infamous question: how come the sky isn’t violet?

First things first: yes, the sun does emit more strongly in the range of blue- rather than violet-frequency light. But we know from rainbows, for example, that there is definitely enough violet light in what the sun radiates to be visible. And Rayleigh scattering really does reshape the solar spectrum to be higher in violet regions then blue ones. So how come we do not notice a violet colour?

The answer lies partially in how our eyes and brain perceive colour, and since this is kinda interesting, I’m using this as an excuse to make a toy to play with to try and convince ourselves this really is basically how it works!

But first, a TL;DR answer:

TL;DR: The violet bit doesn’t add up to much, because there’s so much blue and other stuff

The sky isn’t violet because only a small amount of all the visible light scattered from the sky is in the small range we perceive as violet; most of it is smeared broadly around the blue-ish bit, with plenty of green too. So we just see a whiter version of blue.

This doesn’t contradict individual violet frequencies being stronger than any individual other frequencies. (a) There’s just a much wider range that we perceive as roughly blue than there is specifically violet; and (b) our eyes are absorbing lots of light from right across the rest of the spectrum too. It’s like noticing that one timpani is louder than one viola, and then standing in the middle of a viola section playing fortissimo in your ear whilst the timpanist goes at it next to you. You don’t go “huh, this timpani solo is loud”.

Basically, there needs to be a lot more violet frequency light than anything else in order for us to notice it as the dominant colour: we don’t see very much redness from most high-frequency light at all, but we do see lots of blueness from all of it. (Plus lots of greenness from only slightly longer wavelength light which is often present.)

Hence the light has to be very pure violet light. The prototypical natural examples are rainbows and prismatic crystals: here, there is one specific angle at which only light at violet frequencies emerges. Then, we can see it, as clear as day (so to speak).

Human Colour Perception

So as you probably know, our eyes contain rod and cone cells. The former are optimized for detecting low levels of light and do not contribute to colour perception (hence the wonderfully named Purkinje effect in which night vision becomes monochromatic), whilst the latter come in different varieties to allow us to perceive different levels of light in different parts of the spectrum.

They achieve this by containing proteins which respond more strongly to light in various patches of the visible spectrum. The typical human set up has three different types, L-, M- and S-cones, often thought of as “red”, “green” and “blue”.

But really they are each sensitive to light over a lot of the visible range. And, for instance, the cones sensitive to the longest wavelengths, the L-cones, actually have a peak at the frequency of yellow light, at about 570nm.

The spectral sensitivity of the human eye!
The spectral sensitivity of the human eye!

And all they can do is just see how much in total they are stimulated by all the different light which they are sensitive to. So a colour is a certain amount of “L”-ness, “M”-ness and “S”-ness.

This is still enough for us to be able to differentiate points on the spectrum reasonably well, though; red light means that L cones fire very intensely and M and S cones don’t. Even though L cones are more sensitive to yellow light than red light, the absence of M and S signals tells us we are perceiving red light in this situation.

So what is happening at low wavelengths, and high frequencies? This is a region where short wavelength S signals are detected, but M signals are again dropping off. We perceive this as violet light. There’s a strangeness here: we perceive violet to be similar to blue with some red in it. Why?

My brief Googling earlier was inconclusive, so here are two options:

  • This might actually happen at a chemical level: the L cones have some notable sensitivity to very short wavelength light as well as the expected stronger long wavelength sensitivity.
  • Alternatively, this bump in red sensitivity may occur in post-production instead: [high S, low M and L] content can simply be wired to be perceived as [high S and L, low M] content. We know the brain computes a quantity x measuring blue vs red-green, so it might just be choosing to interpret high values of this x as indicating extra “redness”.

Whatever the mechanics, this bump is reflected in my crude model below, and clearly we do indeed get fooled by computer screens etc. into thinking a particular red-blue mixture is actually violet light. (This oddity arguably actually gives a nicer symmetry to colour perception in that it allows us to loop the spectrum back round to itself much more straightforwardly.)

So why isn’t the sky violet? Well, the spectrum of light our eyes absorb from the sky is actually still very broad. It is stronger in the violet than the blue, sure. But there is still plenty of light at frequencies which will stimulate the other cones. And most of it is over the range stimulating L cones, with significantly smaller amounts in total over the ranges of sensitivity of other cones (even though the strongest signal at any point is at a violet point). Scroll to the end of the post to see what this looks like.

So our TL;DR answer above is basically fine!

A Toy

This is a simple bit of JavaScript which conducts a very crude simulation of human colour perception. It allows you to specify some incident light:

  • You can use a black body spectrum of light as generated by an object of some temperature (the sun is roughly a black body at 5780K). Optionally, you can also impose a Rayleigh-scattering effect (multiplying the spectrum by a power of the wavelength, \lambda^{-4}), or a partial mix of Rayleigh scattered light and the original spectrum (as you might see if there was some obscuring water vapour).
  • Alternatively, you can send in a tight band of light clustered around a specific wavelength, to simulate the pure spectrum you’d expect to see through a prism.

It then displays some output:

  • The shape of the spectrum as a function of wavelength in nanometres is displayed as a dotted line, superimposed against the sensitivities of the three types of cone cell we’re simulating.
  • The colour obtained by using a naive approach to work out the hue of the light as sensed by these cells. (For each of the three cells, the weighted average stimulation across its sensitive range is computed. These numbers are thought of as R, G, B values. We then rescale so that the largest is 1, and display that colour.)
  • You can also (Guy Emerson’s suggestion!) crudely simulate red-green colour blindness by averaging the red & green values in the resulting colour. Notice how difficult this makes distinguishing pure spectrum tones from 510nm upwards.

(Note the subtleties here: your computer screen can only output RGB colours, so in the display box we’re trying to stimulate your L-, M- and S- cones the same amount as the received light would! You can tie your brain in knots thinking about this if you’re not careful.)

You can use the arrow keys to move the sliders, to see either a spectrum of violet through red light; or the colours of objects as you heat them from red-hot, through white-hot, up to blue-hot.

This is obviously an incredibly crude model, but hopefully the results are realistic enough that you can see what is going on: as promised, most of the light absorbed is absorbed across a range seen as blue. The small contribution at the very end of the visible spectrum, in the violet, is not enough to overwhelm the general sense we get of blueness. Enjoy playing!

  • By the way, as a quick note: this is roughly speaking what cameras do! The chips contain little cells which accumulate light of different colours with varying sensitivity and report how excited they are to the processor. Google “digital camera spectral response” to read about this, there are various pages with graphs very like the ones you will play with below!

The toy!

Incident light


Spectrum, sensitivities and perception

Perceived colour:


Published by


Carl Turner is a theoretical physics PhD student in DAMTP, University of Cambridge.

Leave a Reply

Your email address will not be published. Required fields are marked *