Click here to Skip to main content
Licence 
First Posted 17 Aug 2005
Views 33,049
Bookmarked 42 times

CGradientRender

By | 17 Aug 2005 | Article
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

darkoman

Software Developer (Senior)
Elektromehanika d.o.o. Nis
Serbia Serbia

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberkanbang22:34 12 Apr '12  
GeneralMy vote of 5 PinmemberMember 780004014:58 1 Dec '11  
GeneralMy vote of 5 PinmemberMember 77946470:20 29 Mar '11  
GeneralBug and workaround PinmemberEd The C6:41 16 Jul '09  
GeneralDid this work in 16 bit mode PinmemberVinicius Jarina9:35 7 Mar '07  
GeneralDiffusion Point Pinmembertheali19:12 6 Mar '07  
QuestionHOW DO LIKE THIS PinmemberHOW WHAT4:45 6 Dec '05  
GeneralWell done! PinmemberDandy Cheung21:23 17 Aug '05  
GeneralGood start... PinmemberPaul Selormey1:46 17 Aug '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 Aug 2005
Article Copyright 2005 by darkoman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid