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

Function parse

src/utils/color.ts:164–222  ·  view source on GitHub ↗
($color: string)

Source from the content-addressed store, hash-verified

162];
163
164export function parse($color: string): RGBA | null {
165 const c = $color.trim().toLowerCase();
166 if (c.includes('(from ')) {
167 if (c.indexOf('(from') !== c.lastIndexOf('(from')) {
168 return null;
169 }
170 return domParseColor(c);
171 }
172
173 if (c.match(rgbMatch)) {
174 if (c.startsWith('rgb(#') || c.startsWith('rgba(#')) {
175 if (c.lastIndexOf('rgb') > 0) {
176 return null;
177 }
178 return domParseColor(c);
179 }
180 return parseRGB(c);
181 }
182
183 if (c.match(hslMatch)) {
184 return parseHSL(c);
185 }
186
187 if (c.match(hexMatch)) {
188 return parseHex(c);
189 }
190
191 if (knownColors.has(c)) {
192 return getColorByName(c);
193 }
194
195 if (systemColors.has(c)) {
196 return getSystemColor(c);
197 }
198
199 if (c === 'transparent') {
200 return {r: 0, g: 0, b: 0, a: 0};
201 }
202
203 if (
204 c.endsWith(')') &&
205 supportedColorFuncs.some(
206 (fn) => c.startsWith(fn) && c[fn.length] === '(' && c.lastIndexOf(fn) === 0
207 )
208 ) {
209 return domParseColor(c);
210 }
211
212 if (c.startsWith('light-dark(') && c.endsWith(')')) {
213 // light-dark([color()], [color()])
214 const match = c.match(/^light-dark\(\s*([a-z]+(\(.*\))?),\s*([a-z]+(\(.*\))?)\s*\)$/);
215 if (match) {
216 const schemeColor = isSystemDarkModeEnabled() ? match[3] : match[1];
217 return parse(schemeColor);
218 }
219 }
220
221 return null;

Callers 3

color.tests.tsFile · 0.90
parseColorWithCacheFunction · 0.70
getSitesFixesForFunction · 0.50

Calls 8

isSystemDarkModeEnabledFunction · 0.90
domParseColorFunction · 0.85
parseRGBFunction · 0.85
parseHSLFunction · 0.85
parseHexFunction · 0.85
getColorByNameFunction · 0.85
getSystemColorFunction · 0.85
hasMethod · 0.65

Tested by

no test coverage detected