Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

ARGB No New Posts Codes & Snippets

Started by
Guest

0 Members and 1 Guest are viewing this topic.

ARGB
on: August 21, 2011, 07:41:28 AM
Category: String handling, Variables
Language: vJASS

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

For example:

Code: jass
  1. function Degrade takes string message, ARGB colorA, ARGB colorB returns string
  2.  local integer i=0
  3.  local integer L=StringLength(message)
  4.  local string  r
  5.     if(L==1) then
  6.         return ARGB.mix(colorA,colorB,0.5).str(message)
  7.     endif
  8.     set r=""
  9.     loop
  10.         exitwhen (i>=L)
  11.         set r=r+ARGB.mix(colorA,colorB, i*(1.0/ (L-1) ) ).str( SubString(message,i,i+1) )
  12.         set i=i+1
  13.     endloop
  14.  return r
  15. endfunction
  16.  
  17.  
  18. call BJDebugMsg( Degrade("Hello" , 0xFFFF0000, 0xFF0000FF ) )
  19.  
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
  1. struct color
  2.     integer r
  3.     integer g
  4.     integer b
  5.     integer a
  6.  
  7.     method recolor takes integer r, integer g, integer b, integer a returns nothing
  8.         set this.r = r
  9.         set this.g = g
  10.         set this.b = b
  11.         set this.a = a
  12.     endmethod
  13.  
  14.     implement ARGBrecolor
  15. endstruct
  16.  
  17. function test takes color cc returns nothing
  18.     call cc.ARGBrecolor( 0xFFE040AA )
  19. endfunction
  20.  
  21.  
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
« Last Edit: December 18, 2017, 01:44:42 PM by moyack »



 

Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...