util  termcolors.lua at [b4fe00021c]

File termcolors.lua artifact ccbbe9b5fc part of check-in b4fe00021c


-- [ʞ] termcolors.lua
--  ~ lexi hale <lexi@hale.su>
--  © AGPLv3
--  ? print grids comparing truecolor output to 256-color output 

function conv(a)
	return 16 + math.floor(a.r*5)*36 + math.floor(a.g*5)*6 + math.floor(a.b*5)
end
local S = 32
local M = 33 * 4
local grid = {}

local X,Y = 0,0
for R=0,S do for G=0,S do
	for B=0,S do
		local r,g,b = R/S, G/S, B/S
		grid[Y*M + X] = {r=r,g=g,b=b}
		X = X + 1
		if X == M then
			X = 0
			Y = Y + 1
		end
	end
end end

local function show(setcolor)
	for y=0,#grid/M - 1,2 do
	for x=0,M - 1 do
		local top,bot = grid[y*M + x], grid[(y+1)*M + x]
		setcolor(top, false)
		setcolor(bot, true)
		io.write(string.format('▀'))
	end print('\27[m') end
end

print "-- using 256-color escapes"
show(function(clr,bottom)
	local tpl
	if clr then
		if bottom then tpl = '\27[48;5;%um'
		else tpl = '\27[38;5;%um' end
		io.write(string.format(tpl, conv(clr)))
	else
		if bottom
			then io.write('\27[49m')
			else io.write('\27[39m')
		end
	end
end)

print "-- using truecolor escapes"
-- if false then
show(function(clr,bottom)
	local tpl
	if clr then
		if bottom then tpl = '\27[48;2;%u;%u;%um'
		else tpl = '\27[38;2;%u;%u;%um' end
		io.write(string.format(tpl,
			math.floor(clr.r*0xff),
			math.floor(clr.g*0xff),
			math.floor(clr.b*0xff)))
	else
		if bottom
			then io.write('\27[49m')
			else io.write('\27[39m')
		end
	end
end)
-- end

-- for R=0,S do for G=0,S do for B=0,S do
-- 	local r,g,b = R/S, G/S, B/S
-- 	io.write(string.format('\27[48;2;%u;%u;%um   ', math.ceil(r*0xff), math.ceil(g*0xff), math.ceil(b*0xff)))
-- end print('\27[m') end end
--
-- print()