Normal Distribution of Colors

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

ZeroTheConfused
Posts: 6
Joined: Mon Apr 22, 2013 10:25 pm UTC

Normal Distribution of Colors

Postby ZeroTheConfused » Mon Apr 22, 2013 11:06 pm UTC

So, I didn't see any welcome topic, so I hope I'm clear to post straight off.

Anyways, I have an..interesting? question.
I'm thinking of making a game, and one of the things I am wanting to do is when a new world is generated, some values are randomly generated. One of those is hopefully
the color of the foliage of the world. If I wanted to do that quickly, though, I could just use Math.Random and be done with it... What I'm trying to do however is to create a bell curve probability thingy (Aka Normal/Gaussian Distribution) That will have a weighted chance towards green, meaning that other colors will be able to be chosen, but it would be far more likely to select shades of green.

I've looked around the internet and wikipedia and there are a few examples (And lods of statistics stuff), but they are just for single numbers; I need rgb values, or HSB, and hex seems to just be rgb smushed into hex.
Which leads me here, because I have absolutely no idea how i would go about doing this. Knowing me, If i tried three different distributions for each part, I'd get horribly mutilated colors not at all what would want.

So if you've ever done anything similar or have any advice on where to look further/see examples, I'd love to hear it.
Sincerely, A confused Zero.







P.S. While thinking of a topic name, I thought about making a color map picture with the distribution i want and selecting two coordinate values

User avatar
phlip
Restorer of Worlds
Posts: 7569
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Normal Distribution of Colors

Postby phlip » Tue Apr 23, 2013 5:44 am UTC

I imagine it would be simple enough to just generate the values for the three channels separately, each with their own normal distribution, with different means... Pick the mean value for each channel to be the sort of "regular green" colour you want the middle of your distribution to be. So, if the mean green that you want is, say, (20%, 70%, 10%) or whatever, then you'd just do (<random number with mean of 20%>, <random number with mean of 70%>, <random number with mean of 10%>).

Should work fine for a reasonably small variance. If your variance is larger, it'll be more relevant which colourspace you're using... whether you're using RGB or HSV or YPbPr or whatever. Also what the variance should be for each channel. All I can recommend is trying a bunch of different options and seeing how they look.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Normal Distribution of Colors

Postby troyp » Tue Apr 23, 2013 7:07 am UTC

HSV might work out better than RGB. That (1) gives you direct control of lightness and saturation and (2) should make it easy to weight colours towards a particular green. Since hues are arranged in a circle, just create a normal distribution with a mean at the green hue you want and whatever variance you like. Then apply modulo 360 to the resulting value. If you try to do it with RGB, overflows will be more messy because the colour dimensions aren't cyclic. Also, HSV is just easier to visualize without having to mix coloured lights in your head.

User avatar
PM 2Ring
Posts: 3701
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Normal Distribution of Colors

Postby PM 2Ring » Tue Apr 23, 2013 7:22 am UTC

I guess it depends on what you're intending to do with this foliage colour. If it's just for drawing the whole planet as seen from a distance, then a single colour may be adequate, but for close views you probably want a range of colours.

Anyway, I suggest using a hue-based colour system, eg HSV. Generate your base colour by choosing a normally-distributed hue at maximum saturation and value / brightness. Then you can generate shades of that colour by reducing the saturation and/or value.

If the hue parameter is in degrees, then pure green has a hue of 120, pure yellow is 60, pure cyan is 180. So if you make 120 the mean and use a sigma (standard deviation) of 60, then about 68% of the colours will fall in the hue range from yellow through green to cyan. I'd suggest playing around with sigma in the range of 30 to 90, to see what looks good.

Depending on how you convert HSV to RGB, you may need to ensure that your generated hue numbers stay in the proper range. Some libraries tolerate hues larger than 360° or less than 0°, but not all of them do.

BTW, what language are you doing this in?

ZeroTheConfused
Posts: 6
Joined: Mon Apr 22, 2013 10:25 pm UTC

Re: Normal Distribution of Colors

Postby ZeroTheConfused » Wed Apr 24, 2013 12:47 am UTC

As far as just generating three normal distributions for R, G, and B, I have been thinking it would be a pain in the rear in to figure out precisely where I need each one to be to get said variance to be *green* and not have an annoying tendency to be some shade of vomit or something.

HSV I suppose would allow me to get the hues easily, and then randomized saturation or something would be okayish. I suppose I should actually go and try instead of sitting here wasting my time.

And another choice i was thinking of is that I could make a color "map" (Like the ones you see when using a color picker for say, paint, or gimp, or something) that contains ever color, but to essentially duplicate the green area(s). This would give a larger chance to the green values being selected versus other colors, essentially giving me a...jerryrigged normal distribution thingy xD. Sure, manual, but Paint.Net, bars of color and then blurs give me the same color map. Enough that it works anyways lol. Maybe.

I won't know until I go try this out.


As far as a language goes,
Spoiler:
I've not quite decided that, this is more of concept stage stuff right now. (Prettyfying things should always be last, But this is something I'd consider important. And something that could be changed to any language easily.)
But I'll probably be doing it either in java or a C variant. (In school, learned some Python,(And taking Java based CompuSci) but Python, while excellent to program with, just runs physically slower than lower languages, which while trivial with most things, is an important factor. I believe I read that Notch, creator of minecraft, has occasionally remarked Java was a poor choice to make the game with because of perfermance issues.(Even if Python DOES have built in normal and gaussian distribution for it's random function) (Ironic that a "sixteen bit" texture-ized game of blocks coul take so much RAM)
Java is what I'm most familiar with, So I might do that. But if I do decide to change my mind, C, afaik, looks /functions/reads a LOT like Java, that, afaik, quite a bit should be able to run the same verbatim. Who knows.

DeGuerre
Posts: 51
Joined: Mon Feb 04, 2008 6:41 am UTC

Re: Normal Distribution of Colors

Postby DeGuerre » Wed Apr 24, 2013 1:14 am UTC

This is just a suggestion, but you might want to consider taking a cue from master painters, and use a gamut of the colour wheel.

The idea is to work in HSV space, and in particular look at the H-S plane in polar coordinates. Drop a polygon in that space, and choose colours randomly from inside that polygon. Choosing a uniform random point from within a triangle or quadrilateral is a very well-studied problem.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Normal Distribution of Colors

Postby Steax » Thu Apr 25, 2013 1:20 am UTC

I've done something like this before, and I went with a more pragmatic approach: I started with picking 30 colors of feasible "leaf" colors from across the spectrum - including weirder colors, but avoiding colors I intentionally didn't want to be an option. Then I applied weights to ensure the greener ones got picked more often. Then, after a random color has been picked, I apply a random hue/saturation/lightness function on the color to come up with my final color.

It's further away from true randomness, but it gives me more control.
In Minecraft, I use the username Rirez.

User avatar
Xanthir
My HERO!!!
Posts: 5400
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Normal Distribution of Colors

Postby Xanthir » Fri Apr 26, 2013 3:59 am UTC

Steax wrote:I've done something like this before, and I went with a more pragmatic approach: I started with picking 30 colors of feasible "leaf" colors from across the spectrum - including weirder colors, but avoiding colors I intentionally didn't want to be an option. Then I applied weights to ensure the greener ones got picked more often. Then, after a random color has been picked, I apply a random hue/saturation/lightness function on the color to come up with my final color.

It's further away from true randomness, but it gives me more control.

Do this. It'll give you better results. (For one thing, green is the most common, but the less-common colors tend toward the red/yellow, rather than the blue. Using a symmetrical normal distribution isn't useful for this.)

Others are right, though - you need to be operating in a different color space, specifically one where the hue is a single parameter. HSL or one of the many variants of that would work.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests