// 用 rgb 模型计算 hsb 色阶
@use "sass:math";
@use "sass:list";
@use "./helpers/hsb-rgb";
@use "./constants" as const;

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

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

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

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

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

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

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

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

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

@function hue-level($color-base, $index) {
  $h: math.round(hsb-h($color-base));
  $step: 1;
  $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
  }

  @return math.max($s, 5);
}

@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);
}

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

  // dls 应该是在 对6号基础色的 hsb 值进行 round 取整，再进行的色阶计算
  // 所以我们 hue-level，saturation-level，brightness-level 计算时也是先进行取整
  $h: hue-level($color-base, $index);
  $s: saturation-level($color-base, $index);
  $b: brightness-level($color-base, $index);

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