- Joined
- Dec 29, 2008
- Messages
- 271
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.
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
v.1.1: Better safety for GetPlayerColorStringById thanks to Magtheridon
v.1.2: Module initializer thanks to bills
Last edited: