How to: Set text colour (color) for markers/messages

How to: Set text colour (color) for markers/messages

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

How to: Set text colour (color) for markers/messages

Post by LEDZ »

In-game text colour is subject to the following handle:

\255\numR\numG\numB

Caveats:
The numbers RGB cannot end with 0. Not sure exactly why but I assume its just a bug in the engine.
There is no support for Alpha; the \255 at the start is not an Alpha (at least it doesn't work as such) it is merely a character handle for colourizing.

So:

Code: Select all

Spring.Echo("\255\255\1\1This text is red")
Spring.Echo("\255\1\255\1This text is green")
Spring.Echo("\255\1\1\255This text is blue")
Spring.Echo("\255\1\255\127This text is Spring Green!")
And variations on this.

Below is a function that gets a player's colour from the game and converts it into the above handle for text.

Code: Select all

function colorNames(teamID) 
	nameColorR,nameColorG,nameColorB,nameColorA = spGetTeamColor(teamID)
	R255 = math.floor(nameColorR*255) 
	G255 = math.floor(nameColorG*255)
	B255 = math.floor(nameColorB*255)
	if ( R255%10 == 0) then
		R255 = R255+1
	end
	if( G255%10 == 0) then
		G255 = G255+1
	end
	if ( B255%10 == 0) then
		B255 = B255+1
	end
  return "\255"..string.char(R255)..string.char(G255)..string.char(B255) --works thanks to zwzsg
end 
This can be then appended to the beginning of a message to colour it:

Code: Select all

playerColor = colorNames(Spring.GetMyTeamID)
message = playerColor.."This text is the same colour as me!"
Spring.SendCommands("say "..string.format(message))
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: How to: Set text colour (color) for markers/messages

Post by CarRepairer »

To be more general, see the color2incolor and incolor2color functions here: http://code.google.com/p/zero-k/source/ ... s/util.lua
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: How to: Set text colour (color) for markers/messages

Post by Niobium »

CarRepairer wrote:To be more general, see the color2incolor and incolor2color functions here: http://code.google.com/p/zero-k/source/ ... s/util.lua
Wow, that code sucks. It has almost as many problems as lines.

For reference:

Code: Select all

function color2incolor(r,g,b,a)
    local colortable = {r,g,b,a}
    if type(r) == 'table' then
        colortable = r
    end
    local r,g,b,a = unpack(colortable)                      
    if r == 0 then r = 0.01 end
    if g == 0 then g = 0.01 end
    if b == 0 then b = 0.01 end
    --if a == 0 then a = 0.01 end --seems transparent is bad in label text
    a = 1
    
    local inColor = '\255\255\255\255'
    if r then
        inColor = string.char(a*255) .. string.char(r*255) ..  string.char(g*255) .. string.char(b*255)
    end
    return inColor
end
  • Takes an alpha argument when alpha isn't supported (The argument is even overriden in the code before being read)
  • Packs arguments into a table... then immediately unpacks them
  • Checks for r/g/b == 0 and replaces with 0.01 as a non-clear attempt to solve the null char problem.
  • Misses the fact that very small non-zero r/g/b values still give the null char (string.char(0.0001) == null char)
  • Doesn't handle other special characters, like char(10) which will give a new line.
  • nil-checks arguments at the very end of the function
  • Doesn't understand what the first character (255) means, thinks its alpha (Retarded)
  • Passes decimal values to string.char
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: How to: Set text colour (color) for markers/messages

Post by CarRepairer »

Nice. Code isn't retarded, people are. Stop following me around in threads just because I kicked your ass at mex detection. I offered an idea applicable to this topic and the code itself is secondary. You offer childish name calling and not a single solution to what you deem as retarded.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: How to: Set text colour (color) for markers/messages

Post by knorke »

Beside messages, unit description (in tooltip) can be colored too.
Not sure what else?
LEDZ
Posts: 66
Joined: 27 Jun 2011, 13:43

Re: How to: Set text colour (color) for markers/messages

Post by LEDZ »

Thanks CarRepairer, I believe I saw that very function somewhere about year ago when I first wrote something to colour text.
Niobium, your points are very true. A bit cutting though. It may be a bit wrong but you don't have to lambast anyone over it.
Knorke, I use it in a Unit Marker widget that I made. It makes the colour of the marker text the same colour as the unit owner:
Image
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: How to: Set text colour (color) for markers/messages

Post by Niobium »

LEDZ wrote:Niobium, your points are very true. A bit cutting though. It may be a bit wrong but you don't have to lambast anyone over it.
I don't know or care who wrote the code. My post says nothing about anyone, just the quality of the code.

I'm not interested in who is a good or bad at coding, or who is better at mex detection, just that objectively buggy code like the code linked doesn't get used in peoples projects because they assume its OK and copy-pasting is easy.
Post Reply

Return to “Lua Scripts”