0

So I am using a C# WebBrowser control and am trying to convert a string into an HTML/CSS rainbow. Currently I simply randomly generate 1000 colors in hex format and iterate through each char in the string adding a span style color:(hex value) for each char. It works but I would like the colors to merge together for a more rainbow effect. Is this possible? Here is my current function:

    public static string Rainbow(string text)
    {
        int numColors = 1000;
        var colors = new List<string>();
        var random = new Random();
        for (int i = 0; i < numColors; i++)
        {
            colors.Add(String.Format("#{0:X6}", random.Next(0x1000000)));
        }

        string rainbow = "";
        int index = 0;
        foreach (char c in text)
        {
            rainbow += String.Format("<span style='color: {0};'>{1}</span>", colors[index], c.ToString());

            if (index > numColors)
                index = 0;
            else
                index++;
        }

        return rainbow;
    }
Ed R
  • 2,129
  • 6
  • 24
  • 32
  • Since what you want is to create rainbow text in a webbrowser control (basically a web browser) this is really more of a CSS question than a C# one. – apiguy Jan 11 '12 at 07:40
  • I would agree but since I need to store the rainbow spans in a C# string I cannot implement a CSS or javascript answer. I am basically manipulating the HTML documents innerHTML and passing it through to the WebBrowser when it's called in the C# environment. – Ed R Jan 11 '12 at 07:42
  • What you want is not entirely clear. You mean you want the colors to gradually change from one to another pe3r character instead of all being random? – Mr Lister Jan 11 '12 at 07:58
  • basically yes. Instead of going from possibly green to blue to black to purple. It fades from dark green to green to light green to light blue to blue (etc). Appearing more like a rainbow than a blob of different colored characters. – Ed R Jan 11 '12 at 08:01

2 Answers2

1

Simple; don't use Random!

Something like this may help you get started:

for (int i = 0; i < 256; i++)
    {
        colors.Add(String.Format("#{0:X2}{1:X2}00", i, 255-i));
    }

to fade from green to red in 255 steps.

HTH!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • this is exactly the track I need to be on I believe. Though this just covers 1 spectrum of the color chart. I was looking up this and came across a couple javascript examples which illustrate this better. Your way works but what if I wanted it to change colors a bit more rapidly? Say every 4-5 characters it starts fading into a new color scheme. [Javascript Example](http://rainbow.arch.scriptmania.com/scripts/rainbowtext2.html) – Ed R Jan 11 '12 at 08:48
  • Just make the steps bigger. `for (int i = 0; i < 256; i += 51)` – Mr Lister Jan 11 '12 at 09:45
1

You can use hsv to rgb conversion function. Here you find description:

How do I get a rainbow color gradient in C#?

Community
  • 1
  • 1
Thaven
  • 1,857
  • 3
  • 19
  • 35