// When color definition differs for dark and light variant,
// it gets @if ed depending on $variant

@use "sass:color";
@use "sass:math";
@use "sass:string";
@use "theme";

@function gtkalpha($color, $alpha) {
  @return string.unquote("alpha(#{$color}, #{$alpha})");
}

// Determine whether the color has alpha.
@function _has-alpha($color) {
  @return if(color.alpha($color) < 1, true, false);
}

// Determine whether the color is dark.
@function _is-dark($color) {
  // Calculate color brightness as per https://www.w3.org/TR/AERT/#color-contrast
  $brightness: (color.red($color) * 299 + color.green($color) * 587 + color.blue($color) * 114) / 1000;

  @return if($brightness < 128, true, false);
}

$on-light: #000;
$on-dark: #fff;

// Determine whether to use dark or light color on top of given color
// to meet accessibility standards for contrast.
@function on($color) {
  $contrast-color: if(_is-dark($color), $on-dark, $on-light);

  @if color.saturation($color) > 50% or color.alpha($color) < 1 {
    @return $contrast-color;
  } @else {
    @return rgba($contrast-color, .87);
  }
}

// Determine the strength of highlight on top of given color.
@function highlight($color) {
  @if color.lightness($color) >= 80% {
    @return rgba(white, .4);
  } @else if color.lightness($color) >= 40% {
    @return rgba(white, .2);
  } @else {
    @return rgba(white, .05);
  }
}

// Make translucent color opaque by blending with the background color.
@function opacify($color, $on) {
  @return color.mix(color.change($color, $alpha: 1), $on, color.alpha($color) * 100%);
}

//
// Main colors
//

$inverse-surface-z1: #2e313a; // specific to budgie
$inverse-surface-z8: #fff; // specific to budgie
$sidebar-surface-z1: #eceff3; // specific to budgie
$surface-z0: #f5f6f7; //#f9f9f9; Materia
$surface-z1: #f5f6f7; //#fff; Materia
$surface-z8: #f5f6f7; //#fff; Materia
$surface-switch-thumb: #fff;
$on-surface: on($surface-z0);

$primary-on-light: #5294e2;//#1967d2; Materia
$primary-on-dark: #8CAAEE;//#8ab4f8 Materia;
$primary: $primary-on-light;
$on-primary: on($primary);

$error-on-light: #d93025;
$error-on-dark: #fc4138;//#f28b82; Materia
$error: $error-on-light;
$on-error: on($error);

$warning-on-light: #f9ab00;
$warning-on-dark: #fdd663;
$warning: $warning-on-light;
$on-warning: on($warning);

$success-on-light: #1e8e3e;
$success-on-dark: #81c995;
$success: $success-on-light;
$on-success: on($success);

$visited-on-light: #9334e6;
$visited-on-dark: #c58af9;
$visited: $visited-on-light;
$on-visited: on($visited);

$system: #383c4a; // Materia #212121;
$on-system: on($system);

$tooltip: #2e313a;// Materia rgba(#616161, .9);
$on-tooltip: on($tooltip);

$scrim: #383c4a;// Materia rgba(black, .6);
$on-scrim: on($scrim);

$scrim-alt: rgba(black, .3);
$on-scrim-alt: on($scrim-alt);

$panel: #20222a; // Materia #212121;
$on-panel: on($panel);

// for Unity panel which doesn't allow translucent colors
$panel-solid: $panel;
$on-panel-solid: on($panel-solid);

$titlebar: #414249;//#424242; Materia
$titlebar-backdrop: #2e313a;//#303030; Materia
$on-titlebar: on($titlebar);

$titlebar-indicator: currentcolor;

@if theme.$dark-theme {
  $sidebar-surface-z1: #2e313a; // specific to budgie
  $surface-z0: #20222a;// Materia #121212;
  $surface-z1: #2e313a;// Materia color.mix(white, $surface-z0, 5%);
  $surface-z8: #383c4a;// Materia color.mix(white, $surface-z0, 12%);
  $surface-switch-thumb: color.mix(white, $surface-z0, 60%);
  $on-surface: color.mix(white, $surface-z0, 70%);; //Materia on($surface-z0);

  $primary: $primary-on-dark;
  $on-primary: on($primary);

  $error: $error-on-dark;
  $on-error: on($error);

  $warning: $warning-on-dark;
  $on-warning: on($warning);

  $success: $success-on-dark;
  $on-success: on($success);

  $visited: $visited-on-dark;
  $on-visited: on($visited);

  $titlebar: color.mix(white, $surface-z0, 9%);
  $titlebar-backdrop: $surface-z1;
  $on-titlebar: on($titlebar);
}

@if theme.$darkest-theme {
  $inverse-surface-z1: #020202;
  $sidebar-surface-z1: #0a0a0a;
  $surface-z0: #0a0a0a;
  $surface-z1: #0a0a0a;
  $surface-z8: #020202;
  $surface-switch-thumb: color.mix(white, $surface-z0, 75%);
  $on-surface: color.mix(white, $surface-z0, 100%);

  $primary: #8CAAEE;
  $on-primary: on($primary);

  $error: #e53935;
  $on-error: on($error);

  $warning: #ff9800;
  $on-warning: on($warning);

  $success: #43a047;

  $visited: #8e24aa;

  $titlebar: #000000;
  $titlebar-backdrop: #0a0a0a;
  $on-titlebar: #ffffff;

  $panel: #020202;
  $on-panel: on($panel);
}

@if theme.$frappe-theme {
  $inverse-surface-z1: #292C3C;
  $sidebar-surface-z1: #292C3C;
  $surface-z0: #303446;
  $surface-z1: #414559;
  $surface-z8: #292C3C;
  $surface-switch-thumb: #8CAAEE;
  $on-surface: #C6D0F5;

  $primary: #8CAAEE;
  $on-primary: #292C3C;

  $error: #E78284;
  $on-error: #292C3C;

  $warning: #EF9F76;
  $on-warning: #292C3C;

  $success: #A6D189;

  $visited: #CA9EE6;

  $titlebar: #232634;
  $titlebar-backdrop: #292C3C;
  $on-titlebar: #C6D0F5;

  $panel: #232634;
  $on-panel: #C6D0F5;

  $tooltip: #292C3C;
  $on-tooltip: #C6D0F5;
}

@if theme.$light-topbar {
  $panel: $scrim;
  $on-panel: on($panel);

  $panel-solid: #e0e0e0;
  $on-panel-solid: on($panel-solid);

  $titlebar: #f0f0f0;
  $titlebar-backdrop: #ebebeb;
  $on-titlebar: on($titlebar);

  $titlebar-indicator: $primary;

  $inverse-surface-z1: $surface-z1; // specific to budgie
  $inverse-surface-z8: black; // specific to budgie
  $surface-z0: #f9f9f9; //Materia
  $surface-z1: #fff; //Materia
  $surface-z8: #fff; //Materia

}

//
// Overlay state colors
//

// Determine the overlay color depending on the given color and opacity.
@function _overlay($color, $opacity, $on) {
  $opacity-modifier: 0;

  @if color.saturation($color) > 50% or color.saturation($on) > 50% {
    $opacity-modifier: .04;
  }

  @return color.mix(rgba($color, 1), $on, math.percentage($opacity + $opacity-modifier));
}

@function hover-overlay($color, $opacity-modifier: 0, $on: transparent, $alt: false) {
  $opacity: .08;

  @if $alt {
    $opacity: .04;
  }

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function focus-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .08;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function pressed-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .12;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function dragged-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .08;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function activated-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .12;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function selected-overlay($color, $opacity-modifier: 0) {
  $opacity: .24;

  @if $color == $on-surface {
    $color: $primary;
  }

  @return rgba($color, $opacity + $opacity-modifier);
}

$selected-overlay: selected-overlay($on-surface);

//
// For “on” colors
//

@function primary($color) {
  @return if(_is-dark($color), $primary-on-light, $primary-on-dark);
}

@function error($color) {
  @return if(_is-dark($color), $error-on-light, $error-on-dark);
}

@function hint($color) {
  @return if(_has-alpha($color), rgba($color, .6), rgba($color, .7));
}

@function disabled($color) {
  @return if(_has-alpha($color), rgba($color, .38), rgba($color, .5));
}

@function disabled-hint($color) {
  @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
}

@function stroke($color) {
  @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
}

@function disabled-stroke($color) {
  @return if(_has-alpha($color), rgba($color, .12), rgba($color, .2));
}

@function divider($color) {
  @return if(_has-alpha($color), rgba($color, .12), rgba($color, .2));
}

@function fill($color) {
  @return if(_has-alpha($color), rgba($color, .08), rgba($color, .08));
}

@function entry-fill($color) {
  @return if(_has-alpha($color), rgba($color, .04), rgba($color, .04));
}

@function scrollbar-thumb($color, $state: null) {
  @if $state == null {
    @return if(_has-alpha($color), rgba($color, .38), rgba($color, .5));
  } @else if $state == "hover" {
    @return if(_has-alpha($color), rgba($color, .48), rgba($color, .6));
  } @else if $state == "pressed" {
    @return if(_has-alpha($color), rgba($color, .6), rgba($color, .7));
  } @else if $state == "disabled" {
    @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
  } @else {
    @error "Invalid type: '#{$state}'";
  }
}

