Re: Unique Set of n Colors
Re: Unique Set of n Colors
- Subject: Re: Unique Set of n Colors
- From: Jay Reynolds Freeman <email@hidden>
- Date: Mon, 23 Jul 2007 14:00:37 -0700
I can't help much with the Cocoa side of it, but there is
a general kind of algorithmic approach that be useful here.
(Further discussion should probably go off-line since
this is a Cocoa list.)
To use this scheme you will have to read the Unix man
pages on either "rand" or "random", or both. Let's
assume you have done so and are going to use "random"
and "srandom".
1) When your code starts to run, if it does not already
have a saved number that represents your colors, create
one as follows:
a) Initialize "random" with the Unix time (man 3 time).
b) Now call "random" to get a random number; that's
the one you will save for subsequent use. Call it
N. Initializing "random" with the Unix time makes
for different N's every time you need a new one.
2) Now use your random number N -- whether looked up or
just now calculated -- to initialize "random" *again*.
Consecutive subsequent calls to "random" will now
generate the a series of random numbers that will be
the same every time the program runs, provided you
use the same number N to initialize "random".
3) Now enter the part of the algorithm that picks colors.
The idea is to use consecutive calls to "random" to
pick colors, and if the color you get is not okay,
throw it away and keep calling "random" till you
finally get a good one. The algorithm will
see the same random numbers every time, so it
make the same decisions every time, given the same N.
C-like psuedocode for picking colors might look
sorta kinda like the following, but graphics wizards
will have to figure out how to write the functions
"color_is_unacceptable_by_itself" and
"colors_are_too_much_alike". You don't really need
the special "..._color_chosen" loop variables, you
can look them up in your growing selection of colors,
but they make things more readable, I think.
DISCLAIMER -- I haven't tested this!!
< start psuedocode >
first_color_chosen = nil
most_recent_color_chosen = nil
your selection of colors = empty
while( you don't have enough colors ) { // Test details omitted
call "random" to get a new random number, M
take the three least-significant bytes of M
to be the RGB values of a color, C
if( color_is_unacceptable_by_itself( C ) )
continue // that is, go back to the start of the loop
if( first_color_chosen == nil ) { // No comparisons required
add C to your selection of colors
first_color_chosen = C
most_recent_color_chosen = C
}
else if( you already have all the colors you need but one ) {
if( colors_are_too_much_alike( C, first_color_chosen ) )
// This test is for color-similarity between the
// first and last colors of the pie chart
continue
else if( colors_are_too_much_alike( C,
most_recent_color_chosen ) )
// Test for too similar to the adjacent color just
picked.
continue
else {
add C to your selection of colors
most_recent_color_chosen = C
}
else if( colors_are_too_much_alike( C,
most_recent_color_chosen ) )
// Test for too similar to the adjacent color just picked.
continue
else {
add C to your collection of colors
most_recent_color_chosen = C
}
}
< end psuedocode >
"color_is_unacceptable_by_itself" likely has to do with brightness
and aesthetics. "colors_are_too_much_alike" likely at minimum
requires that the two colors differ substantially in at least one
of R, G, and B.
I expect there are better ways to do this, but this kind of
approach will probably work.
-- Jay Reynolds Freeman
---------------------
email@hidden
http://web.mac.com/jay_reynolds_freeman (personal web site)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden