# How to draw pixel-level color gradients in CScrollView ?

• October 6th, 2012, 11:29 PM
Mike Pliam
How to draw pixel-level color gradients in CScrollView ?
I have a very general question. I wish to plot a mathematical function on a 2-dimensional grid. The function will be represented by 3 sets of floats, x-values, y-values, and z-values. Obviously, it is impossible to plot 3 such data sets on a 2D grid, so one approach is to use pixel coloration to represent the z-values.

Those examples (and there are many to be found) show something like the function of the complex variable z, f(z) = exp(z). I have attached a exp(z).jpg example of the imaginary part of the plot exp(z).

My question is: How to accomplish such a gradient colorization, assuming one already has the data?

I have experimented with Gdiplus SetPixel, but I have not figured out how to produce a smooth gradient. Obviously, I'm in way over my head, but I had hoped someone might lead me in the right direction.

Here's my attempt to create a simple gradient:
Code:

```        CDC * pDC = GetDC();         Graphics graphics(pDC->m_hDC);         CRect r;         GetWindowRect(&r);         ScreenToClient(&r);         HWND hwnd = GetSafeHwnd();         CDC * pdc = GetDC();         HDC hdc = ::GetDC(hwnd);         COLORREF cr = RGB(192, 192, 192);         for(int x = 10; x <= 600; x++)         {                 for(int y = 10; y <= 400; y++)                 {                         SetPixelV(hdc, x, y, cr);                         cr += 1;                 }         }```
You can see my lame result also attached mygrad.jpg. Your thoughts greatly appreciated. How would you do this ?
• October 7th, 2012, 12:20 PM
D_Drmmr
Re: How to draw pixel-level color gradients in CScrollView ?
Quote:

Originally Posted by Mike Pliam
My question is: How to accomplish such a gradient colorization, assuming one already has the data?

You have to create a legend that associates certain values with certain colors and then interpolate intermediate values. Easiest way to interpolate colors is by just taking a linear combination of the red, green and blue values.
Quote:

Originally Posted by Mike Pliam
I have experimented with Gdiplus SetPixel, but I have not figured out how to produce a smooth gradient. Obviously, I'm in way over my head, but I had hoped someone might lead me in the right direction.

Besides the fact that you don't release the DCs, your use of SetPixelV seems ok, though that function can be slow. It's faster to assign to a bitmap of a memory DC and then bitblit to the window DC.
However, what is "cr += 1;" supposed to accomplish?

I'd suggest to forget about the graph and focus on the first problem first: how to assign a color to a value?