// Colors generation mixins
// Used in conjunction with theme settings to generate color classes.


// Class wrapper
@mixin ansiCls($name, $color, $background: $color) {
	  .ansi#{$name} { color: $color; }
	.bgAnsi#{$name} { background: $background; }
}

// Default foreground/background
@mixin ansiDef($color: $vt-color, $background: $vt-bg) {
	@include ansiCls(Def,  $color, $background);
	@include ansiCls(DefR,
		if($background == transparent,
			lighten(theme(background), 20%),
			$background),
		lighten($color, 20%)
	);
}


// Ansi colors tables
// https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit

// 0-7:  standard colors (as in ESC [ 30–37 m)
// 8-15: high intensity colors (as in ESC [ 90–97 m)
@mixin ansiStd() {
	@for $i from 0 to 16 {
		@include ansiCls($i, nth(theme(colors), $i+1));
	}
}

// 16-231:  6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b
@mixin ansiColors() {
	$black: change-color( nth(theme(colors), 1),  $lightness: 2% );
	$red:   change-color( nth(theme(colors), 10), $lightness: 50%);
	$green: change-color( nth(theme(colors), 11), $lightness: 50%);
	$blue:  change-color( nth(theme(colors), 13), $lightness: 50%);
	$index: 15;

	// Sass `mix()` function is not suitable for this use!
	// Instead we defined `sum-colors()` in `function.scss`
	@for $r from 0 to 6 {
		$r-shade: sum-colors($black, $red, ($r/5));

		@for $g from 0 to 6 {
			$rg-shade: sum-colors($r-shade, $green, ($g/5));

			@for $b from 0 to 6 {
				$color: sum-colors($rg-shade, $blue, ($b/5));
				$index: $index +1; // (16 + $r*36 + $g*6 + $b);

				@include ansiCls($index, $color);
			}
		}
	}
}

// 232-255:  grayscale from black to white in 24 steps
@mixin ansiGray() {
	$black: nth(theme(colors), 1);
	$white: nth(theme(colors), 16);

	// 23 luma steps
	@for $l from 1 through 24 {
		$color: mix($white, $black, percentage($l/25));
		$index: (231 + $l);

		@include ansiCls($index, $color);
	}
}


// Aliases
@mixin ansi16() {
	@include ansiStd();
}

@mixin ansi256() {
	@include ansiStd();
	@include ansiColors();
	@include ansiGray();
}
