compareColors()

This function is part of Iconify Utils package.

Function compareColors() compares two Color objects, returning true if colors are identical.

This function can be used to parse colors in SVG. When the same color is represented with different methods, this function can help find duplicate colors.

Usage

Function has two parameters:

  • color1, Color. First color.
  • color2, Color. Second color.

Function returns true if colors are identical, false if not.

Example

demo.ts
import { compareColors, stringToColor } from '@iconify/utils';

// Identical colors, function returns `true`
compareColors(stringToColor('rgb(0, 0, 0, 1)')!, stringToColor('#000000')!);

compareColors(
   stringToColor('rgb(0, 0, 0, 1)')!,
   stringToColor('hsl(100, 0%, 0%)')!
);

// All colors are transparent, function returns `true`
compareColors(
   stringToColor('rgb(0, 255, 0, 0)')!,
   stringToColor('hsl(100, 0%, 0%, 0)')!
);

compareColors(
   stringToColor('transparent')!,
   stringToColor('hsl(80, 20%, 50%, 0)')!
);

compareColors(stringToColor('transparent')!, stringToColor('#f8a0')!);

// Different colors, function returns `false`
compareColors(
   stringToColor('transparent')!,
   stringToColor('hsl(100, 0%, 0%, .1)')!
);