Here’s a nice overview from the Smithsonian Institute’s Ocean Portal about how octopus and squid are able to produce such an array of colors and patterns:
Many thousands of color-changing cells called chromatophores just below the surface of the skin are responsible for these remarkable transformations. The center of each chromatophore contains an elastic sac full of pigment, rather like a tiny balloon, which may be colored black, brown, orange, red or yellow. If you squeezed a dye-filled balloon, the color would be pushed to the top, stretching out the surface and making the color appear brighter—and this is the same way chromatophores work. A complex array of nerves and muscles controls whether the sac is expanded or contracted and, when the sac expands, the color is more visible. Besides chromatophores, some cephalopods also have iridophores and leucophores. Iridophores have stacks of reflecting plates that create iridescent greens, blues, silvers and golds, while leucophores mirror back the colors of the environment, making the animal less conspicuous.
Read the whole thing!