// 用 hsl 模型计算 hsb 色阶，计算结果和 _level-hsb-rgb 一样，仅作为"备选方案"
@use "sass:math";
@use "sass:list";
@use "helpers/hsb-hsl";
@use "./constants" as const;

@function hsb-h($color) {
  $hsb: hsb-hsl.hsl-to-hsb($color);

  @return list.nth($hsb, 1);
}

@function hsb-s($color) {
  $hsb: hsb-hsl.hsl-to-hsb($color);

  @return list.nth($hsb, 2);
}

@function hsb-b($color) {
  $hsb: hsb-hsl.hsl-to-hsb($color);

  @return list.nth($hsb, 3);
}

@function hsb($h, $s, $b) {
  @return hsb-hsl.hsb-to-hsl($h, $s, $b)
}

@function color-neutral-level($color-base, $index) {
  $h: math.round(hsb-h($color-base));
  $s: math.percentage(math.div(list.nth(const.$neutral-saturation-list, $index), 100));
  $b: math.percentage(math.div(list.nth(const.$neutral-brightness-list, $index), 100));

  @if $h == 0deg {
    @return hsb(0, 0, $b)
  }

  @return hsb($h, $s, $b);
}

@function hue-level($color-base, $index) {
  $h: math.round(hsb-h($color-base));
  $step: 1deg;
  $h: $h - ($index - const.$color-base-index) * $step;

  @return $h
}

@function saturation-level($color-base, $index) {
  $s: math.round(hsb-s($color-base));
  $step: 5%;
  @if $index < const.$color-base-index {
    $step: math.round(math.div($s, 5));
  }

  $s: $s + ($index - const.$color-base-index) * $step;

  @if $s > 100%  {
    @return 100%
  }
  @else if $s < 5% {
    @return 5%
  }

  @return $s;
}

@function brightness-level($color-base, $index) {
  $b: math.round(hsb-b($color-base));
  $step: -15%;
  @if $index < const.$color-base-index {
    $step: -5%;
  }

  $b: $b + ($index - const.$color-base-index) * $step;

  @if $b > 100% {
    @return 100%;
  }

  @return math.max($b, 0%);
  @return $b;
}

@function color-level($color-base, $index) {
  @if ($index == const.$color-base-index) {
    @return $color-base;
  }

  $h: hue-level($color-base, $index);
  $s: saturation-level($color-base, $index);
  $b: brightness-level($color-base, $index);
  @return hsb($h, $s, $b);
}
