Sign in to follow this  

Mapping scalars to RGB

This topic is 2830 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);
G=max(0,(4-fabs(g-2)-fabs(g-4))/2);
B=max(0,(3-fabs(g-1)-fabs(g-2))/2);
}


[EDIT]
This link might also be of interest:
http://local.wasp.uwa.edu.au/~pbourke/texture_colour/colourramp/

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

This topic is 2830 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this