Click here to Skip to main content
15,886,805 members
Articles / Desktop Programming / MFC
Article

CGradientRender

Rate me:
Please Sign up or sign in to vote.
3.75/5 (25 votes)
17 Aug 2005CPOL2 min read 49.1K   2K   47   10
An article on drawing gradients using the CGradientRender class.

 

 

 

 

Introduction

Most programmers used Windows SDK function GradientFill to draw horizontal or vertical linear gradients or triangle gradients. Also, classes for diagonal gradients can be found on CodeProject. But what about custom gradients? Or experimental gradients? In this article, a class for drawing such gradients is presented. Its functionality is not hard-tested or free of bugs, so please have that in mind when using it.

Background

One can find various articles on gradients all over the Internet and very good examples on CodeProject also.

Using the code

To use the CGradientRender class, include the provided header file "GradientRender.h" and make an instance of this class.

#include "GradientRender.h"

CGradientRender gradientRender;

Now, this class has only one public method called DrawGradient, so call this method like in the example below:

// hDC is a destination device context (i.e. screen device context)

RECT rect = {100, 100, 300, 300};     // gradient rectangle
COLORREF startColor = RGB(0,0,255);   // start gradient color
COLORREF endColor = RGB(255,0,0);     // end gradient color
gradientRender.DrawGradient(hDC,rect,startColor, 
       endColor,GRADIENT_HORIZONTAL,TRANSFORMATION_NONE);

The last two arguments are the type of the gradient and the type of the transformation. If the type of the transformation is TRANSFORMATION_NONE then this class can perform drawing of five basic gradient types:

  • Horizontal gradient (GRADIENT_HORIZONTAL)
  • Vertical gradient (GRADIENT_VERTICAL)
  • Forward diagonal gradient (GRADIENT_FDIAGONAL)
  • Backward diagonal gradient (GRADIENT_BDIAGONAL)
  • Radial gradient (GRADIENT_RADIAL)

Also, using some type of transformation, the results can vary. This is the field of customization and experimenting. Current transformations are listed below:

  • No transformation (TRANSFORMATION_NONE)
  • Caricature transformation (TRANSFORMATION_CHARICATURE)
  • Fisheye transformation (TRANSFORMATION_FISHEYE)
  • Swirled transformation (TRANSFORMATION_SWIRLED)
  • Cylinder transformation (TRANSFORMATION_CYLINDER)
  • Shift transformation (TRANSFORMATION_SHIFT)

In basic, these six transformations are performed on five different types of gradients. There are 30 combinations for now. Some good, some even better. But, one must experiment with the parameters of the transformation in order to get custom results, or add a new transformation which would be the best. All transformations are performed in the private method ApplyTransformation of the CGradientRender class.

Points of Interest

Working on this material, I found an interesting research field. Almost any image transformation can be applied here (even color transformation which is not tested here) and everyone is invited to try.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

Comments and Discussions

 
GeneralMy vote of 4 Pin
futurejo26-Apr-13 17:59
futurejo26-Apr-13 17:59 
GeneralMy vote of 5 Pin
kanbang12-Apr-12 22:34
kanbang12-Apr-12 22:34 
GeneralMy vote of 5 Pin
VinhTran_1-Dec-11 14:58
VinhTran_1-Dec-11 14:58 
GeneralMy vote of 5 Pin
Ceccli29-Mar-11 0:20
Ceccli29-Mar-11 0:20 
GeneralBug and workaround Pin
Ed The C16-Jul-09 6:41
Ed The C16-Jul-09 6:41 
There's a problem with ApplyTransformation:

the atan calculation
fi = atan(double(i-height/2)/double(j-width/2));
generates an undefined fi when j==width/2. Thats a zero divide! Oddly the sample works as is, and even give a valid value for fi, but in my app, I would get undefined fi.

Do not use atan2 as a fix - its range of the angle return is different than atan itself. My simple work around was to use the previous fi value, even tho there's a slight err it should not be noticeable.

if (fabs(double(j-width/2)) > 0.000001)
fi = atan(double(i-height/2)/double(j-width/2));

Ed
GeneralDid this work in 16 bit mode Pin
Vinicius Jarina7-Mar-07 9:35
Vinicius Jarina7-Mar-07 9:35 
GeneralDiffusion Point Pin
theali6-Mar-07 19:12
theali6-Mar-07 19:12 
QuestionHOW DO LIKE THIS Pin
HOW WHAT6-Dec-05 4:45
HOW WHAT6-Dec-05 4:45 
GeneralWell done! Pin
Dandy Cheung17-Aug-05 21:23
Dandy Cheung17-Aug-05 21:23 
GeneralGood start... Pin
Paul Selormey17-Aug-05 1:46
Paul Selormey17-Aug-05 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.