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

@import 'tweaks-temp';
@import 'color-palette';

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

@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.26); }
    @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.3); }
    @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.4); }
  @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 #1A73E8; }
    @if ($theme == 'purple') { @return $purple-400; }
    @if ($theme == 'pink') { @return $pink-400; }
    @if ($theme == 'red') { @return $red-600; }
    @if ($theme == 'orange') { @return $orange-700; }
    @if ($theme == 'yellow') { @return $yellow-700; }
    @if ($theme == 'green') { @return $green-500; }
    @if ($theme == 'teal') { @return $teal-500; }
    @if ($theme == 'grey') { @return $grey-700; }
  } @else {
    @if ($theme == 'default') { @return #3281EA; }
    @if ($theme == 'purple') { @return $purple-300; }
    @if ($theme == 'pink') { @return $pink-300; }
    @if ($theme == 'red') { @return $red-500; }
    @if ($theme == 'orange') { @return $orange-600; }
    @if ($theme == 'yellow') { @return $yellow-a700; }
    @if ($theme == 'green') { @return $green-400; }
    @if ($theme == 'teal') { @return $teal-300; }
    @if ($theme == 'grey') { @return $grey-600; }
  }
}

@function background($type) {
  @if ($type == 'a') { @return #FFFFFF; }
  @if ($type == 'b') { @return #FAFAFA; }
  @if ($type == 'c') { @return #F2F2F2; }
  @if ($type == 'd') { @return #CCCCCC; }

  @if ($blackness == 'false') {
    @if ($type == 'e') { @return #212121; }
    @if ($type == 'f') { @return #242424; }
    @if ($type == 'g') { @return #2C2C2C; }
    @if ($type == 'h') { @return #3C3C3C; }
  } @else {
    @if ($type == 'e') { @return #000000; }
    @if ($type == 'f') { @return #030303; }
    @if ($type == 'g') { @return #0F0F0F; }
    @if ($type == 'h') { @return #121212; }
  }
}

//
// Basic colors
//

$primary:                               theme(color);
$drop_target_color:                     #FF7043;

$background:                            if($variant == 'light', background(c), background(e));
$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($blackness == 'true' and $variant == 'dark', rgba(#101010, 0.9), rgba(#363636, 0.9));
$osd:                                   background(e);
$scrim:                                 rgba(black, 0.6);
$scrim-alt:                             rgba(black, 0.3);
$scrim-inverse:                         rgba(white, 0.1);
$titlebar:                              if($topbar == 'dark', background(g), background(a));
$titlebar-backdrop:                     if($topbar == 'dark', background(g), background(a));

$panel-solid:                           if($blackness == 'true', black, #1F1F1F); // for Unity panel which doesn't allow translucent colors
$panel:                                 if($opacity == 'default', rgba($panel-solid, 0.65), $panel-solid);

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

//
// Misc colors
//

$link:                                  $primary;
$link-visited:                          if($variant == 'light', $purple-500, $purple-200);

$warning:                               if($variant == 'light', #F4B400, #FDD633);
$error:                                 if($variant == 'light', #D93025, #F28B82);
$success:                               if($variant == 'light', #0F9D58, #81C995);

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

$check_radio_primary:                   if($check_radio == 'default', $success, $primary);

$frame:                                 if($variant == 'light', rgba(black, 0.08), rgba(black, 0.16)); // instead of shadows
$border:                                if($variant == 'light', rgba(black, 0.12), rgba(white, 0.12));
$solid-border:                          if($variant == 'light', darken($background, 8%), lighten($background, 12%));
$border-alt:                            rgba(black, 0.16); // darker border color for Chrome and Firefox
$titlebar-indicator:                    if($topbar == 'dark', currentColor, $primary);

//
// 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);

//
// Titlebutton colors
//

$titlebutton-close:                     #fd5f51;
$titlebutton-max:                       #38c76a;
$titlebutton-min:                       #fdbe04;

// 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)));
