Pick brand colours, not background tints, from page logos
Rank by vivid/dark pixels, average within coarse bins, and drop same-hue anti-aliasing blends. On the 4Tech logo this yields red + charcoal instead of white/pink fringe colours. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,7 @@ function hex(r: number, g: number, b: number) {
|
||||
return `#${[r, g, b].map((value) => value.toString(16).padStart(2, "0")).join("").toUpperCase()}`;
|
||||
}
|
||||
|
||||
/** Extracts a small, stable palette from a Facebook page logo without exposing the image to the model. */
|
||||
/** Extracts the brand colours (up to 4) from a Facebook page logo without exposing the image to the model. */
|
||||
export async function logoPalettePrompt(pictureUrl: string | null): Promise<string> {
|
||||
if (!pictureUrl) return "";
|
||||
const cached = cache.get(pictureUrl);
|
||||
@@ -20,20 +20,38 @@ export async function logoPalettePrompt(pictureUrl: string | null): Promise<stri
|
||||
if (source.length > 2_000_000) return "";
|
||||
const { data, info } = await sharp(source, { limitInputPixels: 4_000_000 })
|
||||
.ensureAlpha().resize(48, 48, { fit: "inside", withoutEnlargement: true }).raw().toBuffer({ resolveWithObject: true });
|
||||
const bins = new Map<string, { count: number; rgb: [number, number, number] }>();
|
||||
// Logos are small and mostly background, so rank by vivid or dark pixels, not by raw count.
|
||||
// Averaging within coarse bins merges JPEG noise; anti-aliased blends of a brand colour
|
||||
// and the background (same hue, lighter) are dropped so only real brand colours remain.
|
||||
const bins = new Map<string, { count: number; sum: [number, number, number] }>();
|
||||
let kept = 0;
|
||||
for (let i = 0; i < data.length; i += info.channels) {
|
||||
const [r, g, b, a = 255] = [data[i], data[i + 1], data[i + 2], data[i + 3]];
|
||||
if (a < 128 || (r > 242 && g > 242 && b > 242)) continue;
|
||||
const rgb: [number, number, number] = [r >> 4 << 4, g >> 4 << 4, b >> 4 << 4];
|
||||
const key = rgb.join(",");
|
||||
const entry = bins.get(key) ?? { count: 0, rgb };
|
||||
if (a < 128) continue;
|
||||
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||
if (min > 170 || (max > 200 && (max - min) / max < 0.35)) continue; // white / pale background and tints
|
||||
const key = `${r >> 5},${g >> 5},${b >> 5}`;
|
||||
const entry = bins.get(key) ?? { count: 0, sum: [0, 0, 0] as [number, number, number] };
|
||||
entry.count++;
|
||||
entry.sum[0] += r; entry.sum[1] += g; entry.sum[2] += b;
|
||||
bins.set(key, entry);
|
||||
kept++;
|
||||
}
|
||||
// Hue family in 30-degree steps; every low-saturation colour is one "neutral" family.
|
||||
const family = ([r, g, b]: [number, number, number]) => {
|
||||
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||
if (max === 0 || (max - min) / max < 0.2) return -1;
|
||||
const d = max - min;
|
||||
const h = max === r ? ((g - b) / d + 6) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
|
||||
return Math.round(h * 60 / 30) % 12;
|
||||
};
|
||||
const candidates = [...bins.values()]
|
||||
.map((entry) => ({ count: entry.count, rgb: entry.sum.map((v) => Math.round(v / entry.count)) as [number, number, number] }))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
const selected: [number, number, number][] = [];
|
||||
for (const entry of [...bins.values()].sort((a, b) => b.count - a.count)) {
|
||||
if (selected.every((color) => color.reduce((sum, value, index) => sum + (value - entry.rgb[index]) ** 2, 0) > 42 ** 2)) selected.push(entry.rgb);
|
||||
if (selected.length === 4) break;
|
||||
for (const entry of candidates) {
|
||||
if (entry.count < Math.max(3, kept * 0.03)) break;
|
||||
if (selected.length < 4 && selected.every((color) => family(color) !== family(entry.rgb))) selected.push(entry.rgb);
|
||||
}
|
||||
const value = selected.length ? `<logo_palette>Dominant colours sampled from the page logo: ${selected.map((color) => hex(...color)).join(", ")}. Use these as supporting brand evidence; an explicit visual-style field takes priority.</logo_palette>` : "";
|
||||
cache.set(pictureUrl, { expiresAt: Date.now() + 60 * 60_000, value });
|
||||
|
||||
Reference in New Issue
Block a user