• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] [Snippet] QuickColor

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
An end-user purposed snippet. To ease you in using colors, save them for later use as well as modifying their compositions easily.

JASS:
library QuickColor requires HexString
/*
    
                        [Snippet] Quick Color v1.0
                                by Quilnez
        
        Small snippet for basic color needs. Would ease you in using
        colors as well as getting/modifying their compositions.
        
        Requires:
            HexString | hiveworkshop.com/forums/jass-resources-412/snippet-hexstring-248993/
        
        Limitations:
            - Can't use "alpha", "red", "green", and "blue" as
              variable name (case sensitive)
            - Must be declared outside library/scope
            - Can only be used as a global variable
        
        Example:
            
            Create a red color named "RED"
                //! runtextmacro CreateQuickColor("RED", "FFFF0000")
            
            Usage
                call BJDebugMsg(RED + "some string")
            
            Getting compositions
                call SetUnitVertexColor(someunit, RED.red, RED.green, RED.blue, RED.alpha)
                
            Modifying compositions
                set RED.alpha = 0
                set RED.red   = 0
                set RED.green = 0
                set RED.blue  = 0
*/

//! textmacro CreateQuickColor takes NAME, CODE
    scope QuickColor$NAME$

        globals
            string $NAME$ = "|C$CODE$"
        endglobals

        struct $NAME$ extends array
        
            private static integer a
            private static integer r
            private static integer g
            private static integer b
            
            private static method trunc takes integer i returns integer
                if i > 255 then
                    return 255
                elseif i < 0 then
                    return 0
                endif
                return i
            endmethod
            
            static constant method operator alpha takes nothing returns integer
                return a
            endmethod
            
            static method operator alpha= takes integer value returns nothing
                set a = trunc(value)
                set $NAME$ = SubString($NAME$,0,2)+I2HS(a,true)+SubString($NAME$,4,10)
            endmethod
            
            static constant method operator red takes nothing returns integer
                return r
            endmethod
            
            static method operator red= takes integer value returns nothing
                set r = trunc(value)
                set $NAME$ = SubString($NAME$,0,4)+I2HS(r,true)+SubString($NAME$,6,10)
            endmethod
            
            static constant method operator green takes nothing returns integer
                return g
            endmethod
            
            static method operator green= takes integer value returns nothing
                set g = trunc(value)
                set $NAME$ = SubString($NAME$,0,6)+I2HS(g,true)+SubString($NAME$,8,10)
            endmethod
            
            static constant method operator blue takes nothing returns integer
                return b
            endmethod
            
            static method operator blue= takes integer value returns nothing
                set b = trunc(value)
                set $NAME$ = SubString($NAME$,0,8)+I2HS(b, true)
            endmethod
            
            private static method onInit takes nothing returns nothing
                set a = HS2I(SubString($NAME$,2,4))
                set r = HS2I(SubString($NAME$,4,6))
                set g = HS2I(SubString($NAME$,6,8))
                set b = HS2I(SubString($NAME$,8,10))
            endmethod
            
        endstruct

    endscope
//! endtextmacro
    
endlibrary

Demo:
JASS:
//! runtextmacro CreateQuickColor("RED", "FFFF0000")
    
scope test initializer init
    
    private function init takes nothing returns nothing
        call BJDebugMsg(RED+"1234567890|R")
        call BJDebugMsg("alpha: " + I2S(RED.alpha))
        call BJDebugMsg("red: " + I2S(RED.red))
        call BJDebugMsg("green: " + I2S(RED.green))
        call BJDebugMsg("blue: " + I2S(RED.blue))
    endfunction
    
endscope
 
Last edited:
Level 14
Joined
Dec 12, 2012
Messages
1,007
I don't think this is a typical application for textmacros...

Textmacros are usually helpful if you want to abstract away concrete types (basically like an unsafe version of templates) or to generate redundant code which just differs in some namings but is structurally equal.

Both things do not apply to a Color class, as their internal type will always be the same (either 4 ints or 1 string) and the same code can be used for all kind of colors. Creating one such struct per Color is pretty much generated code where one time would be enough.

Also I think it would be better to not truncate invalid user arguments but to throw some error (in debug mode), because that is usually a logic error that should be fixed.
 
Top