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



All times are UTC + 1 hour


Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: 26 May 2012, 01:58 

Joined: 27 Jun 2011, 12:43
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:
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:
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:
playerColor = colorNames(Spring.GetMyTeamID)
message = playerColor.."This text is the same colour as me!"
Spring.SendCommands("say "..string.format(message))


Top
 Offline Profile  
 
PostPosted: 26 May 2012, 02:58 
Cursed Zero-K Developer
User avatar

Joined: 07 Nov 2007, 21:48
Location: Horse
To be more general, see the color2incolor and incolor2color functions here: http://code.google.com/p/zero-k/source/ ... s/util.lua


Top
 Offline Profile  
 
PostPosted: 26 May 2012, 05:33 
User avatar

Joined: 07 Dec 2008, 02:35
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:
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


Top
 Offline Profile  
 
PostPosted: 26 May 2012, 06:36 
Cursed Zero-K Developer
User avatar

Joined: 07 Nov 2007, 21:48
Location: Horse
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.


Top
 Offline Profile  
 
PostPosted: 26 May 2012, 09:40 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
Beside messages, unit description (in tooltip) can be colored too.
Not sure what else?


Top
 Offline Profile  
 
PostPosted: 26 May 2012, 12:14 

Joined: 27 Jun 2011, 12:43
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


Top
 Offline Profile  
 
PostPosted: 26 May 2012, 23:29 
User avatar

Joined: 07 Dec 2008, 02:35
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.


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.