// _functions.scss
@use 'sass:color';
@use 'sass:math';
@use 'sass:map';

// Convert px to rem
@function rem($pixels) {
    @return math.div($pixels, 16px) * 1rem;
}

// Convert px to em
@function em($pixels) {
    @return math.div($pixels, 16px) * 1em;
}

// Get color from theme
@function theme-color($key) {
    @if map-has-key($theme-colors, $key) {
        @return map-get($theme-colors, $key);
    }
    
    @warn "Unknown `#{$key}` in $theme-colors.";
    @return null;
}

// Calculate contrast color
@function contrast-color($color) {
    $r: color.red($color);
    $g: color.green($color);
    $b: color.blue($color);
    $luminance: (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
    
    @if $luminance > 0.5 {
        @return $gray-900;
    } @else {
        @return $white;
    }
}

// Strip unit from number
@function strip-unit($number) {
    @if type-of($number) == 'number' and not unitless($number) {
        @return math.div($number, ($number * 0 + 1));
    }
    @return $number;
}