473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jan 27 '06 #1
12 3049
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
Jan 27 '06 #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 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
Jan 27 '06 #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
Jan 27 '06 #4

"Adam Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:43**********@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
Jan 27 '06 #5
Mike Wahler wrote:
"Adam Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:43**********@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
Jan 28 '06 #6

"Adam Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:43**********@mk-nntp-2.news.uk.tiscali.com...
Mike Wahler wrote:
"Adam Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:43**********@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
Jan 28 '06 #7
In article <43**********@mk-nntp-2.news.uk.tiscali.com>,
Adam Hartshorne <ad**@dcs.warwick.ac.uk> 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.
Jan 28 '06 #8

"Adam Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:43**********@mk-nntp-2.news.uk.tiscali.com...
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. :)
Jan 28 '06 #9

"Cy Edmunds" <sp***************@rochester.rr.com> wrote in message
news:DC********************@twister.nyroc.rr.com.. .

"Adam Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:43**********@mk-nntp-2.news.uk.tiscali.com...
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
Jan 28 '06 #10
Adding to what Mike wrote, you might prefer the HSL or HSV color
spaces over RGB for random generation.

http://en.wikipedia.org/wiki/HSL_color_space
http://en.wikipedia.org/wiki/HSV_color_space
Martin

--
Quidquid latine scriptum sit, altum viditur.
Jan 28 '06 #11
On Fri, 27 Jan 2006 23:08:44 +0000 in comp.lang.c++, Adam Hartshorne
<ad**@dcs.warwick.ac.uk> 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

Jan 28 '06 #12
On Fri, 27 Jan 2006 23:08:44 +0000, Adam Hartshorne
<ad**@dcs.warwick.ac.uk> 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
Jan 28 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
1
by: Job Lot | last post by:
how to genrate random colors without repeat of the same color or color code. I am creating a line graph using ComponentOne Chart control and what to show series with different colors. thanx
14
by: Debbie Lucier | last post by:
How would I generate a even random number each time? I can generate a random number each time but not an even one? Here is the code I use for the random number from 1-100. <script...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
11
by: Paul Smith | last post by:
I have a button on my web page the backcolor of which I want to change: btnSample.backcolor = ???????? I want the color to be Gainsboro However I enter Gainsboro or color.Gainsboro I have...
5
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag =...
1
by: Demi | last post by:
I want to be able to define standard colors in a base form, then have child forms use those values instead of standard colors. Ex in my base form I want to do this: Color myColor =...
0
by: sweatha | last post by:
Hi Friends I have designed a form with a label box named 'Label30' and text box named 'TextBox21'. Then I have generated the random number in the label box using the coding in form load event as ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.