@use "sass:math" as math;

/*
    Adopted with gratitude from w3.org:
    https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
*/
@function get-relative-luminance($color) {
    $color: math.div($color, 255);

    @if ($color < 0.03928) {
        @return math.div($color, 12.92)
    }

    @return math.pow(math.div($color + 0.055, 1.055), 2.4);
}

@function get-luminance($color) {
    $red: get-relative-luminance(red($color));
    $green: get-relative-luminance(green($color));
    $blue: get-relative-luminance(blue($color));

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

@function get-contrast-ratio($backgroundColor, $foregroundColor) {
    $backgroundLuminance: get-luminance($backgroundColor) + .05;
    $foregroundLuminance: get-luminance($foregroundColor) + .05;

    @return math.div(max($backgroundLuminance, $foregroundLuminance), min($backgroundLuminance, $foregroundLuminance));
}

@function get-contrast-color($color) {
    $lightContrast: get-contrast-ratio($color, white);
    $darkContrast: get-contrast-ratio($color, black);

    @if ($lightContrast > 4.5 or $darkContrast < 4.5) {
        @return white;
    }

    @return black;
}
