Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Suggestions ➜ Lua Styles

Lua Styles

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Worstje   Netherlands  (899 posts)  Bio
Date Mon 06 Nov 2006 04:23 PM (UTC)

Amended on Mon 06 Nov 2006 04:27 PM (UTC) by Worstje

Message
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

Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 06 Nov 2006 08:20 PM (UTC)
Message
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))



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #2 on Tue 07 Nov 2006 02:55 PM (UTC)
Message
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.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


9,701 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.