Finding a site's brand color from the console, and why painted area points the wrong way
If you want to match a site's look, the first color you need is its brand color: the one on the main button, the links, the logo. Finding it by eye is usually easy. Finding it with code is where I went wrong twice, so here is what I measured on three fictional demo sites I built (Northwind Roasters, a coffee shop; Ledgerline, fintech; Afterhours, a dark developer tool) and the snippet I ended up with.
The obvious approach, and what it got wrong
My first try summed up the area of every element with an opaque background-color, grouped by color. On Northwind it said the top color was #f6eadb at 49%, then white at 45%. On Afterhours, #17141f at 97%.
Both are wrong, for two reasons:
- Nested boxes count twice. A card inside a section inside a wrapper adds its area three times if all three have a background. The totals mean nothing.
- The page background was missing. I looped over
document.body.querySelectorAll('*'), which skips<body>and<html>, and that's where most sites set the page color. Northwind's real background,#fffaf3, didn't even make the top three.
Sampling what is actually painted
The fix is to ask the browser what's on top at each point, then walk up to the first ancestor that paints a background. The snippet scrolls through the page one screen at a time and samples a grid every 16px:
async function brandColor(step = 16) { const lin = (c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4); const chroma = ([r, g, b]) => { // OKLCH chroma [r, g, b] = [r, g, b].map((v) => lin(v / 255)); const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b); const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b); const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b); return Math.hypot(1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s); }; const rgb = (c) => { const m = c.match(/rgba?\(([\d.]+), ([\d.]+), ([\d.]+)(?:, ([\d.]+))?\)/); return m && (m[4] ?? 1) > 0.5 ? [+m[1], +m[2], +m[3]] : null; }; const hex = (c) => '#' + c.map((v) => Math.round(v).toString(16).padStart(2, '0')).join(''); const paintedBg = (el) => { // first ancestor with an opaque background for (; el; el = el.parentElement) { const c = rgb(getComputedStyle(el).backgroundColor); if (c) return c; } return [255, 255, 255]; // the canvas default }; const seen = new Map(), clicks = new Map(); const add = (m, k) => m.set(k, (m.get(k) || 0) + 1); for (let y = 0; y < document.documentElement.scrollHeight; y += innerHeight) { scrollTo(0, y); await new Promise(requestAnimationFrame); for (let py = 0; py < innerHeight; py += step) for (let px = 0; px < innerWidth; px += step) { const el = document.elementFromPoint(px, py); if (el) add(seen, hex(paintedBg(el))); } } scrollTo(0, 0); for (const el of document.querySelectorAll('a, button, [role=button]')) { if (!el.getClientRects().length) continue; const cs = getComputedStyle(el); for (const c of [rgb(cs.backgroundColor), rgb(cs.color)]) if (c && chroma(c) > 0.05) add(clicks, hex(c)); // skip greys } const total = [...seen.values()].reduce((a, b) => a + b, 0); const top = (m) => [...m].sort((a, b) => b[1] - a[1]).slice(0, 4); console.log('Painted background:', top(seen).map(([h, n]) => `${h} ${(100 * n / total).toFixed(1)}%`).join(', ')); console.log('Saturated colors on links and buttons:', top(clicks).map(([h, n]) => `${h} x${n}`).join(', ')); } brandColor(); The second loop is the part that finds the brand color: it collects the background and text colors of links and buttons, keeping only saturated ones (OKLCH chroma above 0.05, so greys, black and white drop out).
What it found
At 1280px wide:
| Site | Painted background (share of samples) | Saturated colors on links and buttons |
|---|---|---|
| Northwind | #fffaf3 64.1%, #ffffff 21.4%, #f6eadb 12.0%, #c2410c 1.5% | #c2410c ×3, #4d7c5a ×1 |
| Ledgerline | #ffffff 87.7%, #f1f5f9 10.9%, #1d4ed8 1.0%, #ecfccb 0.4% | #1d4ed8 ×2 |
| Afterhours | #0c0a14 75.9%, #17141f 23.5%, #8b5cf6 0.5% | #8b5cf6 ×1 |
At 390px the shares move a little (the brand color goes up to 2.1% on Northwind and 1.5% on Afterhours, because buttons take more of a narrow screen) and the clickable colors are the same.
Three things stand out:
- The brand color is a tiny share of the paint. 0.5% to 2.1% across the three sites. Any approach that ranks colors by how much of the page they cover will put it fourth or lower, behind the background and surfaces.
- The painted ranking is still useful, just for different roles. First is the page background, second the surface (cards, panels, alternating sections). That matches how I built the sites.
- Clickable elements name the brand color every time. On all three, the most common saturated color on links and buttons is the one I chose as primary. On Northwind the second one, the sage green
#4d7c5a, is the accent.
Where this will fail
My demo pages have only a few buttons each, so the counts are small. On real sites, check these before you trust the result:
- Default link blue. A site that never styled its links will show
#0000eeas a clickable color. If it shows up, ignore it. - Image buttons and gradients. A button painted with a background image or gradient has a transparent
background-color, so the snippet only sees its text color. - Accent colors that aren't clickable. Ledgerline's lime accent
#65a30dis used for small highlights, not links, so it doesn't appear in the clickable list. The#ecfccbin its painted list is a pale tint of it.
For a quick answer the snippet is enough: first painted color is the background, second is the surface, top clickable saturated color is the brand.
I made a Chrome extension, Stylasso, that does this kind of role guessing for the whole palette (plus fonts and spacing) and exports it as CSS variables or a Tailwind theme; the guide is here: extract the colors from any website, with roles.
Written with help from an AI assistant. Every number comes from running the snippet on the three demo pages in headless Chromium.