ARGB

Scripts

14 years
edited 8 years
Code (jass) Select
1
2
library ARGB initializer init
3
//******************************************************************************
4
//*
5
//* ARGB 1.2
6
//* ====
7
//*  For your color needs.
8
//*  
9
//*  An ARGB object is a by-value struct, this means that assigning copies the
10
//* contents of the struct and that you don't have to use .destroy(), the
11
//* downside is that you cannot assign its members (can't do set c.r= 123 )
12
//*
13
//*  This library should have plenty of uses, for example, if your spell involves
14
//* some unit recoloring you can allow users to input the color in the config
15
//* section as 0xAARRGGBB and you can then use this to decode that stuff.
16
//*
17
//* You can also easily merge two colors and make fading effects using ARGB.mix
18
//*
19
//* There's ARGB.fromPlayer which gets an ARGB object containing the player's
20
//* color. Then you can use the previous utilities on it.
21
//*
22
//* The .str() instance method can recolor a string, and the recolorUnit method
23
//* will apply the ARGB on a unit
24
//*
25
//* For other uses, you can use the .red, .green, .blue and .alpha members to get
26
//* an ARGB object's color value (from 0 to 255).
27
//*
28
//* structs that have a recolor method that takes red,green,blue and alpha as 0.255
29
//* integers can implement the ARGBrecolor module to gain an ability to quickly
30
//* recolor using an ARGB object.
31
//*
32
//********************************************************************************
33
34
//=================================================================================
35
globals
36
    private string array i2cc
37
endglobals
38
39
//this double naming stuff is beginning to make me insane, if only TriggerEvaluate() wasn't so slow...
40
struct ARGB extends array
41
    static method create takes integer a, integer r, integer g, integer b returns ARGB
42
        return ARGB(b + g*0x100 + r*0x10000 + a*0x1000000)
43
    endmethod
44
    
45
    // not really part of the exported stuff, I may remove it in the future, so please don't call this textmacro
46
    //! textmacro ARGB_PLAYER_COLOR_2_ARGB
47
        if(pc==PLAYER_COLOR_RED) then
48
            return 0xFFFF0303
49
        elseif(pc==PLAYER_COLOR_BLUE) then
50
            return 0xFF0042FF
51
        elseif(pc==PLAYER_COLOR_CYAN) then
52
            return 0xFF1CE6B9
53
        elseif(pc==PLAYER_COLOR_PURPLE) then
54
            return 0xFF540081
55
        elseif(pc==PLAYER_COLOR_YELLOW) then
56
            return 0xFFFFFC01
57
        elseif(pc==PLAYER_COLOR_ORANGE) then
58
            return 0xFFFE8A0E
59
        elseif(pc==PLAYER_COLOR_GREEN) then
60
            return 0xFF20C000
61
        elseif(pc==PLAYER_COLOR_PINK) then
62
            return 0xFFE55BB0
63
        elseif(pc==PLAYER_COLOR_LIGHT_GRAY) then
64
            return 0xFF959697
65
        elseif(pc==PLAYER_COLOR_LIGHT_BLUE) then
66
            return 0xFF7EBFF1
67
        elseif(pc==PLAYER_COLOR_AQUA) then
68
            return 0xFF106246
69
        elseif(pc==PLAYER_COLOR_BROWN) then
70
            return 0xFF4E2A04
71
        endif
72
        
73
      return 0xFF111111
74
    //! endtextmacro
75
    static method fromPlayerColor takes playercolor pc returns ARGB
76
        //! runtextmacro ARGB_PLAYER_COLOR_2_ARGB()
77
    endmethod
78
79
    static method fromPlayer takes player p returns ARGB
80
     local playercolor pc=GetPlayerColor(p)
81
        //! runtextmacro ARGB_PLAYER_COLOR_2_ARGB()
82
    endmethod
83
84
    method operator alpha takes nothing returns integer
85
        if( integer(this) <0) then
86
            return 0x80+(-(-integer(this)+0x80000000))/0x1000000
87
        else
88
            return (integer(this))/0x1000000
89
        endif
90
    endmethod
91
    method operator alpha= takes integer na returns ARGB
92
     local integer a
93
     local integer r
94
     local integer g
95
     local integer b
96
     local integer col=integer(this)
97
98
       if (col<0) then
99
           set col=-(-col+0x80000000)
100
           set a=0x80+col/0x1000000
101
           set col=col-(a-0x80)*0x1000000
102
       else
103
           set a=col/0x1000000
104
           set col=col-a*0x1000000
105
       endif
106
       set r=col/0x10000
107
       set col=col-r*0x10000
108
       set g=col/0x100
109
       set b=col-g*0x100
110
       return ARGB(b + g*0x100 + r*0x10000 + na*0x1000000) 
111
    endmethod
112
113
114
115
116
    method operator red takes nothing returns integer
117
     local integer c=integer(this)*0x100
118
        if(c<0) then
119
            return 0x80+(-(-c+0x80000000))/0x1000000
120
        else
121
            return c/0x1000000
122
        endif
123
    endmethod
124
    method operator red= takes integer nr returns ARGB
125
     local integer a
126
     local integer r
127
     local integer g
128
     local integer b
129
     local integer col=integer(this)
130
131
       if (col<0) then
132
           set col=-(-col+0x80000000)
133
           set a=0x80+col/0x1000000
134
           set col=col-(a-0x80)*0x1000000
135
       else
136
           set a=col/0x1000000
137
           set col=col-a*0x1000000
138
       endif
139
       set r=col/0x10000
140
       set col=col-r*0x10000
141
       set g=col/0x100
142
       set b=col-g*0x100
143
       return ARGB(b + g*0x100 + nr*0x10000 + a*0x1000000) 
144
    endmethod
145
146
    method operator green takes nothing returns integer
147
     local integer c=integer(this)*0x10000
148
        if(c<0) then
149
            return 0x80+(-(-c+0x80000000))/0x1000000
150
        else
151
            return c/0x1000000
152
        endif
153
    endmethod
154
    method operator green= takes integer ng returns ARGB
155
     local integer a
156
     local integer r
157
     local integer g
158
     local integer b
159
     local integer col=integer(this)
160
161
       if (col<0) then
162
           set col=-(-col+0x80000000)
163
           set a=0x80+col/0x1000000
164
           set col=col-(a-0x80)*0x1000000
165
       else
166
           set a=col/0x1000000
167
           set col=col-a*0x1000000
168
       endif
169
       set r=col/0x10000
170
       set col=col-r*0x10000
171
       set g=col/0x100
172
       set b=col-g*0x100
173
       return ARGB(b + ng*0x100 + r*0x10000 + a*0x1000000) 
174
    endmethod
175
176
    //=======================================================
177
    //
178
    //
179
    method operator blue takes nothing returns integer
180
     local integer c=integer(this)*0x1000000
181
        if(c<0) then
182
            return 0x80+(-(-c+0x80000000))/0x1000000
183
        else
184
            return c/0x1000000
185
        endif
186
    endmethod
187
    method operator blue= takes integer nb returns ARGB
188
     local integer a
189
     local integer r
190
     local integer g
191
     local integer b
192
     local integer col=integer(this)
193
194
       if (col<0) then
195
           set col=-(-col+0x80000000)
196
           set a=0x80+col/0x1000000
197
           set col=col-(a-0x80)*0x1000000
198
       else
199
           set a=col/0x1000000
200
           set col=col-a*0x1000000
201
       endif
202
       set r=col/0x10000
203
       set col=col-r*0x10000
204
       set g=col/0x100
205
       set b=col-g*0x100
206
       return ARGB(nb + g*0x100 + r*0x10000 + a*0x1000000) 
207
    endmethod
208
209
    //====================================================================
210
    // Mixes two colors, s would be a number 0<=s<=1 that determines
211
    // the weight given to color c2.
212
    //
213
    //  mix(c1,c2,0)   = c1
214
    //  mix(c1,c2,1)   = c2
215
    //  mix(c1,c2,0.5) = Mixing the colors c1 and c2 in equal proportions.
216
    //
217
    static method mix takes ARGB c1, ARGB c2, real s returns ARGB
218
      //widest function ever
219
      return ARGB( R2I(c2.blue*s+c1.blue*(1-s)+0.5) + R2I(c2.green*s+c1.green*(1-s)+0.5)*0x100 + R2I(c2.red*s+c1.red*(1-s)+0.5)*0x10000 + R2I(c2.alpha*s+c1.alpha*(1-s)+0.5)*0x1000000)
220
    endmethod
221
222
    method str takes string s returns string
223
       return "|c"+i2cc[.alpha]+i2cc[.red]+i2cc[.green]+i2cc[.blue]+s+"|r"
224
    endmethod
225
226
    method recolorUnit takes unit u returns nothing
227
     local integer a
228
     local integer r
229
     local integer g
230
     local integer b
231
     local integer col=integer(this)
232
233
       if (col<0) then
234
           set col=-(-col+0x80000000)
235
           set a=0x80+col/0x1000000
236
           set col=col-(a-0x80)*0x1000000
237
       else
238
           set a=col/0x1000000
239
           set col=col-a*0x1000000
240
       endif
241
       set r=col/0x10000
242
       set col=col-r*0x10000
243
       set g=col/0x100
244
       set b=col-g*0x100
245
       call SetUnitVertexColor(u,r,g,b,a)
246
    endmethod
247
248
endstruct
249
250
module ARGBrecolor
251
    method ARGBrecolor takes ARGB color returns nothing
252
     local integer a
253
     local integer r
254
     local integer g
255
     local integer b
256
     local integer col=integer(this)
257
258
       if (col<0) then
259
           set col=-(-col+0x80000000)
260
           set a=0x80+col/0x1000000
261
           set col=col-(a-0x80)*0x1000000
262
       else
263
           set a=col/0x1000000
264
           set col=col-a*0x1000000
265
       endif
266
       set r=col/0x10000
267
       set col=col-r*0x10000
268
       set g=col/0x100
269
       set b=col-g*0x100
270
       
271
       call this.recolor(r, g , b, a)
272
    endmethod
273
274
endmodule
275
276
private function init takes nothing returns nothing
277
 local integer i=0
278
279
    // Don't run textmacros you don't own!
280
    //! textmacro ARGB_CHAR takes int, chr
281
        set i=0
282
        loop
283
            exitwhen i==16
284
            set i2cc[$int$*16+i]="$chr$"+i2cc[$int$*16+i]
285
            set i2cc[i*16+$int$]=i2cc[i*16+$int$]+"$chr$"
286
            set i=i+1
287
        endloop
288
    //! endtextmacro
289
    //! runtextmacro ARGB_CHAR( "0","0")
290
    //! runtextmacro ARGB_CHAR( "1","1")
291
    //! runtextmacro ARGB_CHAR( "2","2")
292
    //! runtextmacro ARGB_CHAR( "3","3")
293
    //! runtextmacro ARGB_CHAR( "4","4")
294
    //! runtextmacro ARGB_CHAR( "5","5")
295
    //! runtextmacro ARGB_CHAR( "6","6")
296
    //! runtextmacro ARGB_CHAR( "7","7")
297
    //! runtextmacro ARGB_CHAR( "8","8")
298
    //! runtextmacro ARGB_CHAR( "9","9")
299
    //! runtextmacro ARGB_CHAR("10","A")
300
    //! runtextmacro ARGB_CHAR("11","B")
301
    //! runtextmacro ARGB_CHAR("12","C")
302
    //! runtextmacro ARGB_CHAR("13","D")
303
    //! runtextmacro ARGB_CHAR("14","E")
304
    //! runtextmacro ARGB_CHAR("15","F")
305
endfunction
306
307
endlibrary
308


For example:

Code (jass) Select
1
2
function Degrade takes string message, ARGB colorA, ARGB colorB returns string
3
 local integer i=0
4
 local integer L=StringLength(message)
5
 local string  r
6
    if(L==1) then
7
        return ARGB.mix(colorA,colorB,0.5).str(message)
8
    endif
9
    set r=""
10
    loop
11
        exitwhen (i>=L)
12
        set r=r+ARGB.mix(colorA,colorB, i*(1.0/ (L-1) ) ).str( SubString(message,i,i+1) )
13
        set i=i+1
14
    endloop
15
 return r
16
endfunction
17
18
19
call BJDebugMsg( Degrade("Hello" , 0xFFFF0000, 0xFF0000FF ) )
20

This function would just add a degrade effect to some string, notice it 'leaks' some strings.

Version 1.1 comes with a module and therefore requires jasshelper G.0 or greater.

The module can be used by any struct with a recolor method takes red, green, blue and alpha as arguments. (in range 0..255). It will add a method called ARGBrecolor that takes an ARGB and will rapidly decode all four color values then call this other recolor() method, for example:

Code (jass) Select
1
2
struct color
3
    integer r
4
    integer g
5
    integer b
6
    integer a
7
8
    method recolor takes integer r, integer g, integer b, integer a returns nothing
9
        set this.r = r
10
        set this.g = g
11
        set this.b = b
12
        set this.a = a
13
    endmethod
14
15
    implement ARGBrecolor
16
endstruct
17
18
function test takes color cc returns nothing
19
    call cc.ARGBrecolor( 0xFFE040AA )
20
endfunction
21
22
The example is lame as the struct using the module is lame, but I guess the point is understandable.


[btable=Extra stuff]
The purpose of this script (please stop calling it "system") is to aid in a bunch of silly color related operations, it does so, the result code is fine and easy to use and the performance penalty is barely 3 times what the manual stuff would take. Even though struct systax is in use, it doesn't inherit problems from structs like the bloat .create() and .destroy() add or having to worry about cleaning leaks. Users can input colors as 0xAARRGGBB and this thing will handle decoding those things, it is also a proof of concept for doing such things using struct syntax.

Notice how the Degrade function does not need to do .destroy on what's returned by .mix() this simplifies things a little and is all a result of the structs being by-value instead of by-ref, fun? Also notice how Degrade is being called easily without specifying 4 values per color or having to use other variables to create the colors.
[/btable]

Edit: Fixed that bug with recolorUnit