Skip to content

end to end app store screenshot creation using AI

fix: keep caption text readable on custom slide backgrounds

Parth Jadhav87712d591a2e
diff --git a/skills/app-store-screenshots/template/src/components/editor/background-controls.tsx b/skills/app-store-screenshots/template/src/components/editor/background-controls.tsx
index 2671eec..dd7abd5 100644
--- a/skills/app-store-screenshots/template/src/components/editor/background-controls.tsx
+++ b/skills/app-store-screenshots/template/src/components/editor/background-controls.tsx
@@ -68,6 +68,11 @@ export function BackgroundControls({ slide, theme, onChange }: Props) {
/>
</div>
)}
+ {mode === "custom" && (
+ <p className="text-[11px] text-muted-foreground">
+ Caption text switches to a readable colour when the theme&apos;s colours would be too faint.
+ </p>
+ )}
</div>
);
}
diff --git a/skills/app-store-screenshots/template/src/components/editor/slide-canvas.tsx b/skills/app-store-screenshots/template/src/components/editor/slide-canvas.tsx
index 52c2b45..a2ff4c9 100644
--- a/skills/app-store-screenshots/template/src/components/editor/slide-canvas.tsx
+++ b/skills/app-store-screenshots/template/src/components/editor/slide-canvas.tsx
@@ -35,6 +35,7 @@ import {
import { imageElementKey, isImageElementId, toImageElementId, toTextElementId } from "@/lib/elements";
import { img } from "@/lib/image-cache";
import { pickText, resolveScreenshot } from "@/lib/locale";
+import { slideColors } from "@/lib/contrast";
import { defaultTextElementFontSize, slideFontScales } from "@/lib/typography";
import {
AndroidPhone,
@@ -262,8 +263,7 @@ function Caption({
inverted?: boolean;
onFocus?: () => void;
}) {
- const fg = inverted ? theme.fgAlt : theme.fg;
- const accent = inverted ? theme.accentAlt ?? theme.accent : theme.accent;
+ const { fg, accent } = slideColors(theme, { inverted, backgroundColor: slide.backgroundColor });
const { labelScale, headlineScale } = slideFontScales(slide);
// Scale typography off the *shorter* dimension so landscape layouts don't
// produce headlines so tall they overlap the device frame.
@@ -793,7 +793,7 @@ function SlideBackground({
inset: 0,
overflow: "hidden",
background: backgroundFor(theme, inverted, slide.backgroundColor),
- color: inverted ? theme.fgAlt : theme.fg,
+ color: slideColors(theme, slide).fg,
}}
>
<Blob cW={cW} color={theme.accent} x={-15} y={-10} size={55} opacity={inverted ? 0.25 : 0.32} />
@@ -973,6 +973,7 @@ function SlideElements({
const screenshotSecondary = resolveScreenshot(slide.screenshotSecondary, locale);
const { cW, cH, Frame, frameAspect, defaults } = getSlideGeometry(slide, device, orientation);
const inverted = !!slide.inverted;
+ const colors = slideColors(theme, slide);
const captionRect = rectFor("caption", slide, defaults);
const deviceRect = rectFor("device", slide, defaults);
const secondaryRect = rectFor("deviceSecondary", slide, defaults);
@@ -1076,7 +1077,7 @@ function SlideElements({
const rect = textElement.transform;
const rotation = rect.rotation ?? 0;
const zIndex = rect.zIndex ?? 5 + index;
- const textColor = textElement.color || (inverted ? theme.fgAlt : theme.fg);
+ const textColor = textElement.color || colors.fg;
return (
<Movable
key={textElement.id}
@@ -1130,7 +1131,7 @@ function SlideElements({
fontWeight: textElement.fontWeight ?? 700,
lineHeight: 1.05,
textAlign: textElement.align ?? "center",
- textShadow: inverted ? "0 2px 18px rgba(0,0,0,0.22)" : "0 2px 18px rgba(255,255,255,0.2)",
+ textShadow: colors.dark ? "0 2px 18px rgba(0,0,0,0.22)" : "0 2px 18px rgba(255,255,255,0.2)",
}}
/>
</div>
diff --git a/skills/app-store-screenshots/template/src/lib/contrast.ts b/skills/app-store-screenshots/template/src/lib/contrast.ts
new file mode 100644
index 0000000..d0252b0
--- /dev/null
+++ b/skills/app-store-screenshots/template/src/lib/contrast.ts
@@ -0,0 +1,74 @@
+import type { Slide, Theme } from "./types";
+
+// WCAG 2 contrast helpers used to keep caption text readable when a slide
+// uses a custom background colour instead of one of its theme's backgrounds.
+
+const DARK_TEXT = "#0B0B0F";
+const LIGHT_TEXT = "#FFFFFF";
+// Headlines are large, bold display text, but labels are small; aim for the
+// body-text ratio so both stay legible after App Store downscaling.
+const TEXT_CONTRAST = 4.5;
+// The accent label may be a little softer before we swap it for the text colour.
+const ACCENT_CONTRAST = 3;
+
+function parseHex(hex: string): [number, number, number] | null {
+ const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex.trim());
+ if (!m) return null;
+ const h = m[1].length === 3 ? m[1].split("").map((c) => c + c).join("") : m[1];
+ const n = parseInt(h, 16);
+ return [(n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];
+}
+
+export function relativeLuminance(hex: string): number | null {
+ const rgb = parseHex(hex);
+ if (!rgb) return null;
+ const [r, g, b] = rgb.map((v) => {
+ const c = v / 255;
+ return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
+ });
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
+}
+
+export function contrastRatio(a: string, b: string): number {
+ const la = relativeLuminance(a);
+ const lb = relativeLuminance(b);
+ if (la === null || lb === null) return 21;
+ const [hi, lo] = la > lb ? [la, lb] : [lb, la];
+ return (hi + 0.05) / (lo + 0.05);
+}
+
+function firstReadable(bg: string, candidates: (string | undefined)[], min: number) {
+ return candidates.find((c): c is string => !!c && contrastRatio(c, bg) >= min);
+}
+
+export type SlideColors = {
+ /** Headline and default text-element colour. */
+ fg: string;
+ /** Uppercase label colour. */
+ accent: string;
+ /** True when the slide background is dark (drives subtle text shadows). */
+ dark: boolean;
+};
+
+/**
+ * Text colours for a slide. Theme backgrounds use the theme's own colours.
+ * A custom background keeps the theme colours when they're readable on it and
+ * otherwise switches to the theme's other text colour, then to near-black or
+ * white, whichever contrasts more. Colours a user picked explicitly on a text
+ * element are never passed through here.
+ */
+export function slideColors(theme: Theme, slide: Pick<Slide, "inverted" | "backgroundColor">): SlideColors {
+ const inverted = !!slide.inverted;
+ const themeFg = inverted ? theme.fgAlt : theme.fg;
+ const themeAccent = inverted ? theme.accentAlt ?? theme.accent : theme.accent;
+ const bg = slide.backgroundColor;
+ if (!bg || relativeLuminance(bg) === null) {
+ return { fg: themeFg, accent: themeAccent, dark: inverted };
+ }
+ const dark = (relativeLuminance(bg) ?? 1) < 0.18;
+ const fallback = contrastRatio(DARK_TEXT, bg) >= contrastRatio(LIGHT_TEXT, bg) ? DARK_TEXT : LIGHT_TEXT;
+ const fg = firstReadable(bg, [themeFg, inverted ? theme.fg : theme.fgAlt], TEXT_CONTRAST) ?? fallback;
+ const accent =
+ firstReadable(bg, [themeAccent, inverted ? theme.accent : theme.accentAlt], ACCENT_CONTRAST) ?? fg;
+ return { fg, accent, dark };
+}