ColorListLerp

Something I’ve been interested in doing for my game is having a smooth lerp (linear interpolation) for a list of multiple colors. This kind of effect can be used for anything from health bars to sunsets. Out of the box, you get a LERP from one color to another. However, if you want to do more than one transition, (ex: Red -> Yellow -> Blue), you have to write some code.

Here’s the function I came up with:

<br></br>
         //colorList = list of n colors<br></br>
        //val = ratio of time 0 - 1<br></br>
        public Color getColorLerp(List<color> colorList, float val)<br></br>
        {<br></br>
            if (colorList.Count > 0)<br></br>
            {<br></br>
                float interval = 1f / (float)(colorList.Count-1);<br></br>
                int index = (int)((colorList.Count-1) * val);<br></br>
                float newVal = (val - (interval * index)) / interval;<br></br>
                if (index </color>```


Show Comments