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

@import 'color-palette';

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

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

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

  @if $brightness >= 128 {
    @return "light";
  } @else {
    @return "dark";
  }
}

$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(tone($color) == "dark", $on-dark, $on-light);

  @if saturation($color) > 50% or 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 lightness($color) >= 80% {
    @return rgba(white, .4);
  } @else if lightness($color) >= 40% {
    @return rgba(white, .2);
  } @else {
    @return rgba(white, .1);
  }
}

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

// Set the accessible opacity for the overlay depending on the given color.
@function overlay($state, $color, $background: transparent, $opacity-modifier: 0) {
  @if saturation($color) > 50% or saturation($background) > 50% {
    $opacity-modifier: .04;
  }

  $opacity: ();

  @if $state == "hover" {
    $opacity: .08 + $opacity-modifier;
  } @else if $state == "hover-alt" {
    $opacity: .04 + $opacity-modifier;
  } @else if $state == "focus" {
    $opacity: .08 + $opacity-modifier;
  } @else if $state == "pressed" {
    $opacity: .12 + $opacity-modifier;
  } @else if $state == "activated" {
    $opacity: .12 + $opacity-modifier;
  } @else if $state == "selected" {
    @return rgba($color, .24);
  } @else {
    @error "Invalid type: '#{$state}'";
  }

  @return mix(rgba($color, 1), $background, $opacity * 100%);
}

// Private variables for dark background colors
$-dark-background:                       #181818;
$-dark-surface-1dp:                      mix(white, $-dark-background, 5%);
$-dark-surface-4dp:                      mix(white, $-dark-background, 9%);
$-dark-surface-8dp:                      mix(white, $-dark-background, 12%);
$-dark-surface-switch:                   mix(white, $-dark-background, 60%);

//
// Main colors
//

$background:                            if($variant == 'light', #f2f2f2, $-dark-background);
$surface:                               if($variant == 'light', #ffffff, $-dark-surface-8dp);
$switch-surface:                        if($variant == 'light', #ffffff, $-dark-surface-switch); // Special case for switches
$base:                                  if($variant == 'light', #ffffff, $-dark-surface-1dp); // semi-surface with 1dp elevation
$base-alt:                              if($variant == 'light', #fafafa, $-dark-surface-1dp);
$on-surface:                            on($surface);

$primary-on-light:                      #1a73e8;
$primary-on-dark:                       #8ab4f8;
$primary:                               if($variant == 'light', $primary-on-light, $primary-on-dark);
$on-primary:                            on($primary);

$error-on-light:                        #d93025;
$error-on-dark:                         #f28b82;
$error:                                 if($variant == 'light', $error-on-light, $error-on-dark);
$on-error:                              on($error);

$warning:                               if($variant == 'light', #f4b400, #fdd633);
$on-warning:                            on($warning);

$success:                               if($variant == 'light', #0f9d58, #81c995);
$on-success:                            on($success);

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

$os-background:                         $-dark-background;
$on-os-background:                      on($os-background);

$tooltip:                               rgba(#616161, .9);
$on-tooltip:                            on($tooltip);

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

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

$panel:                                 if($topbar == 'dark', #1f1f1f, $scrim);
$on-panel:                              on($panel);

$panel-solid:                           if($topbar == 'dark', #1f1f1f, #cccccc); // for Unity panel which doesn't allow translucent colors
$on-panel-solid:                        on($panel-solid);

$titlebar:                              if($topbar == 'dark', if($variant == 'light', #383838, $-dark-surface-4dp), #e0e0e0);
$titlebar-backdrop:                     if($topbar == 'dark', if($variant == 'light', #303030, $-dark-surface-1dp), #d6d6d6);
$titlebar-indicator:                    if($topbar == 'dark', currentcolor, $primary);
$on-titlebar:                           on($titlebar);

//
// Overlay state colors
//

$overlay-selected:                      rgba($primary, .24);

//
// For “on” colors
//

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

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

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

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

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

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

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

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

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

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