Here are a couple of functions I've made in Lua, I hope some come in handy:
ansicolor
For use in AnsiNote, this function returns the ANSI background and foreground colors for you, all you need to specify is the number(s) (0-15).
Example: AnsiNote (ansicolor (11, 2), "Yellow on green text")
ansicolor = function (fore, back)
local bold = false
local fore = tonumber (fore) or 7
if fore >= 0 and fore <= 7 then
fore = fore + 30
elseif fore >= 8 and fore <= 15 then
fore = fore + 22
bold = true
else
fore = 37
end -- if
local back = tonumber (back) or 0
if back > 0 and back <= 7 then
back = back + 40
else
back = 40
end -- if
if not bold then
return ANSI (0, fore, back)
else
return ANSI (0, fore, 1, back)
end -- if
end -- func
colorchart
This function display a color chart for ease in deciding on a fore/background combination. Just call it with colorchart (). You can also include two parameters, the first being 'fore' or 'back' and the second the specific fore or back color you want to limit it by, and it will display all combinations for that specific color. Requires ansicolor.
colorchart = function (x, y)
if x == "fore" then
if y and y >= 0 and y <= 15 then
local fore = y
for back = 0, 7 do
AnsiNote (ansicolor (fore, back), string.format (" Fore: %-2s Back: %s ", fore, back))
end -- for
else
for fore = 0, 15 do for back = 0, 7 do
AnsiNote (ansicolor (fore, back), string.format (" Fore: %-2s Back: %s ", fore, back))
end end -- for
end -- if
else
if y and y >= 0 and y <= 7 then
local back = tonumber(y)
for fore = 0, 15 do
AnsiNote (ansicolor (fore, back), string.format (" Fore: %-2s Back: %s ", fore, back))
end -- for
else
for back = 0, 7 do for fore = 0, 15 do
AnsiNote (ansicolor (fore, back), string.format (" Fore: %-2s Back: %s ", fore, back))
end end -- for
end -- if
end -- if
end -- func
getcolor
This function gets the current rgb version of your ANSI color from your settings. Call it with getcolor (#) where # is between 0 and 15. Returns false if you choose something outside that range.
getcolor = function (x)
local func
if x >= 0 and x <= 7 then
x = x + 1
func = GetNormalColour
elseif x >= 8 and x <= 15 then
x = x - 7
func = GetBoldColour
else
func = function () return false end
end
return func (x)
end -- func
string.proper
Like string.upper and string.lower, this returns the proper case (first is capitalized, rest is lowercase) version of a string.
string.proper = function (x)
return string.upper (string.sub(x, 1, 1)) .. string.lower (string.sub(x, 2))
end -- func
|