| 388 | const C_f = 'f'.charCodeAt(0); |
| 389 | |
| 390 | function parseHex($hex: string): RGBA | null { |
| 391 | const length = $hex.length; |
| 392 | const digitCount = length - 1; |
| 393 | const isShort = digitCount === 3 || digitCount === 4; |
| 394 | const isLong = digitCount === 6 || digitCount === 8; |
| 395 | if (!isShort && !isLong) { |
| 396 | return null; |
| 397 | } |
| 398 | |
| 399 | const hex = (i: number) => { |
| 400 | const c = $hex.charCodeAt(i); |
| 401 | if (c >= C_A && c <= C_F) { |
| 402 | return c + 10 - C_A; |
| 403 | } |
| 404 | if (c >= C_a && c <= C_f) { |
| 405 | return c + 10 - C_a; |
| 406 | } |
| 407 | return c - C_0; |
| 408 | }; |
| 409 | |
| 410 | let r: number; |
| 411 | let g: number; |
| 412 | let b: number; |
| 413 | let a = 1; |
| 414 | if (isShort) { |
| 415 | r = hex(1) * 17; |
| 416 | g = hex(2) * 17; |
| 417 | b = hex(3) * 17; |
| 418 | if (digitCount === 4) { |
| 419 | a = hex(4) * 17 / 255; |
| 420 | } |
| 421 | } else { |
| 422 | r = hex(1) * 16 + hex(2); |
| 423 | g = hex(3) * 16 + hex(4); |
| 424 | b = hex(5) * 16 + hex(6); |
| 425 | if (digitCount === 8) { |
| 426 | a = (hex(7) * 16 + hex(8)) / 255; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return {r, g, b, a}; |
| 431 | } |
| 432 | |
| 433 | function getColorByName($color: string): RGBA { |
| 434 | const n = knownColors.get($color)!; |