Random Colors For Use In Defining Different Regions

Discussion in 'C++' started by Adam Hartshorne, Jan 27, 2006.

  1. Hi All,

    I was wondering if somebody could post a few lines of code which would
    produce random colors, which will be used in defining different regions
    on a mesh. So in addition to having n random colors, I feel that there
    should also be some condition to ensure that they aren't too similar in
    appearance, given that is likely to range from 2-20 say.

    Any help much appreciated,

    Adam
     
    Adam Hartshorne, Jan 27, 2006
    #1
    1. Advertising

  2. Adam Hartshorne wrote:
    > Hi All,
    >
    > I was wondering if somebody could post a few lines of code which would
    > produce random colors, which will be used in defining different regions
    > on a mesh. So in addition to having n random colors, I feel that there
    > should also be some condition to ensure that they aren't too similar in
    > appearance, given that is likely to range from 2-20 say.
    >
    > Any help much appreciated,
    >
    > Adam


    I should have added I'm also looking to be able limit the range of the
    colors as well. Say to avoid ever having black or white etc.

    Adam
     
    Adam Hartshorne, Jan 27, 2006
    #2
    1. Advertising

  3. Adam Hartshorne wrote:
    > Hi All,
    >
    > I was wondering if somebody could post a few lines of code which would
    > produce random colors, which will be used in defining different regions
    > on a mesh. So in addition to having n random colors, I feel that there
    > should also be some condition to ensure that they aren't too similar in
    > appearance, given that is likely to range from 2-20 say.
    >
    > Any help much appreciated,
    >
    > Adam


    I think I should have said I obviously know how to produce 3 random
    numbers to plug into RGB, but I was looking for a more sensible
    arrangement than this.

    Also, I should have added I'm also looking to be able limit the range of
    the colors as well. Say to avoid ever having black or white etc.

    Adam
     
    Adam Hartshorne, Jan 27, 2006
    #3
  4. Adam Hartshorne wrote:
    > Hi All,
    >
    > I was wondering if somebody could post a few lines of code which would
    > produce random colors, which will be used in defining different regions
    > on a mesh. So in addition to having n random colors, I feel that there
    > should also be some condition to ensure that they aren't too similar in
    > appearance, given that is likely to range from 2-20 say.
    >
    > Any help much appreciated,
    >
    > Adam


    I think I should have said I obviously know how to produce 3 random
    numbers to plug into RGB, but I was looking for a more sensible
    arrangement than this.

    Also, I should have added I'm also looking to be able limit the range of
    the colors as well. Say to avoid ever having black or white etc.

    Adam
     
    Adam Hartshorne, Jan 27, 2006
    #4
  5. Adam Hartshorne

    Mike Wahler Guest

    "Adam Hartshorne" <> wrote in message
    news:43daaa5f$...
    > Adam Hartshorne wrote:
    >> Hi All,
    >>
    >> I was wondering if somebody could post a few lines of code which would
    >> produce random colors, which will be used in defining different regions
    >> on a mesh. So in addition to having n random colors, I feel that there
    >> should also be some condition to ensure that they aren't too similar in
    >> appearance, given that is likely to range from 2-20 say.
    >>
    >> Any help much appreciated,
    >>
    >> Adam

    >
    > I think I should have said I obviously know how to produce 3 random
    > numbers to plug into RGB, but I was looking for a more sensible
    > arrangement than this.


    I don't understand why you call it 'not sensible'

    >
    > Also, I should have added I'm also looking to be able limit the range of
    > the colors as well. Say to avoid ever having black or white etc.


    You could generate a (pseudo)random number withing a
    specified range. See the C FAQ for details.

    If any undesired values fall inside a range, you can define
    several ranges, delimited by the undesired values, and randomly
    select a range before randomly selecting a value from it.
    If necessary you could also use the same approach to randomly select
    'cells' or ranges of cells in your mesh.

    -Mike
     
    Mike Wahler, Jan 27, 2006
    #5
  6. Mike Wahler wrote:
    > "Adam Hartshorne" <> wrote in message
    > news:43daaa5f$...
    >> Adam Hartshorne wrote:
    >>> Hi All,
    >>>
    >>> I was wondering if somebody could post a few lines of code which would
    >>> produce random colors, which will be used in defining different regions
    >>> on a mesh. So in addition to having n random colors, I feel that there
    >>> should also be some condition to ensure that they aren't too similar in
    >>> appearance, given that is likely to range from 2-20 say.
    >>>
    >>> Any help much appreciated,
    >>>
    >>> Adam

    >> I think I should have said I obviously know how to produce 3 random
    >> numbers to plug into RGB, but I was looking for a more sensible
    >> arrangement than this.

    >
    > I don't understand why you call it 'not sensible'
    >
    >> Also, I should have added I'm also looking to be able limit the range of
    >> the colors as well. Say to avoid ever having black or white etc.

    >
    > You could generate a (pseudo)random number withing a
    > specified range. See the C FAQ for details.
    >
    > If any undesired values fall inside a range, you can define
    > several ranges, delimited by the undesired values, and randomly
    > select a range before randomly selecting a value from it.
    > If necessary you could also use the same approach to randomly select
    > 'cells' or ranges of cells in your mesh.
    >
    > -Mike
    >
    >


    However that isn't making any conditions that the n number of colors
    created are differing in appearance. You can limit the range of the
    random numbers for the creation of each R,G and B value, but that makes
    no stipulation that a color maybe not be used because a similar one has
    already been created.

    Adam
     
    Adam Hartshorne, Jan 27, 2006
    #6
  7. Adam Hartshorne

    Mike Wahler Guest

    "Adam Hartshorne" <> wrote in message
    news:43dab25d$...
    > Mike Wahler wrote:
    >> "Adam Hartshorne" <> wrote in message
    >> news:43daaa5f$...
    >>> Adam Hartshorne wrote:
    >>>> Hi All,
    >>>>
    >>>> I was wondering if somebody could post a few lines of code which would
    >>>> produce random colors, which will be used in defining different regions
    >>>> on a mesh. So in addition to having n random colors, I feel that there
    >>>> should also be some condition to ensure that they aren't too similar in
    >>>> appearance, given that is likely to range from 2-20 say.
    >>>>
    >>>> Any help much appreciated,
    >>>>
    >>>> Adam
    >>> I think I should have said I obviously know how to produce 3 random
    >>> numbers to plug into RGB, but I was looking for a more sensible
    >>> arrangement than this.

    >>
    >> I don't understand why you call it 'not sensible'
    >>
    >>> Also, I should have added I'm also looking to be able limit the range of
    >>> the colors as well. Say to avoid ever having black or white etc.

    >>
    >> You could generate a (pseudo)random number withing a
    >> specified range. See the C FAQ for details.
    >>
    >> If any undesired values fall inside a range, you can define
    >> several ranges, delimited by the undesired values, and randomly
    >> select a range before randomly selecting a value from it.
    >> If necessary you could also use the same approach to randomly select
    >> 'cells' or ranges of cells in your mesh.
    >>
    >> -Mike
    >>
    >>

    >
    > However that isn't making any conditions that the n number of colors
    > created are differing in appearance.


    If the RGB values are different, the colors will be different.
    Admittedly, the human eye cannot easily distinguish among
    colors with very close values. BUT:

    Again, you can impose your own restrictions upon the sets
    of values you generate. E.g. delimit your ranges, such as:
    make sure all red values are at least some distance (value)
    apart. E.g. if your possible range is from 500 to 32000,
    generate a random value between 5 and 320, and then multiply it
    by 100. You could do this with any size factor you like
    (e.g. 5,10, 32). Just remember if you want to include the
    highest and lowest possible values in the set, use a factor
    that divides it evenly. If you want to exclude the first
    'x' values from the set, just use 'x' as the bottom of your
    range. Use the same technique to exclude from the highest
    'x' values.

    All this isn't really a language issue, but of algorithms,
    applicable in any language.

    All I can say in closing is to try to be creative.

    -Mike
     
    Mike Wahler, Jan 28, 2006
    #7
  8. Adam Hartshorne

    Daniel T. Guest

    In article <43daa802$>,
    Adam Hartshorne <> wrote:

    > Hi All,
    >
    > I was wondering if somebody could post a few lines of code which would
    > produce random colors, which will be used in defining different regions
    > on a mesh. So in addition to having n random colors, I feel that there
    > should also be some condition to ensure that they aren't too similar in
    > appearance, given that is likely to range from 2-20 say.
    >
    > Any help much appreciated,


    Create an array of 20 colors of your choice, making sure you can tell
    the difference between them and none of them are black or white. Then
    have the program randomly choose between them. QED.
     
    Daniel T., Jan 28, 2006
    #8
  9. Adam Hartshorne

    Cy Edmunds Guest

    "Adam Hartshorne" <> wrote in message
    news:43daa802$...
    > Hi All,
    >
    > I was wondering if somebody could post a few lines of code which would
    > produce random colors, which will be used in defining different regions on
    > a mesh. So in addition to having n random colors, I feel that there should
    > also be some condition to ensure that they aren't too similar in
    > appearance, given that is likely to range from 2-20 say.
    >
    > Any help much appreciated,
    >
    > Adam


    The problem you pose isn't trivial in general. The outline is this: starting
    from RGB compute XYZ (based on some calibration you must know) and hence to
    Lab space. Select a color difference formula such as CIE2000 and you can
    then compute the difference in color between all pairs of patches. Now you
    still need something like an annealing algorithm to maximize the minimum
    color difference.

    However, if you only expect to have 20 colors on screen at one time I would
    just pick 20 colors from a palette using any decent image editor. That's a
    whole lot more practical. :)
     
    Cy Edmunds, Jan 28, 2006
    #9
  10. Adam Hartshorne

    Cy Edmunds Guest

    "Cy Edmunds" <> wrote in message
    news:DCBCf.106454$...
    >
    > "Adam Hartshorne" <> wrote in message
    > news:43daa802$...
    >> Hi All,
    >>
    >> I was wondering if somebody could post a few lines of code which would
    >> produce random colors, which will be used in defining different regions
    >> on a mesh. So in addition to having n random colors, I feel that there
    >> should also be some condition to ensure that they aren't too similar in
    >> appearance, given that is likely to range from 2-20 say.
    >>
    >> Any help much appreciated,
    >>
    >> Adam

    >
    > The problem you pose isn't trivial in general. The outline is this:
    > starting from RGB compute XYZ (based on some calibration you must know)
    > and hence to Lab space. Select a color difference formula such as CIE2000
    > and you can then compute the difference in color between all pairs of
    > patches. Now you still need something like an annealing algorithm to
    > maximize the minimum color difference.
    >
    > However, if you only expect to have 20 colors on screen at one time I
    > would just pick 20 colors from a palette using any decent image editor.
    > That's a whole lot more practical. :)
    >


    PS- You could Google for "named colors" to get some ideas. For instance:

    http://www.echoecho.com/documentation_named_colors.htm
     
    Cy Edmunds, Jan 28, 2006
    #10
  11. Martin Eisenberg, Jan 28, 2006
    #11
  12. Adam Hartshorne

    David Harmon Guest

    On Fri, 27 Jan 2006 23:08:44 +0000 in comp.lang.c++, Adam Hartshorne
    <> wrote,
    >I was wondering if somebody could post a few lines of code which would
    >produce random colors, which will be used in defining different regions
    >on a mesh. So in addition to having n random colors, I feel that there
    >should also be some condition to ensure that they aren't too similar in
    >appearance, given that is likely to range from 2-20 say.


    I would suggest generating an even spread of colors (where you have
    to define both "spread" and "even") then arranging them into random
    order with std::random_suffle().

    But this isn't really a C++ question; you might do better in
    comp.graphics.algorithms
     
    David Harmon, Jan 28, 2006
    #12
  13. Adam Hartshorne

    rossum Guest

    On Fri, 27 Jan 2006 23:08:44 +0000, Adam Hartshorne
    <> wrote:

    >Hi All,
    >
    >I was wondering if somebody could post a few lines of code which would
    >produce random colors, which will be used in defining different regions
    >on a mesh. So in addition to having n random colors, I feel that there
    >should also be some condition to ensure that they aren't too similar in
    >appearance, given that is likely to range from 2-20 say.
    >
    >Any help much appreciated,
    >
    >Adam


    Each individual colour value in RGB ranges from 0 to 255. Start by
    picking a set of reasonably well separated values from the whole
    range, say: 20, 60, 100, 140, 180, 220. Avoid 0 and 255 if you want
    to avoid white and black.

    Randomly pick one of these for the Red component. Randomly pick a
    different one for the Green and a different one again for the Blue.
    If you don't mind shades of grey then they don't even have to be
    different. If they do have to be different then swap the one picked
    with the end value in the preset list and pick any one except the
    last.

    Pseudocode:

    colourPresets = { 20, 60, 100, 140, 180, 220 }
    presetLast = 5
    colourIndex = colourPresets[random(0 to presetLast)]
    redComponent = colourPresets[colourIndex]
    swap(colourPresets[colourIndex], colourPresets[presetLast])
    decrement presetLast
    colourIndex = colourPresets[random(0 to presetLast)]
    greenComponent = colourPresets[colourIndex]
    swap(colourPresets[colourIndex], colourPresets[presetLast])
    decrement presetLast
    colourIndex = colourPresets[random(0 to presetLast)]
    greenComponent = colourPresets[colourIndex]

    Alternatively just preselect 20 or so suitable colours as others have
    suggested.


    rossum

    --

    The ultimate truth is that there is no ultimate truth
     
    rossum, Jan 28, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. globalrev
    Replies:
    4
    Views:
    782
    Gabriel Genellina
    Apr 20, 2008
  2. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    553
    darrel
    Aug 7, 2008
  3. ZelluX
    Replies:
    3
    Views:
    1,917
    Peter Otten
    Dec 1, 2008
  4. Andrew Poulos

    Date in regional format in different regions

    Andrew Poulos, May 2, 2005, in forum: Javascript
    Replies:
    6
    Views:
    146
    Dr John Stockton
    May 2, 2005
  5. VK
    Replies:
    15
    Views:
    1,210
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page