@import 'immaterial/helpers/color-constants';

// Calculate the luminance for a color.
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
@function calculate-luminance($color) {
  $red: nth($linear-channel-values, red($color) + 1);
  $green: nth($linear-channel-values, green($color) + 1);
  $blue: nth($linear-channel-values, blue($color) + 1);

  @return .2126 * $red + .7152 * $green + .0722 * $blue;
}

// Calculate the contrast ratio between two colors.
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
@function calculate-contrast($back, $front) {
  $backLum: calculate-luminance($back) + .05;
  $foreLum: calculate-luminance($front) + .05;

  @return max($backLum, $foreLum) / min($backLum, $foreLum);
}

// Determine whether to use reverse text on top of given color.
// Returns "no" for dark text and "yes" for light text.
@function is-reverse-text($color) {
  $minimumContrast: 3.1;

  $lightContrast: calculate-contrast($color, white);
  $darkContrast: calculate-contrast($color, rgba(black, .87));

  @if ($lightContrast < $minimumContrast) and ($darkContrast > $lightContrast) {
    @return 'no';
  } @else {
    @return 'yes';
  }
}
