The first time I watched an octopus settle onto a patch of rubble, I missed the change. It was there on the left, mottled brown, and then the animal was on the right, the same dull grey as the stones under it. Nothing in between registered. The skin had not faded; it had rearranged itself, and it had done so faster than my eye could follow.
That performance runs on cells, not paint. An octopus does not carry a reservoir of pigment around and smear it on. Its skin is a stack of three different kinds of colour element, each with its own control mechanism, and the whole assembly is wired to a nervous system that is mostly not in the brain.

Three layers doing three different jobs
The outermost layer is made of chromatophores. A chromatophore is not a flat cell full of ink; it is a small elastic sac holding granules of pigment, with a set of radial muscles attached around its rim. When those muscles contract, the sac is pulled open into a disc, and the colour spreads across the skin. When the muscles relax, the sac springs back to a point and the colour disappears. Each one is effectively a pixel that can be switched on and off, and there are a very large number of them. Published estimates vary widely between species and between counting methods, which is why I avoid quoting a figure.
Below that sit the iridophores. These contain no pigment at all. They are packed with stacks of thin plates, and the spacing between the plates determines which wavelengths get reflected. Change the spacing and the reflected colour shifts. This is structural colour, the same physics that gives a soap bubble or a peacock feather its iridescence, and it is how an octopus produces blue and green in an animal that has no blue or green pigment.
The deepest layer holds leucophores, cells that scatter light across the whole visible range and therefore read as white. They are what allows a rapid pale phase, and because they scatter whatever light reaches them, they also help the skin blend with the ambient colour of the water rather than with a fixed palette.
The awkward part: it is colourblind
Here is the detail that makes the octopus a good teaching case. Behavioural tests and retinal anatomy both point to the same conclusion: octopus eyes carry a single class of photoreceptor. With one spectral channel, an animal cannot separate wavelength from brightness. It should be colourblind, and as far as the eye is concerned, it is.
Yet it matches background colours with unsettling accuracy, including the colour of algae, sand and coral. A colourblind animal solving a colour task is a genuine puzzle, and the explanations are still being argued over. One proposal is that the skin itself senses light. Octopus skin contains light-sensitive proteins, and pieces of skin kept alive in a dish have been shown to respond to illumination by expanding their chromatophores. That would give the animal a second, distributed channel for judging the light falling on its body.