MCPcopy Index your code
hub / github.com/darkreader/darkreader / modifyColorWithCache

Function modifyColorWithCache

src/inject/dynamic-theme/modify-colors.ts:59–86  ·  view source on GitHub ↗
(rgb: RGBA, theme: Theme, modifyHSL: HSLModifyFunction, poleColor?: string, anotherPoleColor?: string)

Source from the content-addressed store, hash-verified

57}
58
59function modifyColorWithCache(rgb: RGBA, theme: Theme, modifyHSL: HSLModifyFunction, poleColor?: string, anotherPoleColor?: string): string {
60 let fnCache: Map<string, string>;
61 if (colorModificationCache.has(modifyHSL)) {
62 fnCache = colorModificationCache.get(modifyHSL)!;
63 } else {
64 fnCache = new Map();
65 colorModificationCache.set(modifyHSL, fnCache);
66 }
67 const id = getCacheId(rgb, theme, poleColor, anotherPoleColor);
68 if (fnCache.has(id)) {
69 return fnCache.get(id)!;
70 }
71
72 const hsl = rgbToHSL(rgb);
73 const pole = poleColor == null ? null : parseToHSLWithCache(poleColor);
74 const anotherPole = anotherPoleColor == null ? null : parseToHSLWithCache(anotherPoleColor);
75 const modified = modifyHSL(hsl, pole!, anotherPole!);
76 const {r, g, b, a} = hslToRGB(modified);
77 const matrix = createFilterMatrix({...theme, mode: 0});
78 const [rf, gf, bf] = applyColorMatrix([r, g, b], matrix);
79
80 const color = (a === 1 ?
81 rgbToHexString({r: rf, g: gf, b: bf}) :
82 rgbToString({r: rf, g: gf, b: bf, a}));
83
84 fnCache.set(id, color);
85 return color;
86}
87
88function noopHSL(hsl: HSLA): HSLA {
89 return hsl;

Callers 5

modifyColorFunction · 0.85
modifyLightSchemeColorFunction · 0.85
_modifyBackgroundColorFunction · 0.85
_modifyForegroundColorFunction · 0.85
_modifyBorderColorFunction · 0.85

Calls 11

rgbToHSLFunction · 0.90
parseToHSLWithCacheFunction · 0.90
hslToRGBFunction · 0.90
createFilterMatrixFunction · 0.90
applyColorMatrixFunction · 0.90
rgbToHexStringFunction · 0.90
rgbToStringFunction · 0.90
getCacheIdFunction · 0.85
hasMethod · 0.65
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected