Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(docs): preserve integration brand colors (#4076)

Colton Padden181c3852ee34
diff --git a/apps/docs/app/[lang]/integrations/[slug]/page.tsx b/apps/docs/app/[lang]/integrations/[slug]/page.tsx
index f74a65eb2..137e931c9 100644
--- a/apps/docs/app/[lang]/integrations/[slug]/page.tsx
+++ b/apps/docs/app/[lang]/integrations/[slug]/page.tsx
@@ -98,7 +98,7 @@ const IntegrationDetailPage = async ({ params }: PageProps<"/[lang]/integrations
</Link>
<header className="mt-8 flex flex-col gap-5 border-b pb-10">
- <span className="flex size-14 items-center justify-center rounded-xl border bg-background text-gray-1000">
+ <span className="flex size-14 items-center justify-center rounded-xl border bg-background">
<Logo aria-hidden className="size-7" height={28} width={28} />
</span>
<div className="flex flex-col gap-2">
diff --git a/apps/docs/app/[lang]/integrations/components/integration-card.tsx b/apps/docs/app/[lang]/integrations/components/integration-card.tsx
index b2eb475dd..387dd501b 100644
--- a/apps/docs/app/[lang]/integrations/components/integration-card.tsx
+++ b/apps/docs/app/[lang]/integrations/components/integration-card.tsx
@@ -27,7 +27,7 @@ export const IntegrationCard = ({ integration, onSelect }: IntegrationCardProps)
prefetch={true}
>
<div className="flex flex-wrap items-start justify-between gap-3">
- <span className="flex size-10 shrink-0 items-center justify-center rounded-md border bg-background text-gray-1000">
+ <span className="flex size-10 shrink-0 items-center justify-center rounded-md border bg-background">
<Logo aria-hidden className="size-5" height={20} width={20} />
</span>
<div className="flex min-w-0 flex-wrap items-center justify-end gap-1.5">
diff --git a/apps/docs/lib/integrations/og-image.test.ts b/apps/docs/lib/integrations/og-image.test.ts
new file mode 100644
index 000000000..9c55c5a19
--- /dev/null
+++ b/apps/docs/lib/integrations/og-image.test.ts
@@ -0,0 +1,53 @@
+import { PNG } from "pngjs";
+import { describe, expect, it } from "vitest";
+import { getIntegration } from "./data";
+import { createIntegrationOgImage } from "./og-image";
+
+const renderIntegrationOgImage = async (slug: string): Promise<PNG> => {
+ const image = await createIntegrationOgImage(getIntegration(slug)!);
+ return PNG.sync.read(Buffer.from(await image.arrayBuffer()));
+};
+
+const countPixels = (
+ image: PNG,
+ matches: (red: number, green: number, blue: number) => boolean,
+) => {
+ let count = 0;
+ for (let offset = 0; offset < image.data.length; offset += 4) {
+ if (
+ image.data[offset + 3]! > 0 &&
+ matches(image.data[offset]!, image.data[offset + 1]!, image.data[offset + 2]!)
+ ) {
+ count += 1;
+ }
+ }
+ return count;
+};
+
+describe("createIntegrationOgImage", () => {
+ it("preserves multicolor integration logos", async () => {
+ const image = await renderIntegrationOgImage("slack");
+ const saturatedPixels = countPixels(
+ image,
+ (red, green, blue) => Math.max(red, green, blue) - Math.min(red, green, blue) > 50,
+ );
+
+ expect(saturatedPixels).toBeGreaterThan(100);
+ });
+
+ it("lifts dark logo colors enough to read against the black background", async () => {
+ const image = await renderIntegrationOgImage("arcana");
+ const liftedLogoPixels = countPixels(
+ image,
+ (red, green, blue) =>
+ red >= 150 && red <= 210 && green >= 150 && green <= 210 && blue >= 150 && blue <= 210,
+ );
+ const blackBackgroundPixels = countPixels(
+ image,
+ (red, green, blue) => red === 0 && green === 0 && blue === 0,
+ );
+
+ expect(liftedLogoPixels).toBeGreaterThan(100);
+ expect(blackBackgroundPixels).toBeGreaterThan(100_000);
+ });
+});
diff --git a/apps/docs/lib/integrations/og-image.tsx b/apps/docs/lib/integrations/og-image.tsx
index 6c3679472..f798e0463 100644
--- a/apps/docs/lib/integrations/og-image.tsx
+++ b/apps/docs/lib/integrations/og-image.tsx
@@ -64,6 +64,7 @@ const fitLogo = (node: ReactNode, maxWidth: number, maxHeight: number): ReactNod
};
interface RasterizedLogo {
+ darkSilhouette: boolean;
height: number;
inkCoverage: number;
src: string;
@@ -111,6 +112,77 @@ const cropLogo = (image: PNG): PNG => {
return cropped;
};
+const contrastLogo = (node: ReactNode): ReactNode => {
+ if (!isValidElement(node)) return node;
+
+ const element = node as ReactElement<LogoElementProps>;
+ if (element.type === "mask") return element;
+
+ const liftDarkColor = (color: string | undefined): string | undefined => {
+ if (!color || color === "none" || color === "currentColor") return color;
+ if (color === "black") return "#b4b4b4";
+
+ const hex = color.startsWith("#") ? color.slice(1) : "";
+ const expanded = hex.length === 3 ? [...hex].map((digit) => digit.repeat(2)).join("") : hex;
+ if (!/^[\da-f]{6}$/iu.test(expanded)) return color;
+
+ const channels = [0, 2, 4].map((offset) =>
+ Number.parseInt(expanded.slice(offset, offset + 2), 16),
+ );
+ const [red, green, blue] = channels as [number, number, number];
+ const luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
+ if (luminance >= 110) return color;
+
+ const lift = (180 - luminance) / (255 - luminance);
+ return `#${channels
+ .map((channel) =>
+ Math.round(channel + (255 - channel) * lift)
+ .toString(16)
+ .padStart(2, "0"),
+ )
+ .join("")}`;
+ };
+
+ const props = element.props;
+ return cloneElement(
+ element,
+ { ...props, fill: liftDarkColor(props.fill), stroke: liftDarkColor(props.stroke) },
+ Children.map(props.children, contrastLogo),
+ );
+};
+
+// A logo whose outer edge is dark disappears on the black OG canvas. Logos with
+// dark details inside their own colored shape are fine as drawn.
+const hasDarkSilhouette = (image: PNG): boolean => {
+ const alphaAt = (x: number, y: number): number =>
+ x < 0 || y < 0 || x >= image.width || y >= image.height
+ ? 0
+ : image.data[(y * image.width + x) * 4 + 3]!;
+ let edge = 0;
+ let darkEdge = 0;
+ for (let y = 0; y < image.height; y += 1) {
+ for (let x = 0; x < image.width; x += 1) {
+ if (alphaAt(x, y) < 200) continue;
+ if (
+ alphaAt(x - 1, y) > 50 &&
+ alphaAt(x + 1, y) > 50 &&
+ alphaAt(x, y - 1) > 50 &&
+ alphaAt(x, y + 1) > 50
+ ) {
+ continue;
+ }
+ edge += 1;
+ const offset = (y * image.width + x) * 4;
+ const luminance =
+ 0.2126 * image.data[offset]! +
+ 0.7152 * image.data[offset + 1]! +
+ 0.0722 * image.data[offset + 2]!;
+ if (luminance < 60) darkEdge += 1;
+ }
+ }
+ return edge > 0 && darkEdge / edge > 0.5;
+};
+
const rasterizeLogo = async (logo: ReactNode): Promise<RasterizedLogo> => {
const response = new ImageResponse(
<div
@@ -128,6 +200,7 @@ const rasterizeLogo = async (logo: ReactNode): Promise<RasterizedLogo> => {
{ height: 512, width: 512 },
);
const image = PNG.sync.read(Buffer.from(await response.arrayBuffer()));
+ const darkSilhouette = hasDarkSilhouette(image);
const cropped = cropLogo(image);
let ink = 0;
for (let offset = 0; offset < cropped.data.length; offset += 4) {
@@ -135,6 +208,7 @@ const rasterizeLogo = async (logo: ReactNode): Promise<RasterizedLogo> => {
}
return {
+ darkSilhouette,
height: cropped.height,
inkCoverage: ink / (cropped.width * cropped.height),
src: `data:image/png;base64,${PNG.sync.write(cropped).toString("base64")}`,
@@ -142,56 +216,6 @@ const rasterizeLogo = async (logo: ReactNode): Promise<RasterizedLogo> => {
};
};
-const collectLogoColors = (node: ReactNode, colors = new Set<string>()): Set<string> => {
- if (!isValidElement(node)) return colors;
-
- const element = node as ReactElement<LogoElementProps>;
- if (element.type === "mask") return colors;
-
- const { children, fill, stroke } = element.props;
- for (const color of [fill, stroke]) {
- if (color && color !== "none" && color !== "currentColor") colors.add(color.toLowerCase());
- }
- Children.forEach(children, (child) => collectLogoColors(child, colors));
- return colors;
-};
-
-const grayscaleColor = (color: string): string => {
- const normalized = color.toLowerCase();
- if (normalized === "currentcolor") return "white";
- if (normalized === "white") return "black";
- if (normalized === "black") return "white";
-
- const hex = normalized.slice(1);
- const expanded = hex.length === 3 ? [...hex].map((digit) => digit.repeat(2)).join("") : hex;
- if (!/^[\da-f]{6}$/.test(expanded)) return "white";
-
- const red = Number.parseInt(expanded.slice(0, 2), 16);
- const green = Number.parseInt(expanded.slice(2, 4), 16);
- const blue = Number.parseInt(expanded.slice(4, 6), 16);
- const gray = 255 - Math.round(0.2126 * red + 0.7152 * green + 0.0722 * blue);
- return `rgb(${gray}, ${gray}, ${gray})`;
-};
-
-const recolorLogo = (node: ReactNode, preserveTones: boolean): ReactNode => {
- if (!isValidElement(node)) return node;
-
- const element = node as ReactElement<LogoElementProps>;
- if (element.type === "mask") return element;
-
- const props = element.props;
- const recolor = (color: string | undefined): string | undefined => {
- if (!color || color === "none") return color;
- return preserveTones ? grayscaleColor(color) : "white";
- };
-
- return cloneElement(
- element,
- { ...props, fill: recolor(props.fill), stroke: recolor(props.stroke) },
- Children.map(props.children, (child) => recolorLogo(child, preserveTones)),
- );
-};
-
const balanceLogoWeight = (logo: RasterizedLogo, referenceCoverage: number): SizedLogo => {
const aspectRatio = logo.width / logo.height;
const fittedWidth = Math.min(180, 132 * aspectRatio);
@@ -225,8 +249,9 @@ const getRasterizedLogo = (integration: Integration): Promise<RasterizedLogo> =>
const Logo = logos[integration.logo];
const resolvedLogo = resolveLogo(<Logo aria-hidden />);
- const recoloredLogo = recolorLogo(resolvedLogo, collectLogoColors(resolvedLogo).size > 1);
- const rasterized = rasterizeLogo(recoloredLogo);
+ const rasterized = rasterizeLogo(resolvedLogo).then((logo) =>
+ logo.darkSilhouette ? rasterizeLogo(contrastLogo(resolvedLogo)) : logo,
+ );
rasterizedLogos.set(integration.logo, rasterized);
return rasterized;
};