Functions

Posted by Fadedparadox on Thu 25 Jun 2009 05:16 PM — 4 posts, 17,880 views.

USA #0
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

Amended on Thu 25 Jun 2009 05:23 PM by Fadedparadox
USA #1
Oh and for the colorchart, colorchart ("fore") will sort it by foreground color instead of the default, background.
Australia Forum Administrator #2

That looks nice, thanks for releasing it.

The colorchar output looks like this:

This lists all the combinations of the ANSI colours as foreground and background combinations (this would be useful for MUD admins deciding on a colour scheme).


By the way, if you want to see what all the named RGB colours look like, you can type: /Debug 'colours'

Amended on Thu 25 Jun 2009 09:11 PM by Nick Gammon
USA #3
I mostly use colorchart for deciding on output colors, since it's hard for me to picture what color combinations go well together.

The getcolor function I use to make matches that I can't use regex for, but are colored specifically. I just test to textcolour and backcolour from TriggerStyleRuns.