@@ -11,7 +11,7 @@ import { formatHunkHeader } from "../../core/hunkHeader";
1111import type { DiffFile } from "../../core/types" ;
1212import { blendHex , hexColorDistance } from "../lib/color" ;
1313import { sanitizeTerminalLine } from "../../lib/terminalText" ;
14- import type { AppTheme } from "../themes" ;
14+ import { TRANSPARENT_BACKGROUND , type AppTheme } from "../themes" ;
1515import { expandDiffTabs } from "./codeColumns" ;
1616
1717const PIERRE_THEME = {
@@ -238,6 +238,26 @@ function strengthenWordDiffBg(lineBg: string, signColor: string) {
238238 return strongestCandidate ;
239239}
240240
241+ /** Return whether a theme color can safely participate in RGB distance and blend math. */
242+ function isHexThemeColor ( color : string ) {
243+ return / ^ # [ 0 - 9 a - f ] { 6 } $ / i. test ( color ) ;
244+ }
245+
246+ /** Resolve one word-diff background without turning transparent surfaces into black blends. */
247+ function resolveWordDiffHighlightBg ( contentBg : string , lineBg : string , signColor : string ) {
248+ if ( contentBg === TRANSPARENT_BACKGROUND || lineBg === TRANSPARENT_BACKGROUND ) {
249+ return contentBg ;
250+ }
251+
252+ if ( ! isHexThemeColor ( contentBg ) || ! isHexThemeColor ( lineBg ) ) {
253+ return contentBg ;
254+ }
255+
256+ return hexColorDistance ( contentBg , lineBg ) >= MIN_WORD_DIFF_BG_DISTANCE
257+ ? contentBg
258+ : strengthenWordDiffBg ( lineBg , signColor ) ;
259+ }
260+
241261/** Resolve the inline word-diff background, strengthening theme colors that are too subtle to see. */
242262function wordDiffHighlightBg ( kind : SplitLineCell [ "kind" ] , theme : AppTheme ) {
243263 const cacheKey = [
@@ -251,14 +271,16 @@ function wordDiffHighlightBg(kind: SplitLineCell["kind"], theme: AppTheme) {
251271 ] . join ( ":" ) ;
252272 let cached = wordDiffBackgroundCache . get ( cacheKey ) ;
253273 if ( ! cached ) {
254- const addition =
255- hexColorDistance ( theme . addedContentBg , theme . addedBg ) >= MIN_WORD_DIFF_BG_DISTANCE
256- ? theme . addedContentBg
257- : strengthenWordDiffBg ( theme . addedBg , theme . addedSignColor ) ;
258- const deletion =
259- hexColorDistance ( theme . removedContentBg , theme . removedBg ) >= MIN_WORD_DIFF_BG_DISTANCE
260- ? theme . removedContentBg
261- : strengthenWordDiffBg ( theme . removedBg , theme . removedSignColor ) ;
274+ const addition = resolveWordDiffHighlightBg (
275+ theme . addedContentBg ,
276+ theme . addedBg ,
277+ theme . addedSignColor ,
278+ ) ;
279+ const deletion = resolveWordDiffHighlightBg (
280+ theme . removedContentBg ,
281+ theme . removedBg ,
282+ theme . removedSignColor ,
283+ ) ;
262284
263285 cached = {
264286 addition,
0 commit comments