Covers that change with your theme
My site has seven colour themes, but a cover is one image. An AI model paints the light in two colours I never show, a script turns them into masks, and CSS recolours it to match the theme.

My site has seven colour themes. The text, the borders and even the code change colour with them. But a cover is one image, so a green cover stays green next to Dracula's purple.
I could make a copy for every theme, but that's seven images for every cover, at every size. Instead, I make one image and two small masks, and let CSS do the rest. Try it:

This post's own cover, at the top of the page, works the same way. Change the site's theme in the header and watch it.
The main idea is simple: the image says where the light is, and CSS decides what colour it is.
Why not hue-rotate()?
CSS can already change an image's colours with filter: hue-rotate(), but it turns every colour by the same amount. The ivory titles would change too. I also have two accents, the neon and one bright detail, and I need to change each one separately.
So I need to know exactly which pixels belong to each accent. That's what the masks are for.
Can the AI model make the mask?
I asked myself that first. Krea 2, the model I use, gives me one ordinary image, with no mask and no transparency. I could ask it for the mask next to the cover, or make it as a second image, but it would be a new drawing. Even small differences would leave coloured edges around the light.
There are also models that can find an object from a description, but they usually give you the outline of the object. Light doesn't stop at an outline: it glows, and it reflects on the floor.
So I don't ask the model for a mask. I ask it for a picture that's easy to turn into one. It's the same idea as a green screen. The actor isn't green, so anything green must be the background. In my covers, the machines are grey, black and white, the titles are ivory, and two bright colours mark the light. Nobody ever sees those two colours, so they don't have to look good, only be easy to find.
Finding the right two colours
I made the same three scenes with four colour pairs. Then I measured the hue of every clearly coloured pixel. A good pair makes two separate groups, with nothing else in between:
| Pair | What I measured |
|---|---|
| Teal + lime | Extra blue in one image (about 21% of its coloured pixels), and green-tinted glass |
| Magenta + lime | Two clean groups: 310° to 340° and 100° to 120° (no other 10° step had more than 1% of the coloured pixels) |
| Blue + lime | Two separate groups, but the light is dark and hard to see |
| Magenta + cyan | The colours moved a lot from one image to the next: the magenta went from 310° all the way past 0° to 20° |
I used magenta and lime. I'd worried that the magenta would tint the ivory titles, but their colour was almost the same with all four pairs.
From colours to masks
A script goes through every pixel and asks two questions: is this the right colour? and is it strong enough? That's hard to answer with red, green and blue values, because a bright magenta and a dark magenta have very different numbers.
OKLCH is a much better fit. It splits a colour into lightness, chroma (how strong the colour is) and hue (which colour it is, in degrees). Both magentas have almost the same hue. Greys, and even the ivory titles, have almost no chroma, so they're easy to leave out:
export function belongs(rgb: Rgb, { hue: [from, to], soft, chroma: [weak, strong] }: Range) {
const { chroma, hue } = chromaAndHue(rgb);
const middle = (from + to) / 2;
const away = Math.abs(((hue - middle + 540) % 360) - 180);
const half = (to - from) / 2;
return (1 - smoothstep(half, half + soft, away)) * smoothstep(weak, strong, chroma);
}- Hue is a circle: 350° and 10° are only 20° apart. The line with
% 360measures the distance the short way round. - The result isn't just yes or no: it's a number from 0 to 1.
smoothstepmakes it change gradually, so the masks have soft edges. Without that, the light would look cut out with scissors.
The result goes in the alpha channel of a small WebP file, which is what a CSS mask uses by default. The masks are 800 × 450, half the width and height of the cover. Each pair weighs only 12 to 21 KB. My first test, on older covers with teal light and green-tinted glass, needed 34 to 36 KB.
Recolouring with CSS
Each mask cuts out a layer of plain colour, and the layers go on top of the image:
.themed-cover {
--cover-neon: var(--brand);
--cover-detail: var(--code-type);
position: relative;
isolation: isolate;
}
.cover-neon {
position: absolute;
inset: 0;
background-color: var(--cover-neon);
mask-image: var(--neon-mask);
mask-size: cover;
mix-blend-mode: color;
}(The detail layer is the same, with its own colour and mask.)
mix-blend-mode: color takes the hue and saturation from the layer, and keeps the lightness of the image. A bright tube stays bright and its reflection stays dim, but both change colour. isolation: isolate stops the layers from blending with the rest of the page.
I didn't need any new colours. My themes already have a main colour (--brand) and one for types in code (--code-type). In the default theme they're phosphor green and teal, the colours I wanted.
The covers are always dark, even in light mode, so they need each theme's dark colours. I had already solved that for another feature: my tokens are set again on any element with data-theme, so adding data-theme="dark" to the cover is enough.
The cover is also the fallback
In my first test, the base image had grey lights, and CSS always added the colour. It worked, but if a mask didn't load, you'd see a cover with its lights off. It would also look wrong in link previews, which can't use my CSS.
So now the script paints the cover in the default theme's colours. It uses the formula from the spec for mix-blend-mode: color, so it should match what the browser does. That solves several problems:
- If a mask is missing or doesn't load, you still see a finished cover.
- Link previews and the CMS get a normal image.
- The default theme doesn't need the colour layers, so they stay at
display: none. In my tests, it never downloaded the detail mask. It still needs the neon one, for the glow below. - When printing, or when the browser uses its own high-contrast colours (
forced-colors), I hide the layers.
It isn't perfect. Where a mask is partly see-through, a little of the default green can show through the new colour. You can see it in the faintest floor reflections in Rosé Pine and Gruvbox, if you look closely. I think it's worth it for a safe fallback and less to download.
The mistake I measured
After the first version, the faintest reflections on the floor were still slightly purple. The mask only included stronger colours, so it missed them. I counted the leftovers: about 5% of the pixels were still purple. So I made the mask include weaker colours too, and only 0.15% were left. The number was much better.
Then I looked at the picture:
The whole floor was glowing. color gives a pixel the layer's full saturation, even if the original colour was very weak. A faint reflection ended up with as much colour as a neon tube.
I was trying to do two things with one mask. Adding the new colour needs a careful mask that follows how strong the light is. Removing the old colour needs to catch even the faintest trace, but should turn it grey, not into another colour. So now the script does them separately. It leaves just as little purple, and the floor is dark again.
I'm glad I had a number, because it helped me find the problem. But it also told me that the bad version was better. Measure it, but look at it too.
This is what Krea made, before any of that:

A glow that breathes
With a mask of the light, I can also animate it, without a GIF or any more frames. My first idea was to fade the colour layer in and out. But that wouldn't dim the light. It would show the green underneath. I needed another layer that adds light, using the same mask. The glow stays faint and slow until you point at the cover, and then it lights up:
/* Always on: a faint, slow loop. */
.cover-glow {
background-color: var(--cover-neon);
mask-image: var(--neon-mask);
mix-blend-mode: screen;
opacity: 0;
animation: cover-glow 6s ease-in-out infinite;
}
@keyframes cover-glow {
50% {
opacity: 0.2;
}
}
/* Only when you point at it: the same light, but stronger. */
.themed-cover::after {
content: '';
position: absolute;
inset: 0;
background-color: var(--cover-neon);
mask: var(--neon-mask) center / cover no-repeat;
mix-blend-mode: screen;
opacity: 0;
transition: opacity 900ms ease-out;
}
.themed-cover:hover::after {
opacity: 0.6;
transition-duration: 500ms;
}The glow uses two layers, because it does two jobs. I tried it with one. While an animation runs, it controls opacity, so a hover rule can't change it. Instead, I changed the two brightness levels the animation moved between, and used @property so they changed gradually. My test showed that both numbers changed smoothly, so I thought it worked.
But the light didn't change smoothly. If you pointed at the cover when the loop was at its dimmest, the brightness jumped from 0 to 0.55 almost at once. It then took almost three seconds to reach 0.8, its brightest. What you saw depended on where the loop was. I'd tested the settings, not what was on the screen. I only found the problem when I measured the opacity on every frame, starting from different points in the loop.
Now the loop never changes. The second layer fades in over half a second, stays at 0.6 while you point at the cover (at 1 it glared), and fades out more slowly, like a tube cooling down. Cards do the same when you reach them with the keyboard.
My first version pulsed twice and stopped after 4.8 seconds, because of WCAG 2.2.2. Something that moves or blinks on its own for more than five seconds, next to other content, needs a way to pause, stop or hide it. I liked the glow so much that I wanted it to keep going. WCAG describes blinking as changing back and forth in a way that's meant to draw attention. A faint six-second fade is meant to do the opposite, so I think it's fine, but that's my own reading, not a guarantee. If it were any stronger or faster, I'd add a pause button. With reduced motion on, the loop doesn't run. The cover still lights up when you point at it, but doesn't fade.
There's a cost too: every theme now downloads the neon mask, because the glow needs it.
After Krea, nothing is random
The AI model only makes the drafts, and choosing one is up to me. Everything after that is ordinary code:
pnpm covers draft change-with-the-theme --from 5001 # four drafts and a sheet to compare them
pnpm covers use change-with-the-theme 5002The second command makes the cover and both masks. I ran it twice on the same draft, and all three files were identical. Nothing redraws the picture either. I'd tried upscaling and refining with AI, but it changed the letters and swapped some objects. Now the drafts are already the final size, and the script only trims a few rows and changes the colour of the light. The previous post explains how I chose the style.
The older covers stay as they are. They use many colours, so there's no clean way to tell the light from everything else. That has to be planned before the image is made, and that's the part I'd tell anyone to start with: choose colours for the script first, and for your readers later.



