Making a light that fades out one color and fades in a different color

As in start one color, fade out, fade in with a second color, fade out, and loop.

I'm new to Unity and kinda new to programming and very new to javascript, so I'm sure I'm doing this some weird way. I did some research but now I'm stuck. At one point I'd gotten it to fade out of the first color and fade in the second color but it would break after that.

If I run this script, It very quickly alternates between the two selected colors- obviously not the intended effect.

var color0 = Color.magenta;
var color1 = Color.red;
var duration = 1.0;
var lastColor;
var nowFading;
var currentColorSum;
var lastColorSum;

function Awake () {
lastColor = color0;
nowFading = true;
}

function Update () {

    lastColorSum = lastColor.r + lastColor.g + lastColor.b;

    while(nowFading)
    {
        currentColorSum = light.color.r + light.color.g + light.color.b;
        if(currentColorSum <= 0)
        {
            nowFading = false;
        }

        else
        {
            if (lastColor == color0)
            {
                light.color -= color0 / duration * Time.deltaTime;
            }
            else if (lastColor == color1)
            {
                light.color -= color1 / duration * Time.deltaTime;
            }
            else
            {
                Debug.LogError("lastColor invalid", this);
            }
        }
    }

    while(!nowFading)
    {
        currentColorSum = light.color.r + light.color.g + light.color.b;
        if(currentColorSum >= lastColorSum)
        {
            if (lastColor == color0)
            {
                lastColor = color1;
                nowFading = true;
            }

            else if (lastColor == color1)
            {
                lastColor = color0;
                nowFading = true;
            }

            else
            {
                Debug.LogError("lastColor invalid", this);
            }
        }

        else
        {
            if (lastColor == color0)
            {
                light.color += color1 / duration * Time.deltaTime;
            }

            else if (lastColor == color1)
            {
                light.color += color0 / duration * Time.deltaTime;
            }

            else
            {
                Debug.LogError("lastColor invalid", this);
            }
        }
    }
}

How come it's not doing what I want? And am I using all of these lines of code to do something I could do with a simple math function?

var color0 = Color.magenta;
var color1 = Color.red;
var duration = 1.0;

function Start () {
    while (true) {
        yield LerpLightColor (color0, Color.black);
        yield LerpLightColor (Color.black, color1); 
        yield LerpLightColor (color1, Color.black);
        yield LerpLightColor (Color.black, color0); 
    }
}

function LerpLightColor (col1 : Color, col2 : Color) {
    var t = 0.0;
    var rate = 1.0/duration;
    while (t < 1.0) {
        t += Time.deltaTime * rate;
        light.color = Color.Lerp(col1, col2, t);
        yield;
    }
}

this starts a coroutine which fades between each color once every second

var col1 = Color.blue;
var col2 = Color.green;
var light : Light;
var duration : float = 1.0;

function Start()
{
    ColorFunction();
}

function ColorFunction()
{

    var t = 1.0;

    while(true)
    {
        light.color = Color.Lerp(col1, col2, Mathf.PingPong(t,1));
        yield;
        t += Time.deltaTime / duration;
    }

}