tags:

views:

91

answers:

2

I need a function that for a given R,G,B values returns the next color.

By saying the next color I mean the color which one of the components change by 1(assuming that R,G,B values are of range 0-255).

It has to be constructed that way that starting from the white color(or black alternatively) you can iterate throught all of the colors(which count is 255*255*255). It would be nice, but not required that after iterating throught all of the colors, they are being repeated from the beginning.

+1  A: 

It is not clear what you mean with "next color" but you can easily have a counter from which you extract the components:

uint32 color = 0;

uint8 r = color%256;
uint8 g = (color/256) % 256;
uint8 b = (color/(256*256)) % 256;

// use color

++color;

otherwise you can just generate random colors and keep track of already used ones with an NSSet. Of course you are gonna pack 3 components in a single object, like an int.

Jack
I'm not interested in the random color. What i need is: overall color that we could get is 255*255*255>16mil. What i would like to get is the any of the 16mil colors(like iterate through it - order of the colors is not important - i don't want the colors to repeat untill i reach 16mil's one).
krasnyk
Jack, that should be an `unsigned` or `uint32_t` for `b` to have useful values.
Georg Fritzsche
That's true, thanks.
Jack
A: 

You could base color retrieval on color lists, either using a custom or Apple one like "Web Safe Colors":

NSColorList* cl = [NSColorList colorListNamed:@"Web Safe Colors"];
NSLog(@"%@", cl);
for (NSString *key in [cl allKeys]) { 
    NSLog(@" * %@ - %@", key, [cl colorWithKey:key]);
}

If you need more and the order doesn't matter, you could use e.g. +colorWithColorSpace:components:count::

uint32_t c = 0;
// increment c whenever a color is requested ...

CGFloat components[4] = { 
    ((c & 0xFF0000) >> 16) / 256.0, 
    ((c & 0x00FF00) >> 8 ) / 256.0,
    ((c & 0x0000FF) >> 0 ) / 256.0,
    0.0 
};
NSColor *color = [NSColor colorWithColorSpace:[NSColorSpace genericRGBColorSpace] 
                                   components:components 
                                        count:4];
Georg Fritzsche
That would work if any of the color palette had a lot of colors. I need like at least 1mil and the palettes have no more than 200.
krasnyk
@krasnyk: See the update - i wonder however if these are visually distinguishable.
Georg Fritzsche