#!/usr/bin/awk -f
#
# adapted from june mcenroes scheme.c
# source: https://codeberg.org/sewn/home/src/branch/master/bin/scheme

function abs(v) { return v < 0 ? -v : v; }
function min(a, b) { return a < b ? a : b; }

function rgb(i) {
	r = 0; g = 0; b = 0;
	h = c[i,"h"]; s = c[i,"s"]; v = c[i,"v"]

	n = v * s
	h = h / 60.0
	x = n * (1.0 - abs((h - (2.0 * int(h / 2.0))) - 1.0))
	m = v - n
	r = m; g = m; b = m
	if      (h <= 1.0) { r += n; g += x; }
	else if (h <= 2.0) { r += x; g += n; }
	else if (h <= 3.0) { g += n; b += x; }
	else if (h <= 4.0) { g += x; b += n; }
	else if (h <= 5.0) { r += x; b += n; }
	else if (h <= 6.0) { r += n; b += x; }

	r *= 255.0
	g *= 255.0
	b *= 255.0
}

function rgbhex(i) {
	rgb(i); return sprintf("%02hhX%02hhX%02hhX",
		r, g, b) 
}

function hsv(i, h, s, v) {
	c[i,"h"] = h; c[i,"s"] = s; c[i,"v"] = v;
}

function hsl(i) {
	h = c[i,"h"]; s = c[i,"s"]; v = c[i,"v"]
	l = v * (1 - s / 2)
	s = (l == 0 || l == 1) ? 0 : (v - l) / min(l, 1 - l)
}

function p(i, j, hd, sf, vf) {
	c[i,"h"] = (c[j,"h"] + hd) % 360;
	c[i,"s"] = min(c[j,"s"] * sf, 1.0);
	c[i,"v"] = min(c[j,"v"] * vf, 1.0);
}

BEGIN {
	len=21
	# i think i can call this "dull dawn"
	c[0,"h"] = 0.0;  c[0,"s"] = 0.0;  c[0,"v"] = 0.0; 
	names[0] = "black";  hsv(0, 44.0,  0.27, 0.10);  # embraced soil
	names[1] = "red";    hsv(1, 12.0,  0.7, 0.58);  # self-mutilation
	names[2] = "green";  hsv(2, 48.0,  0.8, 0.74);  # decomposition
	names[3] = "yellow"; hsv(3, 41.0,  0.9, 0.89);  # depersonalization
	names[4] = "blue";   hsv(4, 198.0, 0.4, 0.48);  # stoic suppression
	names[5] = "purple"; hsv(5, 2.0,   0.4, 0.63);  # ephemeral infatuation
	names[6] = "cyan";   hsv(6, 111.0, 0.3, 0.61);  # euphoric drowning
	names[7] = "white";  hsv(7, 44.0,  0.3, 0.72); # faithful purification

	names[8]  = "bri_black"; p(8,  0, 0.0, 1.0, 1.7); # bg1
	names[15] = "bri_white"; p(15, 7, 0.0, 1.0, 1.7);
	for (i = 1; i < 7; i++) {
		names[i + 8] = "bri_" names[i]
		p(i + 8, i, 0.0, 1.0, 1.2); # bright i..
	}
	names[16] = "bg";    hsv(16, 44.0,  0.3,  0.07); # code in non tty ctx
	names[17] = "fg";    hsv(17, 44.0,  0.3,  0.96);
	names[18] = "amber"; p(18, 1,  16,  1.16, 1.4);

	names[19] = "bgn"; p(19, 16, 0.0,  1.0, 0.8);
	names[20] = "bg2"; p(20, 0,  0.0,  1.0, 2.8);
	names[21] = "bg3"; p(21, 0,  0.0,  1.0, 4.6);

	if (ARGV[1] == "foot") {
		print "[colors-dark]"
		for (i = 0; i <= 7; i++) {
			printf "regular%d=%s\n", i, rgbhex(i)
		}
		for (i = 8; i <= 15; i++) {
			printf "bright%d=%s\n", i - 8, rgbhex(i)
		}
		printf "background=%s\n", rgbhex(16)
		printf "foreground=%s\n", rgbhex(17)
		printf "selection-background=%s\n", rgbhex(18)
	} else if (ARGV[1] == "css") {
		printf ":root {\n"
		for (i = 0; i <= len; i++) {
			hsl(i); printf "\t--col_%s: hsl(%.1f, %f%%, %f%%);\n",
				names[i], h, s * 100, l * 100;
		}
		printf "}\n";
	} else if (ARGV[1] == "enum") {
		for (i = 0; i <= 18; i++) {
		    print i " " rgb(c[i,"h"], c[i,"s"], c[i,"v"])
		}
	} else if (ARGV[1] == "ppm") {
		cols = 8;
		rows = int((len + cols - 1) / cols);
		width = 64 * cols;
		height = 64 * rows;

		printf "P3\n%d %d\n255\n", width, height;

		t[0,"r"] = 0; t[0,"g"] = 0; t[0,"b"] = 0; 
		for (i = 0; i < len; i++) {
			rgb(i);
			t[i,"r"] = r; t[i,"g"] = g; t[i,"b"] = b;
		}

		for (y = 0; y < height; ++y) {
			row_idx = int(y / 64);
			for (x = 0; x < width; ++x) {
				col_idx = int(x / 64);
				i = row_idx * cols + col_idx;
				printf "%d %d %d\n", t[i,"r"], t[i,"g"], t[i,"b"];
			}
		}
	} else {
		printf "usage: scheme [foot|css|enum]\n"
		exit -1
	}
}
