// 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 == '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 == '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-400; }
    @if ($theme == 'grey') { @return $grey-600; }
  }
}

@function titlebar($state: 'normal') {
  @if ($variant == 'light' and $topbar == 'light' and $state=='normal' and $blur == 'false') { @return #F7F7F7; }
  @if ($variant == 'light' and $topbar == 'light' and $state=='backdrop' and $blur == 'false') { @return #F2F2F2; }
  @if ($variant == 'light' and $topbar == 'light' and $state=='normal' and $blur == 'true') { @return #FFFFFF; }
  @if ($variant == 'light' and $topbar == 'light' and $state=='backdrop' and $blur == 'true') { @return #FCFCFC; }
  @if ($variant == 'light' and $topbar == 'dark' and $state=='normal') { @return #2C2C2C; }
  @if ($variant == 'light' and $topbar == 'dark' and $state=='backdrop') { @return #3c3c3c; }
  @if ($variant == 'dark' and $state=='normal') { @return #202020; }
  @if ($variant == 'dark' and $state=='backdrop') { @return #2C2C2C; }
}

//
// Basic colors
//
$primary:                               theme(color);
$entry-highlight:                       lighten($primary, 25%);
$drop_target_color:                     #FF7043;

$background:                            if($variant == 'light', #F2F2F2, #333333);
$surface:                               if($variant == 'light', #FFFFFF, #3C3C3C);
$base:                                  if($variant == 'light', #FFFFFF, #2B2B2B); // semi-surface with 1dp elevation
$base-alt:                              if($variant == 'light', #FAFAFA, #303030);

$scrim:                                 rgba(black, 0.6);
$scrim-alt:                             rgba(black, 0.3);
$scrim-inverse:                         rgba(white, 0.1);

$menu:                                  if($opacity == 'default', rgba($surface, 0.98), $surface);
$tooltip:                               rgba(#363636, 0.9);
$titlebar:                              titlebar(normal);
$titlebar-backdrop:                     titlebar(backdrop);
$panel:                                 if($topbar == 'light', rgba(#F7F7F7, 0.8), rgba(#1F1F1F, 0.8));
$panel-solid:                           if($topbar == 'light', #F7F7F7, #1F1F1F); // for Unity panel which doesn't allow translucent colors

$blur_opacity:                          if($variant == 'light', 0.85, 0.5);

@if $blur == 'true' {
  $background:                          if($variant == 'light', rgba(#FFFFFF, $blur_opacity), rgba(#333333, $blur_opacity));
  $surface:                             if($variant == 'light', rgba(#FFFFFF, $blur_opacity), rgba(#3C3C3C, $blur_opacity));
  $base:                                if($variant == 'light', rgba(#FFFFFF, $blur_opacity), rgba(#2B2B2B, $blur_opacity)); // semi-surface with 1dp elevation
  $base-alt:                            if($variant == 'light', rgba(#FAFAFA, $blur_opacity), rgba(#303030, $blur_opacity));
  $titlebar:                            rgba(titlebar(normal), $blur_opacity);
  $titlebar-backdrop:                   rgba(titlebar(backdrop), $blur_opacity);
  $panel:                               if($topbar == 'light', rgba(#F7F7F7, $blur_opacity), rgba(#1F1F1F, $blur_opacity));
}

//
// Misc colors
//

$link:                                  $blue-600;
$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;

$frame:                                 if($variant == 'light', rgba(black, 0.08), rgba(black, 0.16)); // instead of shadows
$border:                                if($variant == 'light', rgba(black, 0.08), rgba(white, 0.12)); // Buttons and Entries
$solid-border:                          if($variant == 'light', darken($background, 10%), lighten($background, 10%));
$border-alt:                            rgba(black, 0.16); // darker border color for Chrome and Firefox
$border-bottom:                         if($variant == 'light', rgba(black, 0.18), rgba(white, 0.08)); // Buttons and Entries
$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.16);
$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);

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