/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
@use 'sass:meta';
@use 'sass:math';
@use 'sass:color';

@function _linear-channel-value($channel-value) {
  $normalized-channel-value: math.div($channel-value, 255);
  @if $normalized-channel-value < 0.03928 {
    @return math.div($normalized-channel-value, 12.92);
  }

  @return math.pow(math.div($normalized-channel-value + 0.055, 1.055), 2.4);
}

// Calculate the luminance for a color.
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
@function luminance($color) {
  $red: _linear-channel-value(color.red($color));
  $green: _linear-channel-value(color.green($color));
  $blue: _linear-channel-value(color.blue($color));

  @return 0.2126 * $red + 0.7152 * $green + 0.0722 * $blue;
}

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

// Determine whether the color is 'light' or 'dark'.
@function tone($color) {
  @if $color == 'dark' or $color == 'light' {
    @return $color;
  }

  @if meta.type-of($color) != 'color' {
    @warn '#{$color} is not a color. Falling back to "dark" tone.';
    @return 'dark';
  }

  $minimumContrast: 3.1;
  $lightContrast: contrast-ratio($color, white);
  $darkContrast: contrast-ratio($color, rgba(black 0.87));

  @if $lightContrast < $minimumContrast and $darkContrast > $lightContrast {
    @return 'light';
  } @else {
    @return 'dark';
  }
}

// Determine whether to use dark or light text on top of given color to meet accessibility standards for contrast.
// Returns 'dark' if the given color is light and 'light' if the given color is dark.
@function contrast-tone($color) {
  @return if(tone($color) == 'dark', 'light', 'dark');
}
