// 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 is "light" or "dark".
@function tone($color) {
  @if $color == "dark" or $color == "light" {
    @return $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";
  }
}

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

  @if not map-has-key(map-get($on-colors, $contrast-tone), $type) {
    @error "Invalid type: '#{$type}'";
  }

  @return map-get(map-get($on-colors, $contrast-tone), $type);
}

$oomox-dark: if(tone(%FG%) == dark, %FG%, %BG%); // BG should be the darkest background-color in MD theory.
$oomox-light: if(tone(%FG%) == light, %FG%, %MATERIA_SURFACE%); // MATERIA_SURFACE should be the lightest background-color in MD theory.

$on-colors: (
  dark: (
    primary: %SEL_BG%,
    error: %TERMINAL_COLOR9%,
    text: $oomox-dark,
    text2: rgba($oomox-dark, .7),
    text-disabled: rgba($oomox-dark, .5),
    text2-disabled: rgba($oomox-dark, .3),
    stroke: rgba($oomox-dark, .3),
    stroke-disabled: rgba($oomox-dark, .12),
    divider: rgba($oomox-dark, .12),
    fill: rgba($oomox-dark, .08),
    entry-fill: rgba($oomox-dark, .04)
  ),
  light: (
    primary: %SEL_BG%,
    error: %TERMINAL_COLOR9%,
    text: $oomox-light,
    text2: rgba($oomox-light, .7),
    text-disabled: rgba($oomox-light, .5),
    text2-disabled: rgba($oomox-light, .3),
    stroke: rgba($oomox-light, .3),
    stroke-disabled: rgba($oomox-light, .12),
    divider: rgba($oomox-light, .12),
    fill: rgba($oomox-light, .08),
    entry-fill: rgba($oomox-light, .04)
  )
);

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

//
// Main colors
//

$primary:                               if($variant == 'light', on(light, primary), on(dark, primary));

$background:                            %BG%;
$surface:                               %MATERIA_SURFACE%;
$switch-surface:                        %MATERIA_SURFACE%; // Special case for switches

$base:                                  %MATERIA_VIEW%;
$base-alt:                              %INACTIVE_MATERIA_VIEW%;

$error:                                 if($variant == 'light', on(light, error), on(dark, error));
$warning:                               %TERMINAL_COLOR11%;
$success:                               %TERMINAL_COLOR10%;
$visited:                               %TERMINAL_COLOR5%;

$os-background:                         %HDR_BG3%;
$tooltip:                               rgba(%HDR_BG%, 0.9);
$scrim:                                 rgba(black, %MATERIA_PANEL_OPACITY%);
$scrim-alt:                             rgba(black, 0.3);

$panel:                                 if($topbar == 'dark', %HDR_BG3%, $scrim);
$panel-solid:                           %HDR_BG3%; // for Unity panel which doesn't allow translucent colors

$titlebar:                              %HDR_BG%;
$titlebar-backdrop:                     %HDR_BG2%;
$titlebar-indicator:                    if($topbar == 'dark', currentColor, $primary);

//
// Overlay state colors
//

$overlay-hover-opacity:                 .08;
$overlay-focus-opacity:                 .08;
$overlay-focus-hover-opacity:           .16;
$overlay-active-opacity:                .12;
$overlay-checked-opacity:               .12;
$overlay-selected-opacity:              .24;

$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.12);
$overlay-selected:                      rgba($primary, %MATERIA_SELECTION_OPACITY%);

//
// “On” colors
//

$text:                                  %FG%;
$text2:                                 rgba(%FG%, 0.7);
$text-disabled:                         rgba(%FG%, 0.5);
$text2-disabled:                        rgba(%FG%, 0.3);
$stroke:                                rgba(%FG%, 0.3);
$stroke-disabled:                       rgba(%FG%, 0.12);
$divider:                               rgba(%FG%, 0.12);
$fill:                                  rgba(%FG%, 0.08);
$entry-fill:                            rgba(%FG%, 0.04);

$titlebar-text:                         %HDR_FG%;
$titlebar-text2:                        rgba(%HDR_FG%, 0.7);
$titlebar-text-disabled:                rgba(%HDR_FG%, 0.5);
$titlebar-text2-disabled:               rgba(%HDR_FG%, 0.3);
$titlebar-stroke:                       rgba(%HDR_FG%, 0.3);
$titlebar-stroke-disabled:              rgba(%HDR_FG%, 0.12);
$titlebar-divider:                      rgba(%HDR_FG%, 0.12);
$titlebar-fill:                         rgba(%HDR_FG%, 0.08);
$titlebar-entry-fill:                   rgba(%HDR_FG%, 0.04);

// workaround for GTK3 @placeholder_text_color which doesn't allow translucent colors
$placeholder_text_color:                mix(%FG%, $base, percentage(0.7));
