Tutorial Changing Color Gradients In-Game Tutorial

T

toast

Guest
Before you read: If you don't have at least a basic understanding of color codes, please look up a tutorial!

There are two ways to make color gradients: first those that are predefined and do not change throughout the map, and second those that must be changed on the fly during play.

I will explain only the second method, as the first one is well known and there are even programs that will do it for you. They can't, however, make:

Part One: Making Series of Gradients

This section describes how to set text to show only one color at a time as it progresses through a gradient (see screenshot at the end of this part). To create a regular left-to-right, single-string gradient in-game, go to part two.

The way this works is by creating a color code based on any given percentage of gradience between two end colors. One example of a use for this is life display in a TD: green when it's at full but slowly shifting downwards towards red as the lives decrease. It's a cool effect.

First, you will need a Jass function I found at Jass Vault:
Code:
function CreateColorString takes integer red, integer green, integer blue, integer alpha returns string
    local string charMap = "0123456789abcdef"
    local string cs
    local integer c
    set c = alpha / 0x10
    set cs = SubString(charMap, c, c + 1)
    set c = ModuloInteger(alpha, 0x10)
    set cs = cs + SubString(charMap, c, c + 1)
    set c = red / 0x10
    set cs = cs + SubString(charMap, c, c + 1)
    set c = ModuloInteger(red, 0x10)
    set cs = cs + SubString(charMap, c, c + 1)
    set c = green / 0x10
    set cs = cs + SubString(charMap, c, c + 1)
    set c = ModuloInteger(green, 0x10)
    set cs = cs + SubString(charMap, c, c + 1)
    set c = blue / 0x10
    set cs = cs + SubString(charMap, c, c + 1)
    set c = ModuloInteger(blue, 0x10)
    return cs + SubString(charMap, c, c + 1)
endfunction
For you GUI'ers like me, copy and paste the function into the "Custom Script Code" box that appears when you select the image and name of your map in the Trigger Editor. That will allow you to use it with a Custom Script action from anywhere in your map.

The function takes RGB and alpha values (which are generally left at 255) and returns an 8-character string of the corresponding color code, without the "|C".

1. Decide the values of the start and end colors. For this tutorial I will be using green (1BFF0F) and red (FF0303). Convert these two values into their RGB parts. For green and red, I have 27, 255, 15 and 255, 3, 3.

2. Subtract the corresponding RGB values of the end color from the start color (i.e. green minus red). In this case, I get -228, 252, and 12.

3. From now on, these will be actions you perform in a trigger. The first action is to decide on what will be determining the percentage between the 2 end colors. In the TD example as before, I'll use the lives:
Code:
Set tempReal = ((Real(currentLives)) / (Real(originalLives)))

Make sure this ratio will always be between 0 and 1.

4. The next step is to define the method of determining the values based on percentage. Begin with the differences of the two colors. In this case, -288, 252, and 12. Since this is the distance over which the gradient will be varying, multiply each by the percentage. That gives us -228*tempReal, 252*tempReal, and 12*tempReal. But wait! You can't have negative RGB values. Since a negative indicates that the number is changing from low to high instead of high to low, we can simply take the absolute value and multiply by 1-percentage. In the case of the red component, we now have 228*(1-tempReal).

5. Add the lowest value for each of red, green, and blue to each of their corresponding expressions. Since no value can fall lower than that lowest and the multiplication yeilds a number between 0 and the difference, we have to add it. Now we have (228*(1-tempReal))+27, (252*tempReal)+3, and (12*tempReal)+3.

6. That's it! Convert the numbers to integers and send them into CreateColorString with an alpha value of 255:
Code:
Custom script:   set udg_tempString="|c"+CreateColorString(R2I((1.0-udg_tempReal)*228.0)+27, R2I(udg_tempReal*252)+3, R2I(udg_tempReal*12)+3, 255)

It will return a string without "|C" at the beginning, so don't forget to store it in a variable somewhere! If you have the RGB values stored in a variable, you can simply replace each number in there with the mathematical expression that got that number in the first place. This is very much like the system described in part two.

(For GUI'ers, the "R2I" indicates a a real to integer conversion, and "udg" indicates you're using global variables, the ones you define in the little variable window in the Trigger Editor. If you really don't understand Jass, copy and paste this script and replace each expression that I showed in the tutorial with your own. Don't forget "udg" on your variable names!)

This can be expanded to as many colors as you like. In the TD-like screenshot below, I faded green into yellow while the ratio was 50% or more, and then yellow to red while it was below, creating a very nice traffic light-colored series.

4g7wuj9.jpg


Part Two: Making Regular Gradients

Some people apparently mistook my original tutorial for creating regular left-to-right, single-string gradients in-game. As per the requests, here it is.

To make this work effectively, you will need some kind of system that gives you a set of 6 integers, 2 each of red, green, and blue. Such a system can be a predefined loop or player input, that is entirely up to you. Once you have the 6 values, follow these steps. These are essentially the steps that any program that creates regular gradients goes through, translated for use in-game. You'll also need an input string and an empty output string.

1. Calculate the average difference between successive steps of each red, green and blue, like so:
difference = (endColor-startColor)/(length of input-1)
Subtracting one color from another finds the total varying distance of the gradient. Since the number of changes is length-1 (0% has no effect), we divide by that. In WE terms:
Code:
Set redDifference = (((Real(redEnd)) - (Real(redStart))) / ((Real((Length of inputString))) - 1.00))
It's best to leave everything in reals and roundoff to the required integers last thing. Repeat for green and blue.

2. Since you'll be changing each character in a string, you will need to create a loop that will run through each character in the string:
Code:
For each (Integer A) from 1 to (Length of inputString), do (Actions)

3. All the remaining calculations are performed in the loop, as they are different for each character in the string, but the formulas are the same. To find a component, multiply the difference variable of it by the loop integer minus one - again, because 0% isn't counted. The code looks like:
Code:
Set redComponent = (redStart + (Integer((redDifference x (Real(((Integer A) - 1)))))))
Repeat for green and blue.

4. Now to add the character to the end of the string. This is broken down into two parts:

4a. Get the character in question using the Jass function SubStringBJ (since this will eventually be in one long Custom Script for you GUI'ers, it's easier to do this now as opposed to using the GUI substring function). Use the loop's index as the start and endpoints.
Code:
SubStringBJ(udg_inputString, bj_forLoopAIndex, bj_forLoopAIndex)
For step 5, this entire expression will be simplified to "SubStringBJ()" to keep things neat.

4b. Create the color code for the character using CreateColorString as before:
Code:
CreateColorString(redComponent, greenComponent, blueComponent, 255)
This will also be simplified for neatness in step 5 as "CreateColorString()"

5. With the character and it's color code complete, all that remains is to tack it onto the end of the output string. Don't forget to add in "|C" and "|R":
Code:
Custom Script: set udg_outputString = udg_outputString + "|C" + CreateColorString() + SubStringBJ() + "|R"

6. That's it! The loop runs through until the string is over, and a variable (in this case named outputString) will be the gradient-ized version of the input string!

And the end result:
44g6g6w.jpg


If you so wish, you can stack as many of these equations together and get a very complex gradient across other gradients. I don't plan on writing up on that, but if you would like to see these two methods in demonstration, take a look at the attached map.

Thanks for reading! :D
 

Attachments

  • Gradient Test.w3x
    15.1 KB · Views: 341

elmstfreddie

The Finglonger
Reaction score
203
Didn't feel like reading through it, but it looks good lol
(I don't know JASS and I barely make maps anymore, but I still like to help)
 

LordOglog

New Member
Reaction score
16
Very nice well written and structured perhaps some screenshots of colours in game would help visual learners (ask my school :)) . I thought at first this would be an explanation of how colours work, so good job suprising me.
+rep
it appears i must spread some rep
 
T

toast

Guest
Very nice well written and structured perhaps some screenshots of colours in game would help visual learners (ask my school :)) . I thought at first this would be an explanation of how colours work, so good job suprising me.
+rep
it appears i must spread some rep

Thanks. :D I put in a final product screenshot.
 

elmstfreddie

The Finglonger
Reaction score
203
Ok, ok, decided to read it.

Pretty cool, I'll probably use it eventually... Maybe in my maze game.
+rep
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
I like it! Color in all facets interests me, and this is a creative interesting use. Good job!
I did think at first that this was how to change an actual gradient, rather than a single digit, so the title is a little misleading. But other than that, Well done!
 
T

toast

Guest
I did think at first that this was how to change an actual gradient

I think that's still possible. If you have 4 colors, 2 starts and 2 ends, you can use this method to go through those gradients, say from green to white and red to black. Then from each other those colors, you can create another gradient, from light green to dark red. It's possible but difficult.

Think of it like this: you have 2 columns representing your calculated gradients of green-white and red-black. At some point down the line, you want to make a gradient joining them, a row. It forms a sort of H-shape.
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
I think that's still possible. If you have 4 colors, 2 starts and 2 ends, you can use this method to go through those gradients, say from green to white and red to black. Then from each other those colors, you can create another gradient, from light green to dark red. It's possible but difficult.

Think of it like this: you have 2 columns representing your calculated gradients of green-white and red-black. At some point down the line, you want to make a gradient joining them, a row. It forms a sort of H-shape.

Yes this would work nicely on floating text. But I dont think it would work on system text, (i.e. the example you've shown. AFAIK system text is static and can't be changed once it's displayed. Of course, I'd love it if someone could prove me wrong :))
 
I

IKilledKEnny

Guest
Indeed a nice tutorial, a fine suprise. :) However I would replace:

Before you read: If you don't have at least a basic understanding of color codes, please look up a tutorial!

With a good tutorial, I'm sure there are at least 5 tutorials about this topic only in thehelper.net. I especially recommand pineapple one's.
 
T

toast

Guest
Since I get the impression that you guys were looking for a tutorial that shows you how to make regular, left-to-right single-string gradients in-game, I did just that. :D Check out part two.
 

elmstfreddie

The Finglonger
Reaction score
203
Awesome, better than using that crappy Colour Convert program thingy.

HOWEVER: I do not like how the image is a link, it should be in
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
Maybe I better explain what I actually meant when I said the "gradient left to right" bit.

Your title says "changing color gradients in game". I thought it was about changing left to right gradients in game. And, as I said before I dont think your system (as nice as it is!!!) will work dynamically on system text. These are the examples you have given.

Your system is very nice and allows colors to be changed in game, but the colors are still static, once they are applied, in this case to system text. They are also still applied to single digits only. And that's the key to the dynamic gradient left to right color change. I'm 98% certain that your color trigger won't be able to change system text once it has been displayed. I'd like to see how a modified version of this works applied to floating text.

Like Elms said...it's far better than using the color converter. And both parts are dynamic to the amount of strings used. I'd Plus Rep you again, but I need to spread some. :D Nice job man!
 
T

toast

Guest
HOWEVER: I do not like how the image is a link, it should be in tags, and if it is, then upload your pics on tinypic.com. It's easy, and a lot better than imageshack n crap like that.[/QUOTE]

Thanks for the suggestion, I changed the images to tinypic.

[quote="chovynz, post: 387263"]Your system is very nice and allows colors to be changed in game, but the colors are still static, once they are applied, in this case to system text. They are also still applied to single digits only. And that's the key to the dynamic gradient left to right color change. I'm 98% certain that your color trigger won't be able to change system text once it has been displayed. I'd like to see how a modified version of this works applied to floating text. [/QUOTE]

I, too, am sure that this can't be applied to text already displayed. As far as I know there is no way to change any text that has already been displayed without removing and replacing it. (I would be gladly proved wrong, however :P - and if there was, then this theoretically should have no problem handling that).
And as for floating text, I believe it will have to use this remove-and-refresh system (right?), but I would also like to see something like that - perhaps I will make a little something.
...I would change the title to something like "Creating Gradients On-the-Fly" but I can't figure out how. :confused:
 

chovynz

We are all noobs! in different states of Noobism!
Reaction score
130
This is what I've done with your beautiful triggers.... :eek:
Entered chat gets displayed in the middle and with system text.

Thanks for the suggestion, I changed the images to tinypic.



I, too, am sure that this can't be applied to text already displayed. As far as I know there is no way to change any text that has already been displayed without removing and replacing it. (I would be gladly proved wrong, however :p - and if there was, then this theoretically should have no problem handling that).
And as for floating text, I believe it will have to use this remove-and-refresh system (right?), but I would also like to see something like that - perhaps I will make a little something.
...I would change the title to something like "Creating Gradients On-the-Fly" but I can't figure out how. :confused:

I can easily continously change the color of floating text using sliding vertex colors for sliding text...but that defeats your gradient as far as I know.
 

Attachments

  • Gradient Test_chovy.w3x
    15.8 KB · Views: 343
T

toast

Guest
I can easily continously change the color of floating text using sliding vertex colors for sliding text...but that defeats your gradient as far as I know.

Well perhaps I should read up on a floating text tutorial - it's one of the few elements of the WE that I've rarely (if ever) used. Also, you made a mistake in your map :p inputString is reset to the "This is the result of the loop! Testing one two three four five." every time after setting it to the entered string.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top