Lua Styles

Posted by Worstje on Mon 06 Nov 2006 04:23 PM — 3 posts, 10,036 views.

Netherlands #0
Okay, so I am using Lua because I needed the style information for a certain line. But I ran into a brick wall (that right now, I'm walking around).

Because I have an application that can process ansi codes (but not #RRGGBB/name codes) and display text based on it, I need to get the original ansi codes.

Right now, I have a table with the colours as RGBColourToName() outputs them and the ansi numbers. Like this, I make a 'new' ansi code.

This is really slow, sadly. I've tried sending sample strings to my application with UdpSend which do get sent rather quickly, which leads me to believe it is the Ansi(RGBColourToName(), RGBColourToName) call I've built. It also has the disadvantage that it won't work anymore after someone changes the default colors.

Would it be possible to have original ansi colornumbers+bold in the styles parameter, or a AnsiColor(v) that outputs the appropriate ansi code?

Edit: my code right now is as follows:

for _, v in ipairs (styles) do
			s = s .. AnsiSequence(RGBColourToName(v.textcolour),
				RGBColourToName(v.backcolour)) .. v.text
			-- Tell(RGBColourToName (v.textcolour) .. ",")
			-- ColourTell (RGBColourToName (v.textcolour), 
			-- 	RGBColourToName (v.backcolour), 
			-- 	v.text)
		end -- for each style run

Amended on Mon 06 Nov 2006 04:27 PM by Worstje
Australia Forum Administrator #1
Quote:

This is really slow, sadly.


It shouldn't be slow. This is a straight table lookup. Each colour has a direct mapping to an ANSI code. You haven't shown your function that actually does the work, and which you say is slow.

Here is my implementation of such a thing. It initially builds up two tables (normal, hilite) which map the RGB colour to the ANSI number (0 to 7) required to build up that colour. I use the actual RGB codes reported by GetNormalColour and GetBoldColour so it will work regardless of what colours you are seeing on your screen.

Then for the foreground colours it adds 30, so black is 30, red is 31, and so on. If it can't find it in the "normal" table it tries the hilite table, and inserts an ANSI "bold" code (ANSI (1)).

Then it does the same exercise for the background colour, except adding in the bold, as you don't have bold background colours.

However be warned that this will only work for the straight ANSI colours, and things like ColourNote can display colours that simply don't have an ANSI representation.


normal, hilite = {}, {}

-- build table of normal, hilight colours
for i = 1, 8 do
    normal [GetNormalColour (i)] = i - 1 -- normal
    hilite [GetBoldColour (i)] =  i - 1  -- highlight
end -- for 


function AnsiSequence (fore, back)
  local result = ANSI(0) -- reset 

  -- foreground colour
  if normal [fore] then
    result = result .. ANSI(normal [fore] + 30)
  elseif hilite [fore] then
    result = result .. ANSI(1) .. ANSI(hilite [fore] + 30)
  end

 -- background colour
  if normal [back] then
    result = result .. ANSI(normal [back] + 40)
  elseif hilite [back] then
    result = result .. ANSI(hilite [back] + 40)
  end

  return result
end -- AnsiSequence 

-- test

print ( AnsiSequence (12632256, 0))


Netherlands #2
Right, a bit silly of me not to give the entire code. Sorry.


AnsiTable = {
	black = 0,
	maroon = 1,
	green = 2,
	olive = 3,
	navy = 4,
	purple = 5,
	teal = 6,
	silver = 7,
	gray = 8,
	red = 9,
	lime = 10,
	yellow = 11,
	blue = 12,
	magenta = 13,
	cyan = 14,
	white = 15
	}

function AnsiSequence(fore, back)
	ifore = AnsiTable[fore];
	iback = AnsiTable[back];
	ibold = "0"
	
	if (ifore > 7) then
		ifore = ifore - 8
		ibold = "1"
	end -- ifore > 7
	
	if (iback > 7) then
		-- A bold background color should not be possible. Just a check for sanity.
		iback = iback - 8
	end -- iback > 7
	
	return "\27[" .. ibold .. ";" .. (ifore+30) .. ";" .. (iback+40) .. "m"
end -- function


I haven't tried using the builtin ANSI function yet. I forgot about it since I felt 'ESC[X;Y;Zm' should be faster than 'ESC[XmESC[ymESC[zm'.

I had overlooked GetNormalColour and GetBoldColour in the help. I'm not sure how I managed that since I looked through there three times trying to find something like them. This solves my problem with non-default settings, thanks! It's still a shame that mush converts ansi to RGB, and I convert back from RGB to ansi. I'm a bit of a perfectionist like that, though. :)

Doing some more thinking, I think I know what else may be slowing things down. Mush parses each line and sends it to my application using udp send. In the case of 22 lines, that becomes 22 UdpSend commands, each gagging the original line. It might be faster to use OnPluginPacketReceived, but the biggest hurdle there is that that many lines span like three packets. I'll look into it, though.