# Mapping scalars to RGB

## Recommended Posts

cignox1    735
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 on other sites
_Unicron_    438
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 :)

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 on other sites
cignox1    735
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!