Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. const HexColor = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
  2. const RgbColor = /^rgb\( *(\d{1,3}), *(\d{1,3}), *(\d{1,3}),? *\)$/;
  3.  
  4. function expandHexColor(color: string): string {
  5. if (color.length === 4 && color.charAt(0) === '#') {
  6. const c = color.split('');
  7. return `#${c[1]}${c[1]}${c[2]}${c[2]}${c[3]}${c[3]}`;
  8. }
  9. return color;
  10. }
  11.  
  12. function rgbToHex(rgb: string): string {
  13. if (!RgbColor.test(rgb)) { return rgb; }
  14. const v = RgbColor.exec(rgb).map(val => parseInt(val).toString(16)).map(hex => hex.length === 1 ? '0' + hex : hex);
  15. return `#${v[1] + v[2] + v[3]}`;
  16. }
  17.  
  18. export function ColorEqualityTester(first: string, second: string): boolean | undefined {
  19. if ((HexColor.test(first) || RgbColor.test(first)) && (HexColor.test(second) || RgbColor.test(second))) {
  20. return rgbToHex(expandHexColor(first)).toLowerCase() === rgbToHex(expandHexColor(second).toLowerCase());
  21. }
  22. return undefined;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement