goldies
    Preparing search index...
    • Calculates the relative luminance of a color according to WCAG 2.0 guidelines.

      Relative luminance is the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white. This value is commonly used for calculating color contrast ratios for accessibility.

      Parameters

      • hexCode: string

        A 3 or 6 character hexadecimal color code. The leading # is optional.

      Returns number

      The relative luminance value as a number between 0 (black) and 1 (white).

      If hexCode isn't a 3- or 6-digit hex color.

      // Pure red
      calculateRelativeLuminance('#ff0000');
      // => 0.2126
      // Pure black
      calculateRelativeLuminance('#000000');
      // => 0
      // Pure white
      calculateRelativeLuminance('#ffffff');
      // => 1
      // Using 3-digit shorthand
      calculateRelativeLuminance('000');
      // => 0