Pigment sacs under white skin, a lagging controller and three kinds of seabed.
The science
Skin optics. Cephalopod skin is a white reflecting layer (leucophores and iridophores, albedo ρ0 about 0.9) under stacked sacs of pigment: yellow, red and brown/black chromatophores. A muscle-driven sac expands from a dot to a disc, so each class covers a fraction a of the skin. With Beer-Lambert absorption the reflected colour is
c = ρ0 ∘ exp(-A a), where the columns of A are the optical densities of the three classes in R, G, B. Yellow mostly absorbs blue, red absorbs green and blue, brown absorbs everything.
Matching. The animal sees the ground; the controller inverts the model, a* = A-1 ln(ρ0 / T), clamps each coverage to 0..1, and the muscles relax toward a* with a first-order lag da/dt = (a* - a) / τ. Colours darker than the pigments allow or brighter than ρ0 cannot be reached; those patches show up as colour error. The error is CIE76 ΔE in L*a*b* (about 2.3 is a just-noticeable difference).
Acuity and depth. Real octopuses do not copy every pebble; the acuity setting blurs the ground colour before matching. Water absorbs red light fastest (about 0.45 per metre against 0.02 for blue); depth tints the light on both the skin and the ground, so the ratio, and therefore the match, is unchanged while the whole scene turns blue.
Simplifications. Colour only: no skin texture (papillae), no polarisation or iridescence, no stereo vision or predator model. The pigment densities and the albedo are plausible round numbers, not measured spectra. The controller is an ideal inverse model; a real animal estimates the background with a visual system that is far from perfect. Matching is evaluated per skin vertex from the top-down colour of the ground beneath it.