@devries-cameron20 / t3code Mirror · read-only / commits / 75d8b132cd
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 02:45 UTC

feat(web): tint image chips with their average color (#11468)

Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

maria committed Sep 12, 2026 at 19:11 UTC 75d8b132cd0718d8cd703c97d02b143891d80022
1 file changed +51 -2
apps/web/src/components/contextChipParts.tsx
+51 -2
@@ -1,6 +1,12 @@
1 import type { PullRequestContextMetadata } from "@t3tools/contracts";
2 import { CircleDashedIcon, FilmIcon, GitPullRequestIcon, ImageIcon } from "lucide-react";
3 -import type { ComponentProps, MouseEvent, ReactNode } from "react";
3 +import {
4 + useState,
5 + type ComponentProps,
6 + type CSSProperties,
7 + type MouseEvent,
8 + type ReactNode,
9 +} from "react";
10
11 import { cn } from "~/lib/utils";
12 import { PierreEntryIcon } from "./chat/PierreEntryIcon";
@@ -139,6 +145,34 @@ export function PullRequestChip(props: {
145 );
146 }
147
148 +/** Sample the loaded thumbnail once; transparent pixels should not darken its accent. */
149 +function averageImageColor(image: HTMLImageElement): string | undefined {
150 + try {
151 + const canvas = document.createElement("canvas");
152 + canvas.width = canvas.height = 16;
153 + const context = canvas.getContext("2d");
154 + if (!context) return;
155 + context.drawImage(image, 0, 0, 16, 16);
156 + const { data } = context.getImageData(0, 0, 16, 16);
157 + let red = 0;
158 + let green = 0;
159 + let blue = 0;
160 + let alpha = 0;
161 + for (let index = 0; index < data.length; index += 4) {
162 + const weight = data[index + 3]!;
163 + red += data[index]! * weight;
164 + green += data[index + 1]! * weight;
165 + blue += data[index + 2]! * weight;
166 + alpha += weight;
167 + }
168 + if (alpha === 0) return;
169 + return `rgb(${Math.round(red / alpha)} ${Math.round(green / alpha)} ${Math.round(blue / alpha)})`;
170 + } catch {
171 + // Cross-origin or unavailable pixels keep the default image tone and preview action.
172 + return;
173 + }
174 +}
175 +
176 export function ImageChipButton({
177 name,
178 previewUrl,
@@ -146,6 +180,7 @@ export function ImageChipButton({
180 labelClassName,
181 size,
182 suffix,
183 + style,
184 ...props
185 }: ComponentProps<"button"> & {
186 name: string;
@@ -155,6 +190,9 @@ export function ImageChipButton({
190 size: string;
191 suffix?: string | null;
192 }) {
193 + const [sample, setSample] = useState<{ url: string; color: string | undefined }>();
194 + const [corsFailedUrl, setCorsFailedUrl] = useState<string>();
195 + const accent = sample?.url === previewUrl ? sample?.color : undefined;
196 return (
197 <Button
198 variant="chip"
@@ -165,10 +203,21 @@ export function ImageChipButton({
203 "cursor-zoom-in",
204 )}
205 aria-label={`Image attachment, ${name}, ${size}`}
206 + style={{ ...style, ...(accent ? { "--context-chip-accent": accent } : {}) } as CSSProperties}
207 {...props}
208 >
209 {previewUrl ? (
171 - <img src={previewUrl} alt="" className="size-3.5 shrink-0 rounded-sm object-cover" />
210 + <img
211 + key={previewUrl}
212 + crossOrigin={corsFailedUrl === previewUrl ? undefined : "anonymous"}
213 + src={previewUrl}
214 + alt=""
215 + className="size-3.5 shrink-0 rounded-sm object-cover"
216 + onError={() => setCorsFailedUrl(previewUrl)}
217 + onLoad={(event) =>
218 + setSample({ url: previewUrl, color: averageImageColor(event.currentTarget) })
219 + }
220 + />
221 ) : (
222 <ImageIcon
223 className={cn(