@use "sass:math";

@function toRGB($color) {
    @return "rgb(" + red($color) + ", " + green($color) + ", " + blue($color) +
        ")";
}
@function getContrast($color) {
    $color-brightness: round(
        (red($color) * 299) + (green($color) * 587) +
            math.div((blue($color) * 114), 1000)
    );
    $light-color: round(
        (red(#fff) * 299) + (green(#fff) * 587) +
            math.div((blue(#fff) * 114), 1000)
    );
    @if abs($color-brightness) < math.div($light-color, 1.7) {
        @return white;
    } @else {
        @return getColorVariant(map-get($eui-base-colors, "neutral"), 120);
    }
}

@function col_r($color) {
    @if $color <= 0.03928 {
        @return $color / 12.92;
    } @else {
        @return pow((($color + 0.055) / 1.055), (2.4));
    }
}

@function getContrastNew($bgColor, $lightColor: #fff, $darkColor: #404040) {
    $r: red($bgColor);
    $g: green($bgColor);
    $b: blue($bgColor);
    $ui_r: $r / 255;
    $ui_g: $g / 255;
    $ui_b: $b / 255;

    $ui_r_c: col_r($ui_r);
    $ui_g_c: col_r($ui_g);
    $ui_b_c: col_r($ui_b);

    $L: (0.2126 * $ui_r_c) + (0.7152 * $ui_g_c) + (0.0722 * $ui_b_c);
    @if ($L > 0.179) {
        @return $darkColor;
    } @else {
        @return $lightColor;
    }
}


// method 1 check here : very accurate contrast compared to chrome extension contrast checker tool
// https://gist.github.com/isGabe/8959422

@function color_luminance($color) {
    // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
    // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
    $rgba: red($color), green($color), blue($color);
    $rgba2: ();

    @for $i from 1 through 3 {
      $rgb: nth($rgba, $i);
      $rgb: $rgb / 255;

      $rgb: if($rgb < .03928, $rgb / 12.92, math.pow(($rgb + .055) / 1.055, 2.4));

      $rgba2: append($rgba2, $rgb);
    }

    @return .2126 * nth($rgba2, 1) + .7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3);
  }

  @function calculate-contrast($color1, $color2) {
    // Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
    // Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
    $luminance1: color_luminance($color1) + .05;
    $luminance2: color_luminance($color2) + .05;
    $ratio: $luminance1 / $luminance2;

    @if $luminance2 > $luminance1 {
      $ratio: 1 / $ratio;
    }

    $ratio: round($ratio * 10) / 10;

    @return $ratio;
  }


// https://www.jasongaylord.com/blog/2023/05/30/calculate-contrast-in-sass

// NOT used as not accurate compared to chrome extension tool

// @function sRGBToLinear($colorValue) {
//     $colorValue: calc($colorValue / 255);
//     @if $colorValue <= 0.04045 {
//         @return calc($colorValue / 12.92);
//     } @else {
//         @return calc(math.pow($colorValue + 0.055, 2.4));
//     }
// }

// @function calculate-contrast($value1, $value2) {
//     $luminance1: (0.2126 * sRGBToLinear(red($value1))) +
//         (0.7152 * sRGBToLinear(green($value1))) +
//         (0.0722 * sRGBToLinear(blue($value1)));
//     $luminance2: (0.2126 * sRGBToLinear(red($value2))) +
//         (0.7152 * sRGBToLinear(green($value2))) +
//         (0.0722 * sRGBToLinear(blue($value2)));

//     @return calc(
//         (max($luminance1, $luminance2) + 0.05) /
//             (min($luminance1, $luminance2) + 0.05)
//     );
// }



// TO BE TESTED to auto-calculated based on a certain ratio, creating high-contrast maps, etc...

@function find-safe-color($color, $ratio: 4.5) {
    $lightness: lightness($color);
    $value: $color;
    $contrast: 0;
    $i: 0;

    @if $lightness < 50 {
        $value: lighten($value, 5%);
        $contrast: calculate-contrast($color, $value);
        $i: $i + 1;

        @while $contrast < $ratio and $i < 20 {
            $value: lighten($value, 5%);
            $contrast: calculate-contrast($color, $value);
            $i: $i + 1;
        }
    } @else {
        $value: darken($value, 5%);
        $contrast: calculate-contrast($color, $value);
        $i: $i + 1;

        @while $contrast < $ratio and $i < 20 {
            $value: darken($value, 5%);
            $contrast: calculate-contrast($color, $value);
            $i: $i + 1;
        }
    }

    @if $i >= 20 {
        @debug "Color not safe";
    }

    @return ($value, $contrast);
}

// interesting to generate maps of same colors and corresponding to some defaults for some
// categories of html elements where the colors are used : to be checked later

@function wcag-safe-colors($base-color) {
    $colors: ();

    $wcag30: find-safe-color($base-color, 3);
    $wcag45: find-safe-color($base-color);
    $wcag70: find-safe-color($base-color, 7);

    // Generate shades
    $colors: map-merge(
        $colors,
        (
            "base-color": $base-color,
            "wcag20-aa-normal-color": nth($wcag45, 1),
            "wcag20-aa-normal-ratio": nth($wcag45, 2),
            "wcag20-aa-large-color": nth($wcag30, 1),
            "wcag20-aa-large-ratio": nth($wcag30, 2),
            "wcag21-aa-input-color": nth($wcag30, 1),
            "wcag21-aa-input-ratio": nth($wcag30, 1),
            "wcag21-aaa-normal-color": nth($wcag70, 1),
            "wcag21-aaa-normal-ratio": nth($wcag70, 2),
            "wcag21-aaa-large-color": nth($wcag45, 1),
            "wcag21-aaa-large-ratio": nth($wcag45, 2),
        )
    );

    @return $colors;
}


