package Colors import NoWurst import Maths import String import Integer import Table import ErrorHandling public constant COLOR_WHITE = color(255,255,255) public constant COLOR_BLACK = color(0,0,0) /** Tuple for your RGB color needs */ public tuple color(int red, int green, int blue) /** Tuple for your RGBA color needs (with Alpha) */ public tuple colorA(int red, int green, int blue, int alpha) /** Add an alpha value to a simple color tuple */ public function color.withAlpha( int a ) returns colorA return colorA(this.red, this.green, this.blue, a) /** Get a colorA tuple from a hexstring */ public function fromHexString( string number ) returns colorA var alpha = number.substring(0,2) var red = number.substring(2,4) var green = number.substring(4,6) var blue = number.substring(6,4) return colorA( toDec(red), toDec(green), toDec(blue), toDec(alpha) ) public function color.normalize() returns color return color(this.red.abs(), this.green.abs(), this.blue.abs()) public function color.lerp(color target, real smoothness) returns color return color(this.red.lerp(target.red, smoothness),this.green.lerp(target.green, smoothness),this.blue.lerp(target.blue, smoothness)) public function makeColor(int colo) returns colorA int a int col = colo if col < 0 col=-(-col+0x80000000) a = 0x80 + col div 0x1000000 col = col-(a-0x80)*0x1000000 else a = col div 0x1000000 col = col-a*0x1000000 let r=col div 0x10000 col=col-r*0x10000 let g=col div 0x100 let b=col-g*0x100 return colorA(r,g,b,a) /** Constant playercolors */ public function fromPlayerColor( playercolor pc ) returns color return pc.toColor() public function playercolor.toColorString() returns string return this.toColor().toColorString() public function playercolor.toColor() returns color if this == PLAYER_COLOR_RED return color(255,3,3) else if this == PLAYER_COLOR_BLUE return color(0,66,255) else if this == PLAYER_COLOR_CYAN return color(28,230,185) else if this == PLAYER_COLOR_PURPLE return color(84,0,129) else if this == PLAYER_COLOR_YELLOW return color(255,252,1) else if this == PLAYER_COLOR_ORANGE return color(254,138,14) else if this == PLAYER_COLOR_GREEN return color(32,192,0) else if this == PLAYER_COLOR_PINK return color(229,91,176) else if this == PLAYER_COLOR_LIGHT_GRAY return color(149,150,151) else if this == PLAYER_COLOR_LIGHT_BLUE return color(126,191,241) else if this == PLAYER_COLOR_AQUA return color(16,98,70) else if this == PLAYER_COLOR_BROWN return color(78,42,4) return color(0,0,0) /** Get a simple colortuple containing the colorvalues of the playercolor */ public function fromPlayer( player p ) returns color return fromPlayerColor(GetPlayerColor(p)) /** * Mixes two colors, s would be a number 0<=s<=1 that determines * the weight given to color c2. * * c1.mix(c2,0) = c1 * c1.mix(c2,1) = c2 * c1.mix(c2,0.5) = Mixing the colors c1 and c2 in equal proportions. */ public function colorA.mix( colorA c2, real s ) returns colorA return colorA( R2I(c2.red*s+this.red*(1-s)+0.5), R2I(c2.green*s+this.green*(1-s)+0.5), R2I(c2.blue*s+this.blue*(1-s)+0.5), R2I(c2.alpha*s+this.alpha*(1-s)+0.5)) /** Set a units color to the tuplevalues with alpha */ public function unit.setVertexColor(colorA col) SetUnitVertexColor(this, col.red, col.green, col.blue, col.alpha) /** Set a units color to the tuplevalues without alpha */ public function unit.setVertexColor(color col) SetUnitVertexColor(this, col.red, col.green, col.blue, 255) /** Returns a string that is in the wc3 hexcolor format (|caarrggbb) */ public function colorA.toColorString() returns string return "|c" + toHex(this.alpha) + toHex(this.red) + toHex(this.green) + toHex(this.blue) /** Returns a string that is in the wc3 hexcolor format (|crrggbb) */ public function color.toColorString() returns string return "|cff" + toHex(this.red) + toHex(this.green) + toHex(this.blue) /** Returns the tuple in string format color(r,g,b) */ public function color.toString() returns string return "color(" + (this.red).toString() + ", " + (this.green).toString() + ", " + (this.blue).toString() + ")" /** Returns the tuple in string format colorA(r,g,b,a) */ public function colorA.toString() returns string return "colorA(" + (this.red).toString() + ", " + (this.green).toString() + ", " + (this.blue).toString() + ", " + (this.alpha).toString() + ")" /** Converts and int to a hexnumber */ public function toHex( int number ) returns string var firstpart = number div 16 var secondpart = number - firstpart * 16 return hexs[firstpart] + hexs[secondpart] /** Converts a twochar string to an integer */ public function toDec( string twochar ) returns int if twochar.length() > 2 error("todec is not made for this") var first = twochar.substring(0,1) var second = twochar.substring(1,2) return decs.loadInt(StringHash(first)) * 16 + decs.loadInt(StringHash(second)) string array hexs Table decs /** Playercolor array */ public color array playercolors init for i = 0 to 9 hexs[i] = i.toString() hexs[10] = "A" hexs[11] = "B" hexs[12] = "C" hexs[13] = "D" hexs[14] = "E" hexs[15] = "F" decs = new Table() for i2 = 0 to 9 decs.saveInt(StringHash(i2.toString()), i2 ) decs.saveInt(StringHash("A"), 10 ) decs.saveInt(StringHash("B"), 11 ) decs.saveInt(StringHash("C"), 12 ) decs.saveInt(StringHash("D"), 13 ) decs.saveInt(StringHash("E"), 14 ) decs.saveInt(StringHash("F"), 15 ) playercolors[0] = color(255,255,255) playercolors[1] = color(255,2,2) playercolors[2] = color(0,65,255) playercolors[3] = color(27,230,184) playercolors[4] = color(83,0,128) playercolors[5] = color(255,204,0) playercolors[6] = color(255,255,1) playercolors[7] = color(31,191,0) playercolors[8] = color(229,90,175) playercolors[9] = color(148,149,150) playercolors[10] = color(125,190,241) playercolors[11] = color(15,97,69) playercolors[12] = color(77,41,3)