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

@import 'tweaks-temp';

@function gtkalpha($c, $a) {
  @return unquote("alpha(#{$c}, #{$a})");
}

@function gtkmix($c1,$c2,$r) {
  $ratio: 1 -  $r / 100%; // match SCSS mix()
  @return unquote("mix(#{$c1},#{$c2},#{$ratio})");
}

@function gtkshade($c,$s) {
  @return unquote("shade(#{$c},#{$s})");
}

@function gtkcolor($c) {
  @return unquote("@#{$c}");
}

@function on($color, $state: 'primary') {
  // Allow 'light' or 'dark' to $color
  @if ($color == 'light') { $color: $white; }
  @if ($color == 'dark') { $color: $black; }

  // Calculate color brightness according to https://www.w3.org/TR/AERT/#color-contrast
  $brightness: (red($color) * 299 + green($color) * 587 + blue($color) * 114) / 1000;

  @if ($brightness >= 156) {
    @if ($state == 'primary') { @return rgba($black, 0.87); }
    @if ($state == 'secondary') { @return rgba($black, 0.6); }
    @if ($state == 'disabled') { @return rgba($black, 0.38); }
    @if ($state == 'secondary-disabled') { @return rgba($black, 0.3); }
    @if ($state == 'track') { @return rgba($black, 0.26); }
    @if ($state == 'track-disabled') { @return rgba($black, 0.12); }
    @if ($state == 'divider') { @return rgba($black, 0.12); }
    @if ($state == 'secondary-fill') { @return rgba($black, 0.08); }
    @if ($state == 'fill') { @return rgba($black, 0.04); }
  } @else {
    @if ($state == 'primary') { @return $white; }
    @if ($state == 'secondary') { @return rgba($white, 0.7); }
    @if ($state == 'disabled') { @return rgba($white, 0.5); }
    @if ($state == 'secondary-disabled') { @return rgba($white, 0.32); }
    @if ($state == 'track') { @return rgba($white, 0.3); }
    @if ($state == 'track-disabled') { @return rgba($white, 0.12); }
    @if ($state == 'divider') { @return rgba($white, 0.12); }
    @if ($state == 'secondary-fill') { @return rgba($white, 0.08); }
    @if ($state == 'fill') { @return rgba($white, 0.04); }
  }
}

@function highlight($color) {
  @if (lightness($color) >= 80%) { @return rgba($white, 0.15); }
  @else if (lightness($color) >= 40%) { @return rgba($white, 0.2); }
  @else { @return rgba($white, 0.1); }
}

@function theme($color) {
  @if ($variant == 'light') {
    @if ($theme == 'default') { @return $default-dark; }
    @if ($theme == 'purple') { @return $purple-dark; }
    @if ($theme == 'pink') { @return $pink-dark; }
    @if ($theme == 'red') { @return $red-dark; }
    @if ($theme == 'orange') { @return $orange-dark; }
    @if ($theme == 'yellow') { @return $yellow-dark; }
    @if ($theme == 'green') { @return $green-dark; }
    @if ($theme == 'teal') { @return $teal-dark; }
    @if ($theme == 'grey') { @return $grey-600; }
  } @else {
    @if ($theme == 'default') { @return $default-light; }
    @if ($theme == 'purple') { @return $purple-light; }
    @if ($theme == 'pink') { @return $pink-light; }
    @if ($theme == 'red') { @return $red-light; }
    @if ($theme == 'orange') { @return $orange-light; }
    @if ($theme == 'yellow') { @return $yellow-light; }
    @if ($theme == 'green') { @return $green-light; }
    @if ($theme == 'teal') { @return $teal-light; }
    @if ($theme == 'grey') { @return $grey-200; }
  }
}

@function background($type) {
  @if ($type == 'a') { @return $white; }
  @if ($type == 'b') { @return $grey-050; }
  @if ($type == 'c') { @return $grey-100; }
  @if ($type == 'd') { @return $grey-250; }

  @if ($blackness == 'true') {
    @if ($type == 'e') { @return $black; }
    @if ($type == 'f') { @return $grey-950; }
    @if ($type == 'g') { @return $grey-900; }
    @if ($type == 'h') { @return $grey-850; }
  } @else {
    @if ($type == 'e') { @return $grey-800; }
    @if ($type == 'f') { @return $grey-750; }
    @if ($type == 'g') { @return $grey-700; }
    @if ($type == 'h') { @return $grey-650; }
  }
}

@function overlay($color, $state: 'normal') {
  @if ($state == 'normal') { @return rgba(on($color), 0.06); }
  @if ($state == 'hover') { @return rgba(on($color), 0.12); }
  @if ($state == 'focus') { @return rgba(on($color), 0.08); }
  @if ($state == 'active') { @return rgba(on($color), 0.20); }
  @if ($state == 'checked') { @return rgba(on($color), 0.15); }
  @if ($state == 'selected') { @return rgba(on($color), 0.1); }
}

//
// Basic colors
//

$primary:                               theme(color);
$drop_target_color:                     $orange-dark;
$indicator:                             theme(color);
$titlebar-indicator:                    if($variant == 'dark', currentColor, $primary);
$inverse-indicator:                     if($theme == 'grey', $white, $primary);
$applet-primary:                        if($theme == 'grey' and variant == 'light' and $topbar == 'dark', $grey-200, $primary);

$background:                            if($variant == 'light', background(a), background(g));
$surface:                               if($variant == 'light', background(a), background(h));
$base:                                  if($variant == 'light', background(a), background(g));
$base-alt:                              if($variant == 'light', background(b), background(f));

$tooltip:                               if($variant == 'dark', rgba(darken(background(e), 3%), 0.9), rgba(darken(background(h), 3%), 0.9));
$osd:                                   background(e);
$scrim:                                 background(f);
$scrim-alt:                             background(g);
$scrim-inverse:                         rgba($white, 0.1);
$titlebar:                              if($topbar == 'dark', background(f), background(c));
$titlebar-backdrop:                     if($topbar == 'dark', background(g), background(b));
$titlebar-primary:                      if($topbar == 'dark' and $theme == 'grey' and $variant == 'light', lighten($primary, 15%), $primary);
$sidebar:                               if($topbar == 'dark' and $variant == 'light', $titlebar, $base-alt);
$sidebar-backdrop:                      if($topbar == 'dark' and $variant == 'light', $titlebar-backdrop, $background);
$popover:                               if($variant == 'light', background(a), background(g));
$panel-solid:                           if($topbar == 'dark', background(e), background(c)); // for Unity panel which doesn't allow translucent colors
$panel:                                 rgba($panel-solid, $panel_opacity);

$button:                                if($variant == 'dark', on($background, secondary-fill), on($background, fill));
$entry:                                 if($variant == 'dark', on($background, secondary-fill), on($background, fill));

//
// Misc colors
//

$link:                                  $links;
$link-visited:                          if($variant == 'light', $purple-dark, $purple-light);

$warning:                               if($variant == 'light', $yellow-dark, $yellow-light);
$error:                                 if($variant == 'light', $red-dark, $red-light);
$success:                               if($variant == 'light', $green-dark, $green-light);

$suggested:                             $primary;
$destructive:                           $error;

$assets-color:                          $primary;

@if $colorscheme == 'dracula' {
  $assets-color:                        if($variant == 'light', $green-dark, $green-light);
}

$frame:                                 if($variant == 'light', rgba($black, 0.12), rgba($black, 0.25));
$border:                                if($variant == 'light', rgba($black, 0.12), rgba($white, 0.12));
$shade:                                 if($variant == 'light', rgba($black, 0.08), rgba($white, 0.08));
$window-border:                         if($variant == 'light', rgba($black, 0.12), rgba($black, 0.75));
$solid-border:                          if($variant == 'light', mix(black, $base, 12%), mix(white, $base, 12%));
$border-alt:                            rgba($black, 0.16); // darker border color for Chrome and Firefox

//
// Overlay state colors
//

$overlay-normal:                        gtkalpha(currentColor, 0.05);
$overlay-hover:                         gtkalpha(currentColor, 0.08);
$overlay-focus:                         gtkalpha(currentColor, 0.08);
$overlay-focus-hover:                   gtkalpha(currentColor, 0.16);
$overlay-active:                        gtkalpha(currentColor, 0.12);
$overlay-checked:                       gtkalpha(currentColor, 0.10);
$overlay-selected:                      gtkalpha(currentColor, 0.06);

//
// Text and derivative colors
//

$text:                                  on($background);
$text-secondary:                        on($background, secondary);
$text-disabled:                         on($background, disabled);
$text-secondary-disabled:               on($background, secondary-disabled);
$track:                                 on($background, track);
$track-disabled:                        on($background, track-disabled);
$divider:                               on($background, divider);
$fill:                                  on($background, fill);

$titlebar-text:                         on($titlebar);
$titlebar-text-secondary:               on($titlebar, secondary);
$titlebar-text-disabled:                on($titlebar, disabled);
$titlebar-text-secondary-disabled:      on($titlebar, secondary-disabled);
$titlebar-track:                        on($titlebar, track);
$titlebar-track-disabled:               on($titlebar, track-disabled);
$titlebar-divider:                      on($titlebar, divider);
$titlebar-fill:                         on($titlebar, fill);

$panel-text:                            on($panel);
$panel-text-secondary:                  on($panel, secondary);
$panel-text-disabled:                   on($panel, disabled);
$panel-text-secondary-disabled:         on($panel, secondary-disabled);
$panel-track:                           on($panel, track);
$panel-track-disabled:                  on($panel, track-disabled);
$panel-divider:                         on($panel, divider);
$panel-fill:                            on($panel, fill);


// Titlebutton colors

$titlebutton-close:                      $button-close;
$titlebutton-max:                        $button-max;
$titlebutton-min:                        $button-min;

// workaround for GTK3 @placeholder_text_color which doesn't allow translucent colors

$placeholder_text_color:                if($variant == 'light', mix($black, $base, percentage(0.6)), mix($white, $base, percentage(0.7)));
