Ellen Prager explains the camouflage methods of cephalopods (squids, octopuses, cuttlefishes, etc) in Sex, Drugs, And Sea Slime:
An intricate nervous system runs throughout a cephalopod’s epidermis, connecting its colored pigment organs and reflector cells to its relatively large brain and complex eyes. They are, in fact, the brainiest of all invertebrates, having the largest of the group along with especially well developed eyes. Hanlon’s research team has discovered that cephalopods use their exceptional vision as their primary means of detecting the brightness and patterns within their surroundings, which they then quickly replicate for camouflage. But, ironically, Hanlon’s team also found that most, if not all, cephalopods are color-blind. How then do they perfectly match the color of their surroundings? He suspects that the cephalopods’ skin has some sort of color-sensing capability, but what it is and how it works remain unknown.
(Video of a camouflaged octopus from oceanographer David Gallo's TED talk)