User Tools

Site Tools


notes:csharp:snippets

This is an old revision of the document!


Code Snippets

Snippet: Rainbow colors:

class ColorRainbow
{
    public byte R { get; set; }
    public byte G { get; set; }
    public byte B { get; set; }
 
    int rainbowIndex = 0;
    int rainbowMax = 500;
 
    public void NextColor()
    {
        R = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / (float)(rainbowMax - 1))) / 2.0);
        G = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / (float)(rainbowMax - 1) + 2.0 * Math.PI / 3.0)) / 2.0);
        B = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / (float)(rainbowMax - 1) + 4.0 * Math.PI / 3.0)) / 2.0);
        rainbowIndex++;
        if (rainbowIndex > rainbowMax) rainbowIndex = 0;
 
        //Debug.WriteLine("R:{0:F2} G:{1:F2} B:{2:F2}", r, g, b);
    }
}
notes/csharp/snippets.1452783365.txt.gz · Last modified: 2020/08/26 (external edit)