Skip to content

Instantly share code, notes, and snippets.

@uraraurara
Created October 28, 2012 09:57
Show Gist options
  • Save uraraurara/3968213 to your computer and use it in GitHub Desktop.
Save uraraurara/3968213 to your computer and use it in GitHub Desktop.
a port of 256colors.pl in lua see: xterm's folder
opt_h, opt_q, opt_r = false, false, false
function map_cube(v)
if opt_r then return 5 - v end
return v
end
function map_gray(v)
if opt_r then return 23 - v end
return v
end
if opt_q then io.write("\x1b]4") end
-- colors 16-231 are a 6x6x6 color cube
for red = 0, 5 do
for green = 0, 5 do
for blue = 0, 5 do
if not opt_q then io.write("\x1b]4") end
io.write(string.format(";%d;rgb:%2.2x/%2.2x/%2.2x",
16 + (map_cube(red) * 36 ) + (map_cube(green) * 6) + map_cube(blue),
not red == 0 and red * 40 + 55 or 0,
not green == 0 and green * 40 + 55 or 0,
not blue == 0 and blue * 40 + 55 or 0))
if not opt_q then io.write("\1xb\\") end
end
end
end
-- colors 232-255 are a grayscale ramp, intentionally leaving out
-- black and white
for gray = 0, 23 do
level = map_gray(gray) * 10 + 8
if opt_q then io.write("\x1b]4") end
io.write(string.format(";%d;rgb:%2.2x/%2.2x/%2.2x",
232 + gray, level, level, level))
if opt_q then io.write("\x1b\\") end
end
if opt_q then io.write("\x1b\\") end
-- display the colors
-- first the system ones:
io.write("System colors:\n")
for color = 0, 7 do
io.write(string.format("\x1b[48;5;%sm ", color))
end
io.write("\x1b[0m\n")
for color = 8, 15 do
io.write(string.format("\x1b[48;5;%sm ", color))
end
io.write("\x1b[0m\n\n")
-- now the color cube
io.write("Color cube, 6x6x6:\n")
for green = 0, 5 do
for red = 0, 5 do
for blue = 0, 5 do
color = 16 + red * 36 + green * 6 + blue
io.write(string.format("\x1b[48;5;%sm ", color))
end
io.write("\x1b[0m ")
end
io.write("\n")
end
-- now the grayscale ramp
print("Grayscale ramp:")
for color = 232, 255 do
io.write(string.format("\x1b[48;5;%sm ", color))
end
io.write("\x1b[0m\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment