PDA

View Full Version : Randomized Color Generation (Help)



Holocron Coder
2012-07-03, 09:41 AM
So, I'm working on a pet project (that I'll, hopefully, soon release to the public for critique) that requires generating random colorization for part of the project. I know how to generate colors randomly, in a general sense, but I'm uncertain how to generate colors that are consistent, despite being random.

I'm not sure I'm making a lot of sense, so I'll spoil a bit: I need to generate colors that would be realistic for a nebula. Currently, a nebula is just simulated by a set of circular areas with centers contained in a larger circle. The display is then generated by randomly placing random-colored circles inside the subcircles.

So, I need a way to generate colors so that things seem... realistic? I'm not sure what I'm asking is even possible :smallbiggrin: but any suggestions would be useful.

Currently, I'm generating a base color for the nebula as a whole, then fluctuating around for each subcircle, then fluctuating around that for the individually-drawn circles (with each fluctuation getting smaller). Currently, red is the dominating color, with green and blue only having values up to the red value, at all three fluctuations (possibly resulting in red being the least color, but only barely). The result isn't quite right, though.

Suggestions? :smallbiggrin:

EDIT: Now that I think about it, I can even adjust how a nebula is calculating, although I'll need to retain the sub-circles. But if there is a different modeling that might help with coloring...

Frenth Alunril
2012-07-03, 10:23 AM
Depending on how you are generating your colors, the best way I can think of would be one of two things.

Either, have your white light (assumed default for brightness) affected by a dominant color thereby setting all other colors into that spectrum... Much like an overlay in photoshop. (okay.... never mind)

or, with random generation, make sure that all sub sets of colors generated fall withing an variance which is set by your first random generation.

For example, if we use ROYGBIV and your master randomizer is, R (red) then nothing would go more than say 2 from there, so you would get ivRoy. You could take it a step further, and assign percentages, such as Master is 50%, seconds are 18% and thirds are 7%... this would cut out your largest problem. (assuming complimentary colors are the problem)

If you got G as the master, you would end up with oyGbi which would look kind a like puke, but it's a nebula, what do you expect.

Just try to remember that most 'color' you see in space is put there by scientists who say, "Okay, this is Helium, let's make that yellow, and this is nitrogen, lets make that dark blue, and this is methane, lets make that green."

Those are my two bits.

Holocron Coder
2012-07-03, 10:59 AM
Mmm, well, to go into a bit of code, so to speak, this is roughly how it is being done now:

for each nebula:
red = random() * 125 + 100;
blue = random() * 125 + 100;
green = random() * 125 + 100;

for each subsection:
r = red + (random() * 50 - 25);
b = blue + (random() * 50 - 25);
g = green + (random() * 50 - 25);

for each circle-to-print:
color = (r + (random() * 50 - 25)
+ (g + (random() * 50 - 25)
+ (b + (random() * 50 - 25);

This results in a base color, with each subsection within 25 either way of that color, and each circle being within 25 of that color (resulting in a possible variance of 50 above and below above the base color for each).

I've done it with basing the colors on the other (blue = random() * red, etc), but then you have yellow and green nebula... so I'unno. Might just keep it at basically full-random. Or I might go the routes of nebula types (planetary, refractive, dark, etc). Not sure.

factotum
2012-07-04, 01:17 AM
How about using Hue, Saturation and Value rather than RGB? You can keep the Hue within a fairly tight range so you're not using completely different colours. You'd have to convert from HSV back to RGB to actually render the thing, of course--not sure what the procedure is for doing that, but I reckon a Web search would probably come up with something.

Holocron Coder
2012-07-06, 08:53 AM
How about using Hue, Saturation and Value rather than RGB? You can keep the Hue within a fairly tight range so you're not using completely different colours. You'd have to convert from HSV back to RGB to actually render the thing, of course--not sure what the procedure is for doing that, but I reckon a Web search would probably come up with something.

Hmm, that might be something I can look into. Hadn't thought of that. Thanks :smallbiggrin:

valadil
2012-07-06, 12:56 PM
I did a random color generator for my terminal windows a while back when I was bored. I quickly found that random (meaning 3 values between 0 and 255) didn't actually look good. It averaged out to muddy brown.

Instead I wanted bold vibrant colors. IIRC what I did was first determine how many color channels to randomize: 1, 2, or 3. When 1 channel was in use, red, green, or blue would have a random color. The other two channels would be 0 or 255.

End result was a lot of primary and secondary colors at varying brightnesses, but without dropping the possibility of a brown. Exactly what I wanted.

Holocron Coder
2012-07-09, 07:29 AM
I ended up using the HSV color format instead and randomized only one of the three for each region. The resulting usage is here (http://www.giantitp.com/forums/showthread.php?t=248886).