MCPcopy
hub / github.com/darkreader/darkreader / getBgImageInfo

Function getBgImageInfo

tests/inject/dynamic/image-analysis.tests.ts:85–123  ·  view source on GitHub ↗
(bgImageValue: string)

Source from the content-addressed store, hash-verified

83}
84
85async function getBgImageInfo(bgImageValue: string) {
86 const bgImageURL = bgImageValue.match(/^url\("(.*)"\)$/)![1];
87 const image = await urlToImage(bgImageURL);
88
89 const width = 8;
90 const height = 8;
91 const canvas = document.createElement('canvas');
92 canvas.width = width;
93 canvas.height = height;
94 const context = canvas.getContext('2d')!;
95 context.drawImage(image, 0, 0, width, height);
96
97 const d = context.getImageData(0, 0, width, height).data;
98 let lightPixels = 0;
99 let darkPixels = 0;
100 let opaquePixels = 0;
101 for (let y = 0; y < height; y++) {
102 for (let x = 0; x < width; x++) {
103 const i = 4 * (x + width * y);
104 const r = d[i + 0];
105 const g = d[i + 1];
106 const b = d[i + 2];
107 const a = d[i + 3];
108 if (a > 127) {
109 opaquePixels++;
110 const lightness = (r + g + b) / 3 / 255;
111 if (lightness > 0.7) {
112 lightPixels++;
113 } else if (lightness < 0.3) {
114 darkPixels++;
115 }
116 }
117 }
118 }
119 return {
120 lightness: lightPixels / opaquePixels,
121 darkness: darkPixels / opaquePixels,
122 };
123}
124
125describe('IMAGE ANALYSIS', () => {
126 it('should analyze dark icon', async () => {

Callers 1

Calls 1

urlToImageFunction · 0.85

Tested by

no test coverage detected