 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Adam Hartshorne Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Random Colors For Use In Defining Different Regions |
|
|
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 |
|
| Back to top |
|
 |
Adam Hartshorne Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
Mike Wahler wrote:
| Quote: | "Adam Hartshorne" <adam (AT) dcs (DOT) warwick.ac.uk> wrote in message
news:43daaa5f$1_3@mk-nntp-2.news.uk.tiscali.com...
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 |
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
"Adam Hartshorne" <adam (AT) dcs (DOT) warwick.ac.uk> wrote in message
news:43daaa5f$1_3@mk-nntp-2.news.uk.tiscali.com...
| Quote: | 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'
| Quote: |
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 |
|
| Back to top |
|
 |
Adam Hartshorne Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
Adam Hartshorne wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Adam Hartshorne Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
Adam Hartshorne wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Adam Hartshorne Guest
|
Posted: Sat Jan 28, 2006 1:15 am Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
Adam Hartshorne wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Jan 28, 2006 2:38 am Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
"Adam Hartshorne" <adam (AT) dcs (DOT) warwick.ac.uk> wrote in message
news:43dab25d$1_3@mk-nntp-2.news.uk.tiscali.com...
| Quote: | Mike Wahler wrote:
"Adam Hartshorne" <adam (AT) dcs (DOT) warwick.ac.uk> wrote in message
news:43daaa5f$1_3@mk-nntp-2.news.uk.tiscali.com...
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 |
|
| Back to top |
|
 |
Martin Eisenberg Guest
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Sat Jan 28, 2006 9:00 pm Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
On Fri, 27 Jan 2006 23:08:44 +0000 in comp.lang.c++, Adam Hartshorne
<adam (AT) dcs (DOT) warwick.ac.uk> wrote,
| Quote: | 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 |
|
| Back to top |
|
 |
rossum Guest
|
Posted: Sat Jan 28, 2006 10:00 pm Post subject: Re: Random Colors For Use In Defining Different Regions |
|
|
On Fri, 27 Jan 2006 23:08:44 +0000, Adam Hartshorne
<adam (AT) dcs (DOT) warwick.ac.uk> wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|