@use "sass:color";
@use "sass:list";
@use "sass:math";
@use "./rgb";

@function ch-calculate-alpha($topColor, $bottomColor, $targetColor) {
  $topRgb: rgb.red($topColor), rgb.green($topColor), rgb.blue($topColor);
  $bottomRgb: rgb.red($bottomColor), rgb.green($bottomColor), rgb.blue($bottomColor);
  $targetRgb: rgb.red($targetColor), rgb.green($targetColor), rgb.blue($targetColor);
  $alphaAvg: 0;

  @for $i from 1 through 3 {
    $alphaAvg: math.div($alphaAvg * ($i - 1) + math.div(list.nth($targetRgb, $i) - list.nth($bottomRgb, $i), list.nth($topRgb, $i) - list.nth($bottomRgb, $i)), $i);
  }

  @return $alphaAvg;
}

@function ch-calculate-top-color($alpha, $bottomColor, $targetColor) {
  $topRgb: ();
  $bottomRgb: rgb.red($bottomColor), rgb.green($bottomColor), rgb.blue($bottomColor);
  $targetRgb: rgb.red($targetColor), rgb.green($targetColor), rgb.blue($targetColor);

  @for $i from 1 through 3 {
    $topRgb: list.append($topRgb, math.div(list.nth($targetRgb, $i) - (1 - $alpha) * list.nth($bottomRgb, $i), $alpha));
  }

  @return rgb(list.nth($topRgb, 1), list.nth($topRgb, 2), list.nth($topRgb, 3));
}

