CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2002
    Posts
    1,684

    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 ?
    Attached Images    
    mpliam

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,852

    Re: How to draw pixel-level color gradients in CScrollView ?

    Quote Originally Posted by Mike Pliam View Post
    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 View Post
    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?
    Last edited by D_Drmmr; October 7th, 2012 at 12:22 PM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width