How do I write a COLORREF colour to a BMP file?

A real NEWBIE question....

I have been colouring a Mandelbrot (and writing to a BMP file) with the following routine....

1
2
3
4
5
6
7
8
	double t = (double)i / (double)maxIterations;//Fraction of escape iteration to maximum iteration
	r = (int)(9 * (1 - t)*t*t*t * 255);
	g = (int)(15 * (1 - t)*(1 - t)*t*t * 255);// Use smooth polynomials for r, g, b from Solarian Programmer
	b = (int)(8.5*(1 - t)*(1 - t)*(1 - t)*t * 255);
	SetPixel(hdc, x, y, RGB(r, g, b));//bye passing the wrapper
	outppm.write(&b, 1);
	outppm.write(&g, 1);//Write it BGR 
	outppm.write(&r, 1);


which has worked for a while. I found another routine on the 'net that is much more colourful. It uses an array of COLORREFs. The result is much more appealing but I can't write the resultant COLORREF to the BMP file.

1
2
3
4
5
6
        // Which color gradient is used to color mandelbrot in wikipedia?
	if (i < maxIterations && i > 0) {
		color = mapping[i % 16];
	} else color = BLACK;
	SetPixel(hdc, x, y, color);
        outppm.write(&color, 3);//<<< fails to compile   :( 


Help please. How does a newbie write the COLORREF "color" to the BMP file?

Pointers/References etc scare the **** out of me :(
Thanks for your response Thomas1965. In another project I will(can) do what your link suggests :) but for the moment my project is committed to saving a pixel at a time. It would be MUCH more efficient writing the whole screen in one "go". The screen has all sorts of statistics displayed on it so I don't want to save the "hdc" at the moment.

As you can see, the original saves the BGR bytes from the variables "b", "g" and "r". I just want to know how to save from the COLORREF variable "color". Is there any way I can reference the RGB elements on the COLORREF variable? Seeing I have to flip the order too?

But thanks for your response :)
On further research I found A solution. It is not the one I hoped for, to "write" straight from the COLORREF variable "color" but it serves to get my program working again.

For anyone else who would like to know, the solution is the use of macros GetBValue, GetGValue and GetRValue to re-extract the BGR components from the "color" variable. Thus:-
1
2
3
4
5
6
7
	SetPixel(hdc, x, y, color);
	r = GetRValue(color);
	g = GetGValue(color);
	b = GetBValue(color);
	outppm.write(&b, 1);
	outppm.write(&g, 1);//Write it BGR 
	outppm.write(&r, 1);


Thanks for your attention :)
Topic archived. No new replies allowed.