Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(branding): body text color, logo color as primary, monochrome cleanup (#5285)
Abimael Martell946bbd45e495
diff --git a/apps/api/src/__tests__/lib/branding/color-roles.test.ts b/apps/api/src/__tests__/lib/branding/color-roles.test.tsindex e2dcff155..bd4297c9e 100644--- a/apps/api/src/__tests__/lib/branding/color-roles.test.ts+++ b/apps/api/src/__tests__/lib/branding/color-roles.test.ts@@ -307,6 +307,59 @@ describe("monochrome brands", () => {expect(merged.colors?.primary).toBe("#635BFF");});+ it("drops saturated colors no button uses, but keeps the displaced link color", () => {+ const merged = mergeBrandingResults(+ { colorScheme: "light", colors: {} },+ {+ ...llmPicking("#0072F5"),+ colorRoles: {+ ...llmPicking("#0072F5").colorRoles,+ secondaryColor: "#FFC96B",+ accentColor: "#9A050F",+ },+ },+ [button("#171717", "#FFFFFF", 0), button("#FFFFFF", "#171717", 1)],+ );+ expect(merged.colors?.primary).toBe("#171717");+ expect(merged.colors?.accent).toBe("#171717");+ expect(merged.colors?.secondary).toBeUndefined();++ const linkOnly = mergeBrandingResults(+ { colorScheme: "light", colors: {} },+ llmPicking("#0072F5"),+ [button("#171717", "#FFFFFF", 0)],+ );+ expect(linkOnly.colors?.accent).toBe("#0072F5");+ });++ it("keeps saturated accents when the primary is navy chrome or there is no black call to action", () => {+ const navy = mergeBrandingResults(+ { colorScheme: "light", colors: {} },+ {+ ...llmPicking("#061B31"),+ colorRoles: {+ ...llmPicking("#061B31").colorRoles,+ accentColor: "#635BFF",+ },+ },+ [button("#061B31", "#FFFFFF", 0)],+ );+ expect(navy.colors?.accent).toBe("#635BFF");++ const noButtons = mergeBrandingResults(+ { colorScheme: "light", colors: {} },+ {+ ...llmPicking("#111111"),+ colorRoles: {+ ...llmPicking("#111111").colorRoles,+ accentColor: "#E51503",+ },+ },+ [],+ );+ expect(noButtons.colors?.accent).toBe("#E51503");+ });+it("leaves dark pages alone", () => {const merged = mergeBrandingResults({ colorScheme: "dark", colors: {} },@@ -384,3 +437,96 @@ describe("near-white primaries", () => {).toBe("#E7DBFF");});});++describe("logo color and measured text color", () => {+ const logo = "https://acme.test/logo.svg";+ const candidate = (colors: Array<{ color: string; share: number }>) =>+ ({+ src: logo,+ alt: "Acme",+ isSvg: true,+ isVisible: true,+ location: "header",+ position: { top: 10, left: 10, width: 120, height: 32 },+ indicators: {+ inHeader: true,+ altMatch: true,+ srcMatch: true,+ classMatch: false,+ hrefMatch: true,+ },+ source: "img",+ colors,+ }) as any;+ const llm = (primaryColor: string, textPrimary = "#111111") => ({+ cleanedFonts: [],+ buttonClassification: {+ primaryButtonIndex: -1,+ primaryButtonReasoning: "n/a",+ secondaryButtonIndex: -1,+ secondaryButtonReasoning: "n/a",+ confidence: 0,+ },+ colorRoles: {+ primaryColor,+ accentColor: "",+ backgroundColor: "#FFFFFF",+ textPrimary,+ confidence: 0.8,+ },+ });++ it("takes the logo's color as primary when the extracted one is pale or far from it", () => {+ const pale = mergeBrandingResults(+ {+ colorScheme: "light",+ colors: { primary: "#FFE6AA" },+ images: { logo },+ },+ llm("#FFE6AA"),+ [],+ [+ candidate([+ { color: "#0071DC", share: 0.6 },+ { color: "#FFC220", share: 0.3 },+ ]),+ ],+ );+ expect(pale.colors?.primary).toBe("#0071DC");++ const far = mergeBrandingResults(+ { colorScheme: "light", colors: {}, images: { logo } },+ llm("#F6339A"),+ [],+ [candidate([{ color: "#00A6F4", share: 0.8 }])],+ );+ expect(far.colors?.primary).toBe("#00A6F4");+ });++ it("keeps a primary that is the logo's color, and ignores a black logo", () => {+ const close = mergeBrandingResults(+ { colorScheme: "light", colors: {}, images: { logo } },+ llm("#0074E0"),+ [],+ [candidate([{ color: "#0071DC", share: 0.9 }])],+ );+ expect(close.colors?.primary).toBe("#0074E0");++ const black = mergeBrandingResults(+ { colorScheme: "light", colors: {}, images: { logo } },+ llm("#E51503"),+ [],+ [candidate([{ color: "#000000", share: 1 }])],+ );+ expect(black.colors?.primary).toBe("#E51503");+ });++ it("keeps the measured neutral text color over a saturated pick", () => {+ const merged = mergeBrandingResults(+ { colorScheme: "light", colors: { textPrimary: "#202122" } },+ llm("#3366CC", "#3366CC"),+ [],+ );+ expect(merged.colors?.textPrimary).toBe("#202122");+ });+});diff --git a/apps/api/src/__tests__/lib/branding/processor-text-color.test.ts b/apps/api/src/__tests__/lib/branding/processor-text-color.test.tsindex 83c3143f0..f06d5bab1 100644--- a/apps/api/src/__tests__/lib/branding/processor-text-color.test.ts+++ b/apps/api/src/__tests__/lib/branding/processor-text-color.test.ts@@ -52,6 +52,30 @@ const raw = (}) as any;describe("text color heuristic", () => {+ it("takes the body text color, not the link color, on link-heavy pages", () => {+ const links = Array.from({ length: 10 }, (_, i) => ({+ ...snap({+ tag: "a",+ text: `Article ${i}`,+ textColor: "rgb(51, 102, 204)",+ }),+ isLink: true,+ }));+ const body = [+ snap({ tag: "h1", text: "Welcome", textColor: "rgb(32, 33, 34)" }),+ snap({ text: "The free encyclopedia", textColor: "rgb(32, 33, 34)" }),+ ];++ const profile = processRawBranding(+ raw([...links, ...body], {+ background: "rgb(255, 255, 255)",+ scheme: "light",+ }),+ );++ expect(profile.colors?.textPrimary).toBe("#202122");+ });+it("takes the text color from the page's text, not from button fills", () => {const buttons = Array.from({ length: 8 }, (_, i) =>snap({diff --git a/apps/api/src/lib/branding/merge.ts b/apps/api/src/lib/branding/merge.tsindex 45ffadb90..26549b90d 100644--- a/apps/api/src/lib/branding/merge.ts+++ b/apps/api/src/lib/branding/merge.ts@@ -2,6 +2,8 @@ import { BrandingProfile } from "../../types/branding";import {colorDistance,isBrowserLinkColor,+ isGrayish,+ isNearBlack,isNeutralDark,isUsableBrandPrimary,normalizeRoleHex,@@ -305,7 +307,11 @@ export function mergeBrandingResults(...(llmSecondary ? { secondary: llmSecondary } : {}),accent: llmAccent || merged.colors?.accent,background: llmBackground || merged.colors?.background,- textPrimary: llmText || merged.colors?.textPrimary,+ // Body text is written in a neutral; a saturated pick is usually the+ // link color, so the measured neutral text color stays.+ textPrimary: keepsMeasuredText(llmText, merged.colors?.textPrimary)+ ? merged.colors?.textPrimary+ : llmText || merged.colors?.textPrimary,};// When the LLM omits secondaryColor, remove the JS-heuristic secondary@@ -337,6 +343,7 @@ export function mergeBrandingResults(merged.components?.buttonPrimary?.background,);const primaryColor = normalizeRoleHex(merged.colors?.primary);+ let displaced: string | undefined;if (merged.colorScheme !== "dark" &&primaryFill &&@@ -354,8 +361,16 @@ export function mergeBrandingResults(primary: primaryFill,accent: merged.colors?.accent ?? primaryColor,};+ displaced = primaryColor;}+ applyLogoAndMonochromeColors(+ merged,+ buttonSnapshots,+ logoCandidates,+ displaced,+ );+if (llm.personality) {(merged as any).personality = llm.personality;}@@ -446,3 +461,78 @@ export function mergeBrandingResults(return merged;}++const isNeutral = (hex: string) => isGrayish(hex) || isNearBlack(hex);++function keepsMeasuredText(llmText?: string, measured?: string | null) {+ const heuristic = normalizeRoleHex(measured);+ return (+ !!llmText && !!heuristic && !isGrayish(llmText) && isGrayish(heuristic)+ );+}++/**+ * The logo is the most reliable brand evidence: a clear color in the chosen+ * logo is the primary when the extracted one is neutral, pale or far from it.+ * A brand whose primary stays neutral (black call to action) is monochrome: a saturated accent, secondary or link no button uses then comes+ * from imagery or CSS presets, and the neutral takes its place. The color the+ * monochrome rule moved out of primary (the brand's link color) is kept.+ */+function applyLogoAndMonochromeColors(+ merged: BrandingProfile,+ buttonSnapshots: ButtonSnapshot[],+ logoCandidates: LogoCandidate[] | undefined,+ displaced: string | undefined,+): void {+ if (!merged.colors) return;+ const scheme = merged.colorScheme;+ const logoSrc = merged.images?.logo;+ const logo = logoSrc+ ? logoCandidates?.find(c => c.src === logoSrc)+ : undefined;+ const logoColors = (logo?.colors ?? [])+ .map(c => ({ hex: normalizeRoleHex(c.color), share: c.share }))+ .filter((c): c is { hex: string; share: number } => !!c.hex);+ const onButton = (hex: string) =>+ buttonSnapshots.some(+ b =>+ colorDistance(b.background, hex) < 40 ||+ colorDistance(b.borderColor, hex) < 40,+ );++ const logoBrand = logoColors.find(+ c => c.share >= 0.25 && isUsableBrandPrimary(c.hex, scheme),+ );+ const primary = normalizeRoleHex(merged.colors.primary);+ if (+ logoBrand &&+ (!primary ||+ !isUsableBrandPrimary(primary, scheme) ||+ logoColors.every(c => colorDistance(c.hex, primary) > 60))+ ) {+ merged.colors = { ...merged.colors, primary: logoBrand.hex };+ return;+ }++ // A black or white logo alone can't tell a brand accent (Bloomberg's red)+ // from campaign imagery, and a gray or navy primary may just be chrome, so+ // only a neutral black primary that is also the primary button's fill (the+ // black call to action above) marks the brand as monochrome.+ const neutral =+ primary &&+ isNeutralDark(primary) &&+ colorDistance(merged.components?.buttonPrimary?.background, primary) < 40+ ? primary+ : undefined;+ if (!neutral || scheme === "dark") return;++ const incidental = (value?: string | null) => {+ const hex = normalizeRoleHex(value);+ return !!hex && hex !== displaced && !isNeutral(hex) && !onButton(hex);+ };+ const colors = { ...merged.colors, primary: neutral };+ if (incidental(colors.accent)) colors.accent = neutral;+ if (incidental(colors.link)) colors.link = colors.accent;+ if (incidental(colors.secondary)) delete colors.secondary;+ merged.colors = colors;+}diff --git a/apps/api/src/lib/branding/processor.ts b/apps/api/src/lib/branding/processor.tsindex b261b9ebf..09f8c7ff2 100644--- a/apps/api/src/lib/branding/processor.ts+++ b/apps/api/src/lib/branding/processor.ts@@ -186,17 +186,24 @@ function inferPalette(});// Text color from what text is written in: the most common text color of// non-button elements that stands out from the background. Voting over every- // sampled color let button fills outvote the paragraphs.- const textVotes = new Map<string, number>();- for (const s of snapshots) {- if (s.isButton || s.visible === false || !s.text || !s.text.trim()) {- continue;- }- const hex = hexify(s.colors.text, pageBackground);- if (hex && !isBrowserLinkColor(hex)) {- textVotes.set(hex, (textVotes.get(hex) || 0) + 1);+ // sampled color let button fills outvote the paragraphs, and links outvote+ // body text on link-heavy pages, so links vote only when nothing else does.+ const voteText = (links: boolean) => {+ const votes = new Map<string, number>();+ for (const s of snapshots) {+ if (s.isButton || s.visible === false || !s.text || !s.text.trim()) {+ continue;+ }+ if (!links && (s.isLink || s.tag === "a")) continue;+ const hex = hexify(s.colors.text, pageBackground);+ if (hex && !isBrowserLinkColor(hex)) {+ votes.set(hex, (votes.get(hex) || 0) + 1);+ }}- }+ return votes;+ };+ const bodyVotes = voteText(false);+ const textVotes = bodyVotes.size > 0 ? bodyVotes : voteText(true);const readableText = Array.from(textVotes.entries()).sort((x, y) => y[1] - x[1]).map(([h]) => h)