/**
 * Text Contrast
 * Compares contrast of a given color to the light/dark arguments and returns whichever is most "contrasty"
 */
@function text-contrast( $color ) {
	@if $color == null {
		@return null;
	}
	@else {
		$color-brightness: brightness($color);
		$light-text-brightness: brightness($light_text);
		$dark-text-brightness: brightness($dark_text);

		@if (abs($color-brightness - $light-text-brightness) > abs($color-brightness - $dark-text-brightness)) {
			@return $light_text;
		} @else {
			@return $dark_text;
		}
	}
}
@function brightness($color) {
	@return ((red($color) * .299) + (green($color) * .587) + (blue($color) * .114)) / 255 * 100%;
}
$ubtn-colors: ();
@if variable-exists( palette-primary_1 ) {
	$text-contrast-palette-primary_1: text-contrast( $palette-primary_1 );
	.color1-text-default { color: $text-contrast-palette-primary_1; }
	.color-1-text-contrast {
		color: $text-contrast-palette-primary_1 ! important;
	}
	.color-1-text-contrast-hover {
		&:hover,
		&:focus {
			color: $text-contrast-palette-primary_1 ! important;
		}
	}
	$ubtn-colors: append($ubtn-colors,('color-1' $palette-primary_1 $text-contrast-palette-primary_1));
	@if $ubtn-theme-color == 'palette-primary_1' {
		$ubtn-bgcolor: $palette-primary_1;
		$ubtn-font-color: $text-contrast-palette-primary_1;
	}
}
@if variable-exists( palette-primary_2  ) {
	$text-contrast-palette-primary_2: text-contrast( $palette-primary_2 );
	.color2-text-default { color: $text-contrast-palette-primary_2; }
	.color-2-text-contrast {
		color: $text-contrast-palette-primary_2 ! important;
	}
	.color-2-text-contrast-hover {
		&:hover,
		&:focus {
			color: $text-contrast-palette-primary_2 ! important;
		}
	}
	$ubtn-colors: append($ubtn-colors,('color-2' $palette-primary_2 $text-contrast-palette-primary_2));
	@if $ubtn-theme-color == 'palette-primary_2' {
		$ubtn-bgcolor: $palette-primary_2;
		$ubtn-font-color: $text-contrast-palette-primary_2;
	}
}
@if variable-exists( palette-primary_3 ) {
	$text-contrast-palette-primary_3: text-contrast( $palette-primary_3 );
	.color3-text-default { color: $text-contrast-palette-primary_3; }
	.color-3-text-contrast,
	.color-3-text-contrast-hover:hover {
		color: $text-contrast-palette-primary_3 ! important;
	}
	@if $text-contrast-palette-primary_3 != null {
		$ubtn-colors: append($ubtn-colors,('color-3' $palette-primary_3 $text-contrast-palette-primary_3));
		@if $ubtn-theme-color == 'palette-primary_3' {
			$ubtn-bgcolor: $palette-primary_3;
			$ubtn-font-color: $text-contrast-palette-primary_3;
		}
	}
}
@if variable-exists( palette-primary_4 ) {
	$text-contrast-palette-primary_4: text-contrast( $palette-primary_4 );
	.color4-text-default { color: $text-contrast-palette-primary_4; }
	.color-4-text-contrast,
	.color-4-text-contrast-hover:hover {
		color: $text-contrast-palette-primary_4 ! important;
	}
	@if $text-contrast-palette-primary_4 != null {
		$ubtn-colors: append($ubtn-colors,('color-4' $palette-primary_4 $text-contrast-palette-primary_4));
		@if $ubtn-theme-color == 'palette-primary_4' {
			$ubtn-bgcolor: $palette-primary_4;
			$ubtn-font-color: $text-contrast-palette-primary_4;
		}
	}
}
@if variable-exists( palette-primary_5 ) {
	$text-contrast-palette-primary_5: text-contrast( $palette-primary_5 );
	.color5-text-default { color: $text-contrast-palette-primary_5; }
	.color-5-text-contrast,
	.color-5-text-contrast-hover:hover {
		color: $text-contrast-palette-primary_5 ! important;
	}
	@if $text-contrast-palette-primary_5 != null {
		$ubtn-colors: append($ubtn-colors,('color-5' $palette-primary_5 $text-contrast-palette-primary_5));
		@if $ubtn-theme-color == 'palette-primary_5' {
			$ubtn-bgcolor: $palette-primary_5;
			$ubtn-font-color: $text-contrast-palette-primary_5;
		}
	}
}
@if variable-exists( palette-primary-neutral-color ) {
	$text-contrast-palette-primary-neutral-color: text-contrast( $palette-primary-neutral-color );
	.color-neutral-text-default { color: $text-contrast-palette-primary-neutral-color; }
	.color-neutral-text-contrast,
	.color-neutral-text-contrast-hover:hover {
		color: $text-contrast-palette-primary-neutral-color ! important;
	}
	@if $text-contrast-palette-primary-neutral-color != null {
		$ubtn-colors: append($ubtn-colors,('neutral-color' $palette-primary-neutral-color $text-contrast-palette-primary-neutral-color));
		@if $ubtn-theme-color == 'palette-primary-neutral-color' {
			$ubtn-bgcolor: $palette-primary-neutral-color;
			$ubtn-font-color: $text-contrast-palette-primary-neutral-color;
		}
	}
}

/*create single css classes to apply palettes on universal elements*/
$names: color background background-color border-color;
@for $i from 1 through length( $names ) {
	@for $color from 1 through length( $colors ) {
		.color#{$color}-#{nth( $names, $i )} { #{nth( $names, $i )}: #{nth($colors, $color)} ! important; }
		.color#{$color}-#{nth($names, $i)}-hover { &:focus, &:hover { #{nth( $names, $i )}: #{nth($colors, $color)} ! important; } }
	}
	@if variable-exists( palette-primary-neutral-color ) {
		.color-neutral-#{nth($names, $i)} { #{nth( $names, $i )}: $palette-primary-neutral-color !important; }
	}
}
/** Adding minimal color definitions for default breadcrumbs **/
.breadcrumb li a.filler {
	background: $palette-primary_2;
	color: $text-contrast-palette-primary_2;
	&:after {
		border-left-color: $palette-primary_2;
	}
}
.breadcrumb li a:not(.filler) {
	color: $text-contrast-palette-primary_1;
	&:after {
		border-left-color: $palette-primary_1 !important;
	}
	&:hover, &:focus {
		background: $palette-primary_2;
		color: $text-contrast-palette-primary_2;
		&:after {
			border-left-color: $palette-primary_2 !important;
			color: $text-contrast-palette-primary_2;
		}
	}
}

/** WooCommerce overrides **/
.woocommerce div.product {
	.stock,
	span.price,
	p.price {
		color: $palette-primary_1;
	}
}

/** Gallery overrides **/
.wc-image-links,
.wc-gallery-captions-show,
.wc-gallery-captions-onhover,
.wc-gallery-captions-showon {
	.gallery-caption {
		background-color: $palette-primary_3;
		color: $text-contrast-palette-primary_3;
		> * {
			color: $text-contrast-palette-primary_3;
		}
	}
}


/*
* Base Colors
*
* Create colors for buttons
* (.button-primary, .button-secondary, etc.)
*/
@if variable-exists( ubtn-colors ) {
	@each $ubtn-color in $ubtn-colors {
		$ubtn-name: nth($ubtn-color, 1);
		$ubtn-background: nth($ubtn-color, 2);
		$ubtn-color: nth($ubtn-color, 3);

		// Crete class for .button-primary
		// and legacy flat .button-primary-flat
		.btn-#{$ubtn-name},
		.btn-#{$ubtn-name}-flat {
			background-color: $ubtn-background;
			border-color: $ubtn-background;
			color: $ubtn-color !important;

			&:visited {
				color: $ubtn-color;
			}

			&:hover,
			&:focus {
				background-color: lighten($ubtn-background, 10%);
				border-color: lighten($ubtn-background, 10%);
				color: $ubtn-color !important;
			}

			&:active,
			&.active,
			&.is-active {
				background-color: desaturate($ubtn-background, 10%);
				border-color: desaturate($ubtn-background, 10%);
				color: darken($ubtn-background, 10%) !important;
			}
		}
	}
}
