Colour in Echo text.

Colour in Echo text.

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Colour in Echo text.

Post by LEDZ »

So I've been learning Lua recently, its been fine for the most part, but now this:

Code: Select all

  nameColorizer = string.format("\255\%s\%s\%s", R255, G255, B255)
  messageColorizer = string.format("\255\255\255\255")
  message1 = nameColorizer .. myPlayerName .. messageColorizer .. " has now sent " .. DM*10 .. " of his machines to metalhell"
  Spring.Echo(message1)
*These are just the important parts of my code.
R255 etc. are the player's colours in 0-255 (instead of 0-1 as given by Spring.teamColor)
**Originally I tried other methods, with the same result:

Code: Select all

nameColorizer = "\255\" .. R255 .. "\" .. G255.. "\" .. B255
This is my latest attempt. It works as you would expect except the colour of the player name doesn't work properly.
As far as I can tell, it doesn't interpret the nameColorizer string in the same way as it does for messageColorizer (which makes the rest of the message white). Any ideas on how to get this to work?
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Colour in Echo text.

Post by smoth »

Code: Select all

local colors = {
	--factions
	EFFColor		= {0.80, 0.80, 1.00, 1},
	EFFTrimColor	= {0.80, 0.80, 1.00, 1},

	ZEONColor		= {0.95, 1.00, 0.70, 1},
	ZEONTrimColor	= {1.00, 0.80, 0.60, 1},

	neutralColor		= {1.00, 1.00, 1.00, 1},
	neutralTrimColor	= {1.00, 1.00, 1.00, 1},
	
	--resource colors
	exoColor	= {0.78, 0.40, 0.78, 1},
	refColor	= {1.00, 1.00, 0.30, 1},
	matColor	= {1.00, 0.30, 0.30, 1},
	nrgColor	= {0.30, 0.88, 1.00, 1},
	--damage class crap
	epColor		= {1.00, 0.90, 0.03, 1},
	bmtColor	= {1.00, 0.03, 1.00, 1},
	ltColor		= {0.58, 0.58, 1.00, 1},
	mdColor		= {0.20, 0.90, 0.20, 1},
	--only armor classes
	hvColor		= {0.50, 0.50, 0.50, 1},
	blColor		= {1.00, 0.10, 0.10, 1},
	
	white		= {1.00, 1.00, 1.00, 1},
	black		= {0.00, 0.00, 0.00, 1},
	red			= {1.00, 0.00, 0.00, 1},
	blue		= {0.00, 0.00, 1.00, 1},
	green		= {0.00, 1.00, 0.00, 1},
	yellow		= {1.00, 1.00, 0.00, 1},
	purple		= {0.00, 1.00, 1.00, 1},
}

local colorSets	= {}
for name,vals in pairs(colors) do
	colorSets[name]	= {}
	colorSets[name].argb = string.char(math.floor(vals[4]*255),math.floor(vals[1]*255),
										math.floor(vals[2]*255),math.floor(vals[3]*255))
	colorSets[name].rgba = colors[name]
end

return colorSets
look at the .argb part.. I gotta go to work
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: Colour in Echo text.

Post by LEDZ »

Thanks for the quick reply.
I'm not sure about it though.
The math.floor parts are to make sure the value given is an integer, yes?
If so that's not a problem with my code.
The string.char: doesn't that convert the numbers into their representative symbols? If so I don't see how this would help.
Could you explain a little please?
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Colour in Echo text.

Post by smoth »

I could not explain as I was leaving for work.

I am in a hurry... Between tasks so..

The math stuff is for converting my 0-1.0 values part...

Important part

Order of colors

ARGB vs RGBA

ARGB for text
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Colour in Echo text.

Post by smoth »

No \ just numbers

Val="a\r\g\b"
Not (Val)<- this is single string

Is (a,r,g,b) <- several values <- correct way
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Colour in Echo text.

Post by zwzsg »

Code: Select all

nameColorizer = string.format("\255\%s\%s\%s", R255, G255, B255)
It is Lua, not C

Code: Select all

nameColorizer = "\255\" .. R255 .. "\" .. G255.. "\" .. B255
Almost, but not quite. Try with:

Code: Select all

nameColorizer="\255"..string.char(R255)..string.char(G255)..string.char(B255)
Just tried and yep, it works that way


\ is an escape character. It means that it does not put the character '\' into the string, but instead \ is used to introduce character that would be hard to type otherwise. \ followed by a number N is the Nth character, or if you prefer, the character that corresponds to the byte value of N. For instance "\065" is "A".


The default behavior of Lua, when you concatenate a string and a number with .. is to transform the number into the string of its decimal representation. So we use string.char(N) to tell Lua we really want the character corresponding to a byte value of N.


smoth wrote:ARGB for text
There is no Alpha for text. The 255 is here to introduce the colors, it is not an alpha value.
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: Colour in Echo text.

Post by LEDZ »

Ahh thats great. I tried string.char in one attempt but I misunderstood. I knew that it took a number as a key for a particular character, but I didn't consider that that is exactly what "\number" was doing as well.
It seems to be working fine now. Thanks.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Colour in Echo text.

Post by jK »

smoth wrote:I could not explain as I was leaving for work.

I am in a hurry... Between tasks so..

The math stuff is for converting my 0-1.0 values part...

Important part

Order of colors

ARGB vs RGBA

ARGB for text
There is no alpha in inlined colorcodes. The \255 is a special char, nothing else.
Btw there is a colorreset code, too. -> "\255\255\001\001 red \255\001\001\255 blue \b red again" -> red blue red again

so the codes are:
"\255\rrr\ggg\bbb"
&
"\b" equal to "\008"
Post Reply

Return to “Lua Scripts”