Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done
 
Private Sub btnRandomColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
        lblColor.BackColor = C_Library.RandomColor.GetRandomColor
        lblColor.Text = lblColor.BackColor.R.ToString + "," + lblColor.BackColor.G.ToString + "," + lblColor.BackColor.B.ToString
 
End Sub
 
Private Sub btnRepeatableColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
                _currentGroupColor = _randomColor.GetRepeatableColor();
 
End Sub
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Security.Cryptography;
 
namespace EventCalendar
{
    public class RandomColor
    {
        private string[] pallette =
        {
"FF6666",
"339933",
"3333CC",
"FF9933",
"990099",
"FFFF00",
"FF00FF",
"66FF33",
"6699FF",
"FFCC66",
"9900FF",
"FFFF66",
"CC0033",
"669966",
"66FFFF",
"CC6600",
"996699",
"CCFF00",
"990000",
"33FF99",
"3399FF",
"CC9900",
"CC33CC",
"00FF66"};
 
        private int _n = 0;
        public string GetRepeatableColor()
        {
            string x = pallette[_n];
            _n += 1;
            if (_n > 23)
            {
                _n = 0;
            }
            return x;
        }
        public static string GetRandomColorName()
        {
            KnownColor[] colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
            return Color.FromKnownColor(colors[GetRandomNumber(colors.Length)]).Name;
        }
        public static Color GetRandomColor()
        {
            KnownColor[] colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
            return Color.FromKnownColor(colors[GetRandomNumber(colors.Length)]);
        }
 
        public static int GetRandomNumber(int maxValue)
        {
            RandomNumberGenerator rng = RNGCryptoServiceProvider.Create();
            byte[] bytes = new byte[4];
            rng.GetBytes(bytes);
            int ranNum = BitConverter.ToInt32(bytes, 0);
            return Math.Abs(ranNum % maxValue);
        }
    }
}
 
Posted on Tuesday, December 18, 2007 1:30 PM miscellaneous | Back to top


Comments on this post: Random colors or Repeatable Colors

# re: Random colors or Repeatable Colors
Requesting Gravatar...
Really great post, thanks a lot for sharing this with us as it's really helped me out.
Left by canvas prints on Nov 08, 2011 11:21 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net