Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Random, unique colors that are completely different

  1. #1
    Newbie Newbie
    Join Date
    Jul 2014
    Posts
    2

    Random, unique colors that are completely different

    I'm currently working on a chart drawing program with pie and bar charts, and am trying to create a function to generate a random color for each segment. I am currently using rand() to generate a random float[3] array for each and every segment, however some of the colors generated might look very similar to each other. (e.g 0.115f, 0.f, 0.f and 0.1684f, 0.f, 0.f). I was thinking of generating a big vector to store all the required colors and checking for similarities, but it seems to be a very tedious method.

    Is there easier way to generate a unique set of colors instead of using just rand() and checking? The maximum number of colors i need to generate is around 25.

  2. #2
    Junior Member Newbie
    Join Date
    Jul 2011
    Location
    USA
    Posts
    23
    Quote Originally Posted by nightmarez77 View Post
    I'm currently working on a chart drawing program with pie and bar charts, and am trying to create a function to generate a random color for each segment. I am currently using rand() to generate a random float[3] array for each and every segment, however some of the colors generated might look very similar to each other. (e.g 0.115f, 0.f, 0.f and 0.1684f, 0.f, 0.f). I was thinking of generating a big vector to store all the required colors and checking for similarities, but it seems to be a very tedious method.

    Is there easier way to generate a unique set of colors instead of using just rand() and checking? The maximum number of colors i need to generate is around 25.
    Why can't you just hardcode the colors and map them to integers? Do they need to be randomly generated? RGB space has 256^3 color combinations which are far more than enough to pick 25 distinct colors.

    You could also partition the interval [0, 255] into 25 disjoint closed intervals. The midpoint of each closed interval is then mapped to some integer from the list 0,1,2,..,24. So now just pick three random numbers from 0-24 and use them in a color map to create an RGB triple (x,y,z), where x,y,z are integers in [0,255]. Of course, you would then need to remap the three integers in [0,255] to three floating point types in [0,1] before using the color.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •