• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet] [Lacking] PlayerColor Utils

A very simple library for easier playercolor managing ;)
I saw many systems using a bunch of string arrays over and over again
so I thought this library might be useful.
It also features retrieving the RGB values from a color string.

JASS:
library PlayerColorUtils
// PlayerColor Utils by The_Witcher
//
//  Just a bunch of functions for easier   player <-> color string    handling
//
//  Feel free to use!
//
//  Functions:
//      function GetPlayerColorString takes player p returns string
//      function GetPlayerColorStringById takes integer i returns string
//
//      function GetPlayerColorRed takes player p returns integer
//      function GetPlayerColorGreen takes player p returns integer
//      function GetPlayerColorBlue takes player p returns integer
//
//      function GetPlayerColorRedPercent takes player p returns integer
//      function GetPlayerColorGreenPercent takes player p returns integer
//      function GetPlayerColorBluePercent takes player p returns integer
//
//      function ColorStringGetRed takes string s returns integer
//      function ColorStringGetGreen takes string s returns integer
//      function ColorStringGetBlue takes string s returns integer
//
//  Variables:
//      public string array colors
//
//==============================================================================   
//========================= System Code ========================================
//==============================================================================

    globals
        public string array colors[12]
    endglobals
    
    private function ColorStringGetColor takes string s returns integer
        local string charMapL = "0123456789abcdef"
        local string charMapU = "0123456789ABCDEF"
        local string c1 = SubString(s, 0, 1)
        local string c2 = SubString(s, 1, 2)
        local integer x = 0
        local integer y = 0
        loop
            exitwhen x == 15 or SubString(charMapL, x, x + 1) == c1 or SubString(charMapU, x, x + 1) == c1
            set x = x + 1
        endloop
        loop
            exitwhen y == 15 or SubString(charMapL, y, y + 1) == c2 or SubString(charMapU, y, y + 1) == c2
            set y = y + 1
        endloop
        return x * 16 + y
    endfunction
    
    // Get the red, green and blue values from a color string
    
    function ColorStringGetRed takes string s returns integer
        return ColorStringGetColor(SubString(s, 4, 6))
    endfunction    
    
    function ColorStringGetGreen takes string s returns integer
        return ColorStringGetColor(SubString(s, 6, 8))
    endfunction    
    
    function ColorStringGetBlue takes string s returns integer
        return ColorStringGetColor(SubString(s, 8, 10))
    endfunction
    
    // Get the color string for a specific player

    function GetPlayerColorString takes player p returns string
        return colors[GetPlayerId(p)]
    endfunction    
    
    function GetPlayerColorStringById takes integer i returns string
        if i < 0 or i > 11 then
            return "|cffFFFFFF"
        endif
        return colors[i]
    endfunction
    
    // Get the red, green and blue values from a playercolor

    function GetPlayerColorRed takes player p returns integer
        return ColorStringGetColor(SubString(colors[GetPlayerId(p)], 4, 6))
    endfunction
    
    function GetPlayerColorGreen takes player p returns integer
        return ColorStringGetColor(SubString(colors[GetPlayerId(p)], 6, 8))
    endfunction
    
    function GetPlayerColorBlue takes player p returns integer
        return ColorStringGetColor(SubString(colors[GetPlayerId(p)], 8, 10))
    endfunction
    
    // Get the red, green and blue values from a playercolor in percent
    
    function GetPlayerColorRedPercent takes player p returns real
        return ColorStringGetColor(SubString(colors[GetPlayerId(p)], 4, 6)) / 255.0
    endfunction
    
    function GetPlayerColorGreenPercent takes player p returns real
        return ColorStringGetColor(SubString(colors[GetPlayerId(p)], 6, 8)) / 255.0
    endfunction
    
    function GetPlayerColorBluePercent takes player p returns real
        return ColorStringGetColor(SubString(colors[GetPlayerId(p)], 8, 10)) / 255.0
    endfunction
    
    private module Init
        private static method onInit takes nothing returns nothing
            set colors[0] = "|cffFF0202"
            set colors[1] = "|cff0041FF"
            set colors[2] = "|cff1BE5B8"
            set colors[3] = "|cff530080"
            set colors[4] = "|cffFFFC00"
            set colors[5] = "|cffFE890D"
            set colors[6] = "|cff1FBF00"
            set colors[7] = "|cffE45AAF"
            set colors[8] = "|cff949596"
            set colors[9] = "|cff7DBEF1"
            set colors[10] = "|cff0F6145"
            set colors[11] = "|cff4D2903"
        endmethod
    endmodule

    private struct init extends array
        implement Init
    endstruct

endlibrary

v.1.0: Initial Release
v.1.1: Better safety for GetPlayerColorStringById thanks to Magtheridon
v.1.2: Module initializer thanks to bills
 
Last edited:
Level 2
Joined
Nov 14, 2010
Messages
8
suggestion...

JASS:
globals
	private string array colors
endglobals

private module Init
	private static method onInit takes nothing returns nothing
		set colors[0] = "|cffFF0202"
		set colors[1] = "|cff0041FF"
		set colors[2] = "|cff1BE5B8"
		set colors[3] = "|cff530080"
		set colors[4] = "|cffFFFC00"
		set colors[5] = "|cffFE890D"
		set colors[6] = "|cff1FBF00"
		set colors[7] = "|cffE45AAF"
		set colors[8] = "|cff949596"
		set colors[9] = "|cff7DBEF1"
		set colors[10] = "|cff0F6145"
		set colors[11] = "|cff4D2903"
	endmethod
endmodule

private struct init extends array
	implement Init
endstruct
 
^ You forgot this:

JASS:
function GetColorString takes integer playerId returns string
    return colors[playerId]
endfunction

No i didn't^^ even with safety integration as we discussed 2 posts earlier...
JASS:
    function GetPlayerColorStringById takes integer i returns string
        if i < 0 or i > 11 then
            return "|cffFFFFFF"
        endif
        return colors[i]
    endfunction
updated with module initializer, thanks to bills ;)
 
Level 2
Joined
Nov 14, 2010
Messages
8
JASS:
globals
	public string array colors[12]
endglobals

why public? This is dangerous.
 
Nah, users aren't that stupid.

But I have to agree with you on that one. Theoretically speaking, it would be better if this system would a struct with readonly members like this:

JASS:
library PlayerColorUtils

    private module Init
        private static method onInit takes nothing returns nothing
            set colors[0] = "|cffFF0202"
            set colors[1] = "|cff0041FF"
            set colors[2] = "|cff1BE5B8"
            set colors[3] = "|cff530080"
            set colors[4] = "|cffFFFC00"
            set colors[5] = "|cffFE890D"
            set colors[6] = "|cff1FBF00"
            set colors[7] = "|cffE45AAF"
            set colors[8] = "|cff949596"
            set colors[9] = "|cff7DBEF1"
            set colors[10] = "|cff0F6145"
            set colors[11] = "|cff4D2903"
        endmethod
    endmodule
    
    struct PlayerColors extends array
    
        readonly string array colors
        
        // useless, but who cares, it gets inlined.
        static method getStringById takes integer id returns string
            return colors[id]
        endmethod
        
        static method getString takes player p returns string
            return getStringById(GetPlayerId(p))
        endmethod
        
        implement Init
    endstruct

endlibrary

That looks better :>
 
JASS:
globals
    public string array colors[12]
endglobals
why public? This is dangerous.

Don't think so:
For example a map with dynamic teams: if you switch from team a to team b the public declaration could help changing your color to that team's color...

or whatever you can think of, when a playercolor has to be changed.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,467
You don't need to make ColorStringGetColor a search-based function. Instead, you can make it run off one of the other hex-based libraries out there. I think Nestharus made at least 2 and Azlier made one.

As for listing player colors, I like this approach:

http://www.hiveworkshop.com/forums/jass-resources-412/snippet-getplayercolorstring-37737/

It was originally written by some guy but I edited it a while ago as an optimization. The key here is that you can use the handle ID of a playercolor as a 0-11 array variable.
 
Top