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

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