2 Replies - 562 Views - Last Post: 30 July 2012 - 12:02 PM

#1 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Random Color

Posted 30 July 2012 - 10:42 AM

Since I looked all over for a random color method, not using random RGB, but randomly choosing predefined ones.

I gave up and just wrote my own, I'm sure it isn't the best way but hopefully the next person that comes looking for a method that doesn't create random RGB will find this useful. It can be adapted to be more generic by using a T (token) instead.

public static Color GetRandomColor(Random randomiser)
        {
            //Declare variabels
            PropertyInfo[]
                propInfo;
            Dictionary<string, Color>
                colors;

            //Get all the properties
            propInfo = typeof(Color).GetProperties();

            //Setup dictionary
            colors = new Dictionary<string, Color>();

            //Cycle through each property
            for (int propIndex = 0; propIndex < propInfo.Length; propIndex++)
            {
                //If the current property is type color
                if (propInfo[propIndex].PropertyType.IsAssignableFrom(typeof(Color)))
                {
                    //Add current color
                    colors.Add(propInfo[propIndex].Name, (Color)propInfo[propIndex].GetValue(typeof(Color), null));
                }
            }

            //return the random color
            return colors[colors.Keys.ToArray()[randomiser.Next(0, colors.Count)]];
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Random Color

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2054
  • View blog
  • Posts: 4,049
  • Joined: 24-September 10

Re: Random Color

Posted 30 July 2012 - 10:54 AM

1) why not generalize the method, and allow passing in a collection of colors to choose from (as an array). You can then have a default version that uses the list of all System-Defined colors (those are the colors you are using reflection to grab).

2) you shouldn't do that reflection EVERY time you go to get a color. It's very expensive to reflect all the properties off an object, and sort out which meet your requirements and which don't.

You could instead reflect out all the values at start-up (or creation of the selector tool...). Then reuse the list/array over and over. This could act as that default selection group I speak about when writing the more generalized form of your funciton.

Another option is to hand write the array in code.

You could even have an enum defined for indexing.
Was This Post Helpful? 0
  • +
  • -

#3 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: Random Color

Posted 30 July 2012 - 12:02 PM

Yeah I won't be using this as the final method, more of a test one as I had never tried this.

public class Randomiser<T>
    {
        private Random
            randGen;
        private Dictionary<string, T>
            items;

        public Randomiser()
        {
            PropertyInfo[] propInfo;
            randGen = new Random();
            items = new Dictionary<string, T>();
            propInfo = typeof(T).GetProperties();
            for (int propIndex = 0; propIndex < propInfo.Length; propIndex++)
            {
                if(propInfo[propIndex].PropertyType.IsAssignableFrom(typeof(T)))
                {
                    items.Add(propInfo[propIndex].Name, (T)propInfo[propIndex].GetValue(typeof(T), null));
                }
            }
        }

        public T GetRandomItem()
        {
            return items[items.Keys.ToArray()[randGen.Next(0, items.Count)]];
        }
    }



That better?

Or this one, which will save a little on memory in certain cases

public class Randomiser<T>
    {
        private Random
            randGen;
        private List<string>
            items;
        private T
            token;

        public Randomiser()
        {
            PropertyInfo[] propInfo;
            randGen = new Random();
            items = new List<string>();
            propInfo = typeof(T).GetProperties();
            for (int propIndex = 0; propIndex < propInfo.Length; propIndex++)
            {
                if(propInfo[propIndex].PropertyType.IsAssignableFrom(typeof(T)))
                {
                    items.Add(propInfo[propIndex].Name);
                }
            }
        }

        public T GetRandomItem()
        {
            return (T)typeof(T).GetProperty(items[randGen.Next(0, items.Count)]).GetValue(typeof(T), null);
        }
    }


This post has been edited by racidon: 30 July 2012 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1