Text Colorizer Function (Already made): Comments?

This is where all discussion of WC3 mapmaking should be held. This forum exists particularly for mapmaking needs. If you have a question on how something works or have need for some code review, this is the place to put it.
Post Reply
Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Text Colorizer Function (Already made): Comments?

Post by Greenspawn » Thu Apr 08, 2010 12:13 am

Here's a nifty function that Blizzard doesn't have (I think). Use it at your own discretion, anyone could make it with a basic knowledge of hexadecimal/positional number systems. If you can optimize it, post it so please!

Code: Select all

function ColorizeText takes integer red, integer green, integer blue, string text returns string
    local string d = ""
    local integer i = 1
    local integer x
    if red>255 then
    set red = 255
    else
    if red<0 then
        set red = 0
    endif
    endif
    if green>255 then
        set green = 255
    else
    if green<0 then
        set green = 0
    endif
    endif
    if blue>255 then
        set blue = 255
    else
    if blue<0 then
        set blue = 0
    endif
    endif
    loop
        exitwhen i>2
            set x = ModuloInteger(red, 16)
            set red = (red-x)/16
            if x<10 then
            set d = I2S(x) + d
            endif
            if x == 10 then
                set d = "A" + d
            endif
            if x == 11 then
                set d = "B" + d
            endif
            if x == 12 then
                set d = "C" + d
            endif
            if x == 13 then
                set d = "D" + d
            endif
            if x == 14 then
                set d = "E" + d
            endif
            if x == 15 then
                set d = "F" + d
            endif
            set i = i + 1
    endloop
    loop
        exitwhen i>4
            set x = ModuloInteger(green, 16)
            set green = (green-x)/16
            if x<10 then
                set d = I2S(x) + d
            endif
            if x == 10 then
                set d = "A" + d
            endif
            if x == 11 then
                set d = "B" + d
            endif
            if x == 12 then
                set d = "C" + d
            endif
            if x == 13 then
                set d = "D" + d
            endif
            if x == 14 then
                set d = "E" + d
            endif
            if x == 15 then
                set d = "F" + d
            endif
            set i = i + 1
    endloop
    loop
        exitwhen i>6
            set x = ModuloInteger(blue, 16)
            set blue = (blue-x)/16
            if x<10 then
                set d = I2S(x) + d
            endif
            if x == 10 then
                set d = "A" + d
            endif
            if x == 11 then
                set d = "B" + d
            endif
            if x == 12 then
                set d = "C" + d
            endif
            if x == 13 then
                set d = "D" + d
            endif
            if x == 14 then
                set d = "E" + d
            endif
            if x == 15 then
                set d = "F" + d
            endif
            set i = i + 1
    endloop
    return "|c00" + d + text + "|r"
endfunction
This works because the Blizzard color encoders use hexadecimal numbers to store the color values of red, green, and blue. This essentially converts the decimal input (0-255) into a hex input (0-FF) and orders them correctly. Also there is a dummy statement that you can take out if you know what you're doing in the map, but I added it so that it wouldn't output wrong values if someone set the input to be less than 0 or greater than 255.

UPDATE: It didn't need to have three local strings, so I redid it to work with one local string.
Last edited by Greenspawn on Thu Apr 08, 2010 8:58 pm, edited 3 times in total.
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

User avatar
2-P
Revenent of the Replies
Posts: 328
Joined: Wed Feb 13, 2008 11:18 am

Re: Text Colorizer Function (Already made): Comments?

Post by 2-P » Thu Apr 08, 2010 4:24 pm

What's up with all those "else"? oO And indent the code in the if statement. >:

Code: Select all

if
blablabla
else
endif
----->

Code: Select all

if
  blablabla
endif
edit: and elseif might be a good idea.
Humans don't have the patience to wait even ten minutes for something!

Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Re: Text Colorizer Function (Already made): Comments?

Post by Greenspawn » Thu Apr 08, 2010 8:38 pm

Ok, thanks! That's fixed. Any other thoughts?
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

Post Reply

Return to “Custom Map Creation”