Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const HexColor = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
- const RgbColor = /^rgb\( *(\d{1,3}), *(\d{1,3}), *(\d{1,3}),? *\)$/;
- function expandHexColor(color: string): string {
- if (color.length === 4 && color.charAt(0) === '#') {
- const c = color.split('');
- return `#${c[1]}${c[1]}${c[2]}${c[2]}${c[3]}${c[3]}`;
- }
- return color;
- }
- function rgbToHex(rgb: string): string {
- if (!RgbColor.test(rgb)) { return rgb; }
- const v = RgbColor.exec(rgb).map(val => parseInt(val).toString(16)).map(hex => hex.length === 1 ? '0' + hex : hex);
- return `#${v[1] + v[2] + v[3]}`;
- }
- export function ColorEqualityTester(first: string, second: string): boolean | undefined {
- if ((HexColor.test(first) || RgbColor.test(first)) && (HexColor.test(second) || RgbColor.test(second))) {
- return rgbToHex(expandHexColor(first)).toLowerCase() === rgbToHex(expandHexColor(second).toLowerCase());
- }
- return undefined;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement