Random Colors For Use In Defining Different Regions

A

Adam Hartshorne

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
 
A

Adam Hartshorne

Adam said:
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
 
A

Adam Hartshorne

Adam said:
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
 
A

Adam Hartshorne

Adam said:
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
 
M

Mike Wahler

Adam Hartshorne said:
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
 
A

Adam Hartshorne

Mike said:
I don't understand why you call it 'not sensible'


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
 
M

Mike Wahler

Adam Hartshorne said:
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
 
D

Daniel T.

Adam Hartshorne said:
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.
 
C

Cy Edmunds

Adam Hartshorne said:
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. :)
 
C

Cy Edmunds

Cy Edmunds said:
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
 
D

David Harmon

On Fri, 27 Jan 2006 23:08:44 +0000 in comp.lang.c++, Adam Hartshorne
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
 
R

rossum

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top