@use "sass:color" as sc;
@use "sass:map";
@use "sass:meta";

////
///
/// This file defines all color settings, mixins and functions necessary
/// to provide the themify engine the style guide is based on.
///
/// @group colors
///
////

///
/// The colors map defines all basic and secondary colors.
///
/// @since 3.0.0
/// @group colors
/// @access public
/// @type map
///
$colors: (
  // 6.x colors
  'gray': #001823,
  'gray-medium': #687278,
  'gray-light': #e6e8ea,
  'gray-ultra-light': #f0f1f2,
  'blue': #005ba9,
  'blue-light': #ccdeee,
  'yellow': #ffdb5a,
  'yellow-pastel': #ffec93,
  'yellow-light': #fff8d4,
  'cyan': #009de0,
  'cyan-pastel': #7ed9ff,
  'cyan-light': #e6f8ff,
  'cyan-hover': #1abcff,
  'red': #ea0d33,
  'red-pastel': #ff8a9d,
  'red-light': #fcdbe1,
  'orange': #e06c00,
  'orange-pastel': #ffb673,
  'orange-light': #fff2e5,
  'green': #318b29,
  'green-hover': #41ba36,
  'green-pastel': #67c85e,
  'green-light': #e6f6e5,
  'green-forest': #246420,
  'green-forest-light': #b5e6b2,
  'white': #fff,
) !default;


///
/// This map describes different tints. These are used to calculate all
/// color swatches based on the base colors in $colors.
///
/// @since 3.0.0
/// @group colors
/// @access public
/// @type map
///
$tints: (
  1: 10%,
  2: 25%,
  3: 50%,
  -1: .75,
  -2: .6,
  -3: .5,
  -4: .25,
  -5: .1,
  -6: .06,
  -7: .02,
) !default;

///
/// A function to convert a rgba color to a rgb color.
///
/// @since 3.0.0
/// @group colors
/// @access public
///
/// @param {color} $base-color
///   The key of the color in the $colors map.
///
/// @param {color} $background [#fff]
///   The color to mix with.
///
@function rgba-to-rgb($base-color, $background: #fff) {
  @return sc.mix(rgb(sc.channel($base-color, "red"), sc.channel($base-color, "green"), sc.channel($base-color, "blue")), $background, sc.channel($base-color, "alpha") * 100%);
};

///
/// A function to get a color from the $colors map.
///
/// @since 3.0.0
/// @group colors
/// @access public
/// @require $colors
///
/// @param {color} $base-color
///   The key of the color in the $colors map.
///
/// @param {int} $tint [0]
///   The tint to be applied to the base color.
///
/// @param {boolean} $transparent [false]
///   True when you need a RGBA color, otherwise HEX value will return.
///
/// @param {map} colors$ [$colors]
///   The colors array to use to calculate the correct color tint.
///
@function coloring($base-color, $tint: 0, $transparent: false, $colors: $colors) {
  @if not map.has-key($colors, $base-color) {
    @warn "`#{meta.inspect($base-color)}` doesn't seem to be a valid base color.";
  }

  $color: null;
  @if $tint != 0 {
    @if $tint > 0 {
      $color: shade($base-color, $tint, $colors);
    }
    @if $tint < 0 {
      $color: tint($base-color, $tint, $colors);
    }

    @if $transparent {
      @return $color;
    }
    @else {
      @return rgba-to-rgb($color);
    }
  }
  @if $tint == 0 {
    @return map.get($colors, $base-color);
  }
};

/// Backwards-compat alias for `coloring()`.
/// @deprecated Use `coloring()` instead to avoid conflicts with `sass:color`.
/// Mirrors the `coloring()` signature so existing calls keep working.
/// @param {String} $name    Base color key (e.g. 'red', 'gray').
/// @param {Number} $tint    Optional tint key from $tints (e.g. 2, -1). Default 0.
/// @param {Boolean} $transparent  If true, returns RGBA when tinted.
/// @param {Map} $colorsMap  Colors map to resolve from (defaults to $colors).
/// @return {Color}
@function color($name, $tint: 0, $transparent: false, $colors-map: $colors) {
  @return coloring($name, $tint, $transparent, $colors-map);
}

///
/// A function to adjust the opacity of a base color.
///
/// @since 3.0.0
/// @group colors
/// @access public
/// @require $tints
/// @require $colors
///
/// @param {color} $base-color
///   The base color to be modified.
///
/// @param {int} $tint
///   The tint value to modify the base color with.
///
/// @param {map} colors$ [$colors]
///   The colors array to use to calculate the correct color tint.
///
@function tint($base-color, $tint, $colors: $colors) {
  @if not map.has-key($tints, $tint) {
    @warn "`#{meta.inspect($tint)}` doesn't seem to be a valid tint value.";
  }

  @return rgba(map.get($colors, $base-color), map.get($tints, $tint));
};

///
/// A function to darken a base color.
///
/// @since 3.0.0
/// @group colors
/// @access public
/// @require $tints
/// @require $colors
///
/// @param {color} $base-color
///   The base color to be modified.
///
/// @param {int} $tint
///   The value to shade the base color with.
///
/// @param {map} colors$ [$colors]
///   The colors array to use to calculate the correct color tint.
///
@function shade($base-color, $tint, $colors: $colors) {
  $color-black: #000;

  @if not map.has-key($tints, $tint) {
    @warn "`#{meta.inspect($tint)}` doesn't seem to be a valid tint value.";
  }

  @return sc.mix($color-black, map.get($colors, $base-color), map.get($tints, $tint));
};
