@use "sass:math";
@use "sass:color";

@function toRGB($color) {
    @return red($color)+", "+green($color)+", "+blue($color);
}

@function getContrastOld($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 #ffffff;
    }

    @else {
        @return #000000;
    }
}

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

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

@function getContrast($bgColor, $lightColor: #fff, $darkColor: #000) {
    $r: red($bgColor);
    $g: green($bgColor);
    $b: blue($bgColor);
    $ui_r: calc($r / 255);
    $ui_g: calc($g / 255);
    $ui_b: calc($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 get-rgb-transformer($rgb) {
    @if ($rgb < .03928) {
        @return calc($rgb / 12.92);
    } @else {
        @return math.pow(math.div($rgb + .055, 1.055), 2.4);
    }
}


@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: calc($rgb / 255);

        $rgb: get-rgb-transformer($rgb);

        $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: math.div($luminance1, $luminance2);

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

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

    @return $ratio;
}

@function tint-color($color, $weight) {
    @return color.mix(white, $color, $weight);
}

// Shade a color: mix a color with black
@function shade-color($color, $weight) {
    @return color.mix(black, $color, $weight);
}

@function shift-color($color, $weight) {
    @if $weight > 0 {
        @return shade-color($color, $weight);
    } @else {
        @return tint-color($color, -$weight);
    }
}

