////
/// @group 06-colors
////
@use "sass:map";
@use "sass:meta";
@use "sass:color";
@use "config";

/// Gibt eine Farbe aus der Konfiguration zurück.
/// @param {String} $name - Key, der $palette Map
/// @param {Map} $palette [config.get("color-palette")] - Map, auf die der erste Parameter zugreift
/// @return {Color}
@function color($name, $palette: config.get("color-palette")) {
    @if map.has-key($palette, $name) {
        @return map.get($palette, $name);
    }
    @error "A color named " + meta.inspect($name) + " could not be found in given palette";
}

/// Konvertiert eine Farbe von Hexadezimal zu HSL.
/// @param {Color} $hex-value
/// @return {Color}
/// @example scss
/// .error {
///     --red-hsl: #{adventure.hex2hsl(#ff0000)};
///     background: {
///         color: hsl(var(--red-hsl));
///     }
///     box: {
///         shadow: 0 0 5px 0 hsl(var(--red-hsl) / 20%);
///     }
/// }
@function hex2hsl($hex-value) {
    @return color.channel($hex-value, "hue", $space: hsl) color.channel($hex-value, "saturation", $space: hsl) color.channel($hex-value, "lightness", $space: hsl);
}

/// Erzeugt Custom Properties für eine Farbe.
/// @param {String} $custom-property-name
/// @param {Color} $color
@mixin provide-custom-properties-for-color($custom-property-name, $color) {
    --#{$custom-property-name}: #{$color};
    --#{$custom-property-name}-h: #{color.channel($color, "hue", $space: hsl)};
    --#{$custom-property-name}-s: #{color.channel($color, "saturation", $space: hsl)};
    --#{$custom-property-name}-l: #{color.channel($color, "lightness", $space: hsl)};
    --#{$custom-property-name}-hsl: var(--#{$custom-property-name}-h) var(--#{$custom-property-name}-s) var(--#{$custom-property-name}-l);
}

/// Erzeugt Custom Properties für eine ganze Farbpalette.
/// @param {Map} $palette [config.get("color-palette")]
/// @example scss
/// :root {
///     @include adventure.provide-custom-properties-for-color-palette();
/// }
@mixin provide-custom-properties-for-color-palette($palette: config.get("color-palette")) {
    @each $color-name, $color-value in $palette {
        @include provide-custom-properties-for-color($color-name, $color-value);
    }
}
