@import "../settings/colours-palette";
@import "../settings/colours-organisations";
@import "../settings/warnings";

////
/// @group helpers/colour
////

/// Get colour
///
/// @param {String | Colour} $colour - Name of colour from the colour palette
///   (`$govuk-colours`)
/// @param {String | Colour | Boolean} $legacy [false] - Deprecated.
///   The `$legacy` parameter is deprecated and is non-operational, as the
///   legacy colour palette has been removed. The parameter will be removed in
///   the next major version.
/// @return {Colour} Representation of named colour
///
/// @throw if `$colour` is not a colour from the colour palette
/// @access public

@function govuk-colour($colour, $legacy: false) {
  // Output a warning if $legacy is set to anything.
  @if $legacy and _should-warn("legacy-colour-param") {
    @warn _warning-text("legacy-colour-param", "The `$legacy` parameter of " +
    "`govuk-colour` is deprecated and is non-operational. It will be " +
    "removed in the next major version.");
  }

  @if type-of($colour) == "color" {
    // stylelint-disable-next-line scss/function-quote-no-quoted-strings-inside
    $colour: quote("#{$colour}");
  }

  @if not map-has-key($govuk-colours, $colour) {
    @error "Unknown colour `#{$colour}`";
  }

  @return map-get($govuk-colours, $colour);
}

/// Get the colour for a government organisation
///
/// @param {String} $organisation - Organisation name, lowercase, hyphenated
/// @param {Boolean} $websafe - Deprecated. Use $contrast-safe instead.
/// @param {Boolean} $contrast-safe [true] - By default a version of the colour
///   will be returned which has a minimum 4.5:1 contrast ratio when used with
///   white, as per the WCAG 2.1 Level AA guidelines. If you want to use the
///   non-contrast safe version you can set this to `false` but your should
///   ensure that you still meets contrast requirements for accessibility -
///   for example, do not use the non-contrast safe version for text.
///
/// @return {Colour} Representation of colour for organisation
/// @throw if `$organisation` is not a known organisation
/// @access public

@function govuk-organisation-colour($organisation, $websafe: null, $contrast-safe: true) {
  // Check if the $organisation exists in the aliases map. If so, change the
  // value of $organisation to the aliased value.
  @if map-has-key($_govuk_colours-organisations-aliases, $organisation) {
    $organisation: map-get($_govuk_colours-organisations-aliases, $organisation);
  }

  // Check to see if the organisation exists
  @if not map-has-key($govuk-colours-organisations, $organisation) {
    @error "Unknown organisation `#{$organisation}`";
  }

  // Output a warning if $websafe is set.
  @if $websafe and not index($govuk-suppressed-warnings, "organisation-colour-websafe-param") {
    @warn _warning-text("organisation-colour-websafe-param",
      "The `$websafe` parameter of `govuk-organisation-colour` has been " +
      "renamed to `$contrast-safe`. The old parameter name will be removed in " +
      "the next major version."
    );
  }

  $org-colour: map-get($govuk-colours-organisations, $organisation);

  @if map-has-key($org-colour, deprecation-message) and not index($govuk-suppressed-warnings, "organisation-colours") {
    @warn _warning-text(
      "organisation-colours",
      map-get($org-colour, deprecation-message)
    );
  }

  // If the $websafe parameter is being used (it has been explicitly set as true
  // or false), assume the user hasn't updated to use $contrast-safe yet and map
  // the old parameter's value onto the new parameter.
  @if type-of($websafe) != "null" {
    $contrast-safe: $websafe;
  }

  // Determine the contrast-safe key to use depending on whether it's the new
  // palette or the legacy palette
  $safe-key: if($govuk-new-organisation-colours, "contrast-safe", "colour-websafe");

  @if $contrast-safe and map-has-key($org-colour, $safe-key) {
    @return map-get($org-colour, $safe-key);
  } @else {
    @return map-get($org-colour, colour);
  }
}

/// Make a colour darker by mixing it with black
///
/// @param {Colour} $colour - colour to shade
/// @param {Number} $percentage - percentage of black to mix with $colour
/// @return {Colour}
/// @access public

@function govuk-shade($colour, $percentage) {
  @return mix(#000000, $colour, $percentage);
}

/// Make a colour lighter by mixing it with white
///
/// @param {Colour} $colour - colour to tint
/// @param {Number} $percentage - percentage of white to mix with $colour
/// @return {Colour}
/// @access public

@function govuk-tint($colour, $percentage) {
  @return mix(govuk-colour("white"), $colour, $percentage);
}

/*# sourceMappingURL=_colour.scss.map */
