• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

A color snippet!

Status
Not open for further replies.

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
Here it is:
JASS:
library Color requires HexString
    
    globals
        private constant boolean JASS_MODE = true
    endglobals
    
    //! textmacro NEW_COLOR takes name
    private module $name$Module
        private integer _$name$Int
        method operator $name$Int= takes integer value returns nothing
            set ._$name$Int = value
            set ._$name$Hex = getHex(value)
        endmethod
        
        private string _$name$Hex
        method operator $name$Hex= takes string value returns nothing
            set ._$name$Int = HS2I(value)
            set ._$name$Hex = value
        endmethod
        
        method operator $name$ takes nothing returns thistype
            set .int = ._$name$Int
            set .hex = ._$name$Hex
            return this
        endmethod
        
    endmodule
    //! endtextmacro
    
    //! runtextmacro NEW_COLOR("red")
    //! runtextmacro NEW_COLOR("green")
    //! runtextmacro NEW_COLOR("blue")
    //! runtextmacro NEW_COLOR("alpha")
    
    struct Color
        
        private integer alphaInc
        private integer redInc
        private integer greenInc
        private integer blueInc
        
        private integer count
        private integer length
        
        private thistype nextColor
        
        readonly string hex
        readonly integer int
        
        readonly static constant string PREFIX = "|C"
        readonly static constant string SUFFIX = "|R"
        
        implement alphaModule
        implement redModule
        implement greenModule
        implement blueModule
        
        method operator color takes nothing returns string
            static if JASS_MODE then
                return PREFIX+"FF"+.red.hex+.green.hex+.blue.hex
            else
                return .red.hex+.green.hex+.blue.hex
            endif
        endmethod
        
        method operator value takes nothing returns thistype
        
            set .int = 0
            set .hex = .red.hex+.green.hex+.blue.hex
            static if JASS_MODE then
                set .hex = PREFIX+"FF"+.hex
            endif
            
            return this
        endmethod
        
        method operator valueEx takes nothing returns thistype
        
            set .int = 0
            set .hex = .alpha.hex+.red.hex+.green.hex+.blue.hex
            static if JASS_MODE then
                set .hex = PREFIX+.hex
            endif
            
            return this
        endmethod
        
        method setTargetColor takes thistype color, integer length returns nothing
        
            if .nextColor == 0 then
                set .nextColor = thistype.new(.alpha.int, .red.int, .green.int, .blue.int)
            else
                set .nextColor.alphaInt = .alpha.int
                set .nextColor.redInt = .red.int
                set .nextColor.greenInt = .green.int
                set .nextColor.blueInt = .blue.int
            endif
            
            set .count = 0
            set .length = length+1
            set .alphaInc = (color.alpha.int-.alpha.int)/.length
            set .redInc = (color.red.int-.red.int)/.length
            set .greenInc = (color.green.int-.green.int)/.length
            set .blueInc = (color.blue.int-.blue.int)/.length
            
        endmethod
        
        private method getHex takes integer i returns string
        
            local string s = I2HS(i, true)
            
            if StringLength(s) > 1 then
                return s
            else
                return "0"+s
            endif
            
        endmethod
        
        static method new takes integer alpha, integer red, integer green, integer blue returns thistype
            
            local thistype this = allocate()
            
            set .alphaInt = alpha
            set .redInt = red
            set .greenInt = green
            set .blueInt = blue
            
            set .alphaHex = getHex(alpha)
            set .redHex = getHex(red)
            set .greenHex = getHex(green)
            set .blueHex = getHex(blue)
            
            set .nextColor = 0
            
            return this
        endmethod
        
        method next takes boolean alpha returns string
        
            local string s
            
            if .nextColor != 0 then
                call .nextColor.destroy()
                set .nextColor.alphaInt = .nextColor.alpha.int + .alphaInc
                set .nextColor.redInt = .nextColor.red.int + .redInc
                set .nextColor.greenInt = .nextColor.green.int + .greenInc
                set .nextColor.blueInt = .nextColor.blue.int + .blueInc
                
                set s = .nextColor.red.hex+.nextColor.green.hex+.nextColor.blue.hex
                if alpha then
                    set s = .nextColor.alpha.hex+s
                else
                    set s = "FF"+s
                endif
                
                static if JASS_MODE then
                    set s = PREFIX+s
                endif
            else
                set s = .red.hex+.green.hex+.blue.hex
                
                if alpha then
                    set s = .alpha.hex+s
                else
                    set s = "FF"+s
                endif
                
                static if JASS_MODE then
                    set s = PREFIX+s
                endif
            endif
            
            set .count = .count + 1
            if .count == .length then
                set .count = 0
                set .alphaInc = -.alphaInc
                set .redInc = -.redInc
                set .greenInc = -.greenInc
                set .blueInc = -.blueInc
            endif
            
            return s
        endmethod
        
        method nextEx takes nothing returns thistype
            
            if .count == .length then
                set .count = 0
                set .alphaInc = -.alphaInc
                set .redInc = -.redInc
                set .greenInc = -.greenInc
                set .blueInc = -.blueInc
            endif
        
            if .nextColor != 0 then
                set .nextColor.alphaInt = .nextColor.alpha.int + .alphaInc
                set .nextColor.redInt = .nextColor.red.int + .redInc
                set .nextColor.greenInt = .nextColor.green.int + .greenInc
                set .nextColor.blueInt = .nextColor.blue.int + .blueInc
                set .count = .count + 1
                return thistype.new(.nextColor.alpha.int, .nextColor.red.int, .nextColor.green.int, .nextColor.blue.int)
            else
                set .count = .count + 1
                return thistype.new(.alpha.int, .red.int, .green.int, .blue.int)
            endif
            
        endmethod
        
    endstruct
    
endlibrary

Check the demonstration plz before commenting:
 

Attachments

  • [Snippet] Color.w3x
    23.2 KB · Views: 63
Status
Not open for further replies.
Top