Jump to content
  • Advertisement
Sign in to follow this  

Mapping scalars to RGB

This topic is 3047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm writing a simple tutorial about rendering the Mandelbrot fractal. Instead than using the standard Escape Time Algorithm to decide wich color to assign to the pixel, I use the variant that evaluates a scalar, floating point value. I don't know a nice method to map a [0,1] value to an RGB so that I get nice shades on my images. I would like to avoid color space conversion if possible (HSV could be a good space but I want to keep it as simple as I can). Currenly I tried a couple of ways. The last is the following:
                double idx = val * 3;

                int r = (int)(Math.Abs(Clamp(idx, 0, 1) - 0.5) * 255);
                int g = (int)(Math.Abs(Clamp(idx - 1, 0, 1) - 0.5) * 255);
                int b = (int)(Math.Abs(Clamp(idx - 2, 0, 1) - 0.5) * 255);

I hoped it to result in nice gradients, but it is mostly grey. Anyone has any good idea? Thank you! EDIT: fixed the code.

Share this post

Link to post
Share on other sites
I do this a slightly different way in my visualization apps.
Instead of creating a lookup function to compute the value
I use a 1D texture. The mapping [0,1] serves as your texture
co-ordinate :)

You can then just download a selection of color-ramps or create,
some in photoshop, instead of having to write functions for each one.
(I could also provide you with my set of textures if you wanted)

If you don't want to do it that way, here is some code which
gives a rainbow color table, you might have to modify some
part slightly but it should give you the idea:

void c(float f, float & R, float & G, float &B)
const float dx=0.8
f=(f<0) ? 0: (f>1)? 1 : f //clamp f in [0,1]
g=(6-2*dx)*f+dx //scale f to [dx, 6-dx]
R=max(0, (3-fabs(g-4)-fabs(g-5))/2);

This link might also be of interest:

Share this post

Link to post
Share on other sites
Thank you. Using your function the colors are mostly around the mandelbrot border, and most of the space is blue. Perhaps there is some way to scale colors non linearly, but I wait until the zoom works, perhaps colors are just good as they are now.

Thank you for the link too!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!