Snippet Colorize

Tukki

is Skeleton Pirate.
Reaction score
29
Current Version: 1.0

JASS:
//===========================================================================
//  COLORIZE [1.0]
//===========================================================================
//  
//  QUICK INFO:
//
//    Importing   : Paste this text into an empty, custom-text trigger
//    Author      : Tukki (credits are not required)
//    Requirements: JASS/vJASS knowledge ? 
//
//  FUNCTIONS:
//           
//      call Colorize_Text(<sometext>) --> colorized version
//      call ColorText(<sometext>)     --> colorized version
//
//  EXAMPLES:
//
//      local string text = Colorize_Text("AJF88-KG783-59DE")
//      call DisplayTextToPlayer(Player(0), 0, 0, "Your Code: "+text)
//
//      call DisplayTextToPlayer(Player(0), 0, 0, ColorText("TH1S 1S SP4RT4!"))
//
//      set text = null
//
//  NOTES:
//
//  * There're two functions which do exactly the same thing, the difference
//  is that one requires the Colorize_ prefix and the other doesn't.
//  (Some people hate the public prefix, so for comfortability's sake it's there)
//   
//  * This function makes no difference between symbols (like &%@£$) and numbers
//
//  * If you are using this to colorize a save/load-code and don't want it to
//  colorize the spacing characters (often "-") check the function IsCharAllowed
//  further down. 
//===========================================================================
library Colorize

    //===========================================================================
    // CALIBRATION SECTION
    globals
        private constant string UPPER  = "|CFFAA0000" // Color of upper-case chars
        private constant string LOWER  = "|CFF00AA00" // Color of lower-case chars
        private constant string SYMBOL = "|CFF0000AA" // Color of symbols/numbers

        // Options if you want to add a legend to the returned string:
        //  * Leave it as it is for normal legend
        //  * Make it LEGEND = "" if you wish to disable it
        private constant string LEGEND = "\n\nLegend: "+UPPER+"Upper Case / "+LOWER+"Lower Case / "+SYMBOL+"Symbol/Number|r"
    endglobals
    // END OF CALIBRATION
    //===========================================================================
    
    //===========================================================================
    // CHARACTER CHECK - Checks if a character is allowed
    //
    // * Example of having 2 not-allowed characters:
    //
    // return char!="-" and char!="@"
    //
    // This will make the colorize function ignore - and @ characters
    // and add them as white-colored.
    //===========================================================================
    private function IsCharAllowed takes string char returns boolean
        return true
    endfunction
    
    //===========================================================================
    // MAIN ENGINE - Colorizes a given string
    //===========================================================================
    private function ColorizeEngine takes string base returns string
        local integer length = StringLength(base)
        local integer i      = 0
        local string part    = ""
        local string new     = ""
        local boolean case
        local boolean symbol
        
        loop
            exitwhen i>=length
            set part   = SubString(base, i, i+ 1 )
            set case   = (StringCase(part, true)==part and StringCase(part, false)!=part)
            set symbol = (StringCase(part, true)==part and StringCase(part, false)==part)
            
            if (part!="" and IsCharAllowed(part)) then
                if (case and not symbol) then         
                    set new = new + UPPER + part + "|r"
                elseif (not case and not symbol) then 
                    set new = new + LOWER + part + "|r"
                elseif (not case and symbol) then     
                    set new = new + SYMBOL + part + "|r"
                endif
            elseif (not IsCharAllowed(part) and part!="") then
                set new = new + part    
            endif
            
            set i = i + 1
        endloop
        
        if (LEGEND!="" and LEGEND!=null) then
            set new = new + LEGEND
        endif
        return new
    endfunction
    
    //===========================================================================
    // FUNCTIONS - Functions YOU can use (one public and one normal)
    //===========================================================================
    public function Text takes string text returns string
        return ColorizeEngine(text)
    endfunction
    
    //===========================================================================
    function ColorText takes string text returns string
        return ColorizeEngine(text)
    endfunction
endlibrary



A simple and easy-to-use script with the purpose of colorizing a given string. It colorizes upper-case, lower-case and numbers/symbols differently.

There're even two functions for peope who hate public prefixes!:eek:

JASS:
local string text = Colorize_Text("AJF88-KG783-59DE")
call DisplayTextToPlayer(Player(0), 0, 0, "Your Code: "+text)

call DisplayTextToPlayer(Player(0), 0, 0, ColorText("TH1S 1S SP4RT4!"))

set text = null
 

Kabalsin

New Member
Reaction score
2
An interesting one. You should let the user choose the color for the upper/lower/numbers characters. Also you could add colors for player's name, so if you write player's name it automatically colors it with the player color.
 

Tukki

is Skeleton Pirate.
Reaction score
29
Care to read the script? :p
Script said:
JASS:
    //===========================================================================
    // CALIBRATION SECTION
    globals
        private constant string UPPER  = "|CFFAA0000" // Color of upper-case chars
        private constant string LOWER  = "|CFF00AA00" // Color of lower-case chars
        private constant string SYMBOL = "|CFF0000AA" // Color of symbols/numbers

        // Options if you want to add a legend to the returned string:
        //  * Leave it as it is for normal legend
        //  * Make it LEGEND = "" if you wish to disable it
        private constant string LEGEND = "\n\nLegend: "+UPPER+"Upper Case / "+LOWER+"Lower Case / "+SYMBOL+"Symbol/Number|r"
    endglobals

As for the player-color thing; that's not the purpose of this script. This doesn't care about which player the string is going to be displayed to.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Nevermind with that, it looks helpful for a few purposes. As you said, save/load codes.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
JASS:

            exitwhen i>=length
            set part   = SubString(base, i, i+ 1 )


Could be optimized to be:

JASS:

            set part = SubString(base,i,i+1)
            exitwhen part == ""


This would allow you to remove the length variable, and make it so that you never have to check for "" or null.

JASS:
            set case   = (StringCase(part, true)==part and StringCase(part, false)!=part)
            set symbol = (StringCase(part, true)==part and StringCase(part, false)==part)
            
            if (part!="" and IsCharAllowed(part)) then
                if (case and not symbol) then         
                    set new = new + UPPER + part + "|r"
                elseif (not case and not symbol) then 
                    set new = new + LOWER + part + "|r"
                elseif (not case and symbol) then     
                    set new = new + SYMBOL + part + "|r"
                endif
            elseif (not IsCharAllowed(part) and part!="") then
                set new = new + part    
            endif


Could be optimized to:

JASS:

            set case   = (StringCase(part, true)==part)
            set symbol = (StringCase(part, true)==part and StringCase(part, false)==part)
            if IsCharAllowed(part) then
                if symbol then
                    set new = new + SYMBOL + part + "|r"
                elseif case then
                    set new = new + UPPER + part + "|r"
                else
                    set new = new + LOWER + part + "|r"
                endif
            else
                set new = new + part
            endif


JASS:

        
        if (LEGEND!="" and LEGEND!=null) then
            set new = new + LEGEND
        endif


You should probably just make this enabled when debug mode is on:

JASS:

        
        debug if (LEGEND!="" and LEGEND!=null) then
            debug set new = new + LEGEND
        debug endif


I'm going to graveyard this until changes are made, it's a nice looking system though. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top