Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [GDK] Gradient class

Author
Message
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 12th Mar 2008 23:57 Edited at: 12th Mar 2008 23:59
The other day I was writing some code to create a rainbow when I realized that it would look a lot better if the color transition were gradiated and not so banded. So I wrote some code to create a gradiated box and gradiated arcs. I came to realize that I was having to repeat much of the same code for different types of things and decided to create a class that generates the gradient for me. That led to the code below.

dbGradient.h:


dbGradient.cpp



The code is predicated on the concept that there's always a starting color and an ending color with a finite number of points between them, whether they be the radius or arcs or lines in a rectangle. You begin by declaring the dbGradient object. You can also set the operating parameters at the same time as you declare the object or you can use the Start method later to assign the starting color, the ending color and the span (number of points to cover.)

dbGradient Grad;

or

dbGradient Grad (dbRed, dbGreen, 512);


In order to retrieve each successive output color in the stream you call the Next() method

int nextColor = Grad.Next();

In order to check to see if there are any more colors in the output steam you use the Still() method. It returns a true/false result depending on whether or not you reached the end of the span.



The beauty of this method is that you can use it in a number of different situations and by judicious calculation of the span passed to the object use the object to control your loops.

I've also included an Indexed(int i) method that lets you ask for a specific point in the series if you need to be creative.

Any attempt to give dbGradient a negative number for the span will simply set the span to 1. Any attempt to exceed the span limit or provide a negative index will result in dbGradient returning the color black.

I suspect I may add a Reset() method that resets the counter to zero without resetting the colors.

Of course the above depends on having set a default bitmap.

I'll try to provide some more specific code in a later post.

Lilith, Night Butterfly
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 27th Apr 2008 06:18
Cool, Lilith.

I AM THE ALL MIGHTY CORNHOLIO! Do you have TP for my bunghole?
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 5th May 2008 21:21
Nice Lilith.. (pondering if I should try this against my own.... different approach, probably different results... could be interesting!)

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 5th May 2008 22:55 Edited at: 5th May 2008 22:56
What approach do you use? I started reading an article on codeproject.com about gradients and it looks like there are some other considerations that might have to use to modify my approach because gradients aren't perceptually linear or because a linear approach to it doesn't look right.

I did something weird with this a couple of weeks ago. I created a horizontally gradiated box with green on one end and red on the other. I created three gradient objects, one to vary the color from end to end and the other two to vary the colors at the ends. So the left end graduated from green to yellow while the right end graduated from red to blue over time with the middle portion gradiating between whatever the colors on the ends happened to be at the time. Nothing spectacular but maybe an interesting concept for a background somewhere.

FWIW, I'm thinking in terms of writing something similar that plots the points for drawing a circle or, more precisely, an arc between d1 and d2 degrees with a float for a radius. I have the basics down but I'm trying to figure how to speed it up by limiting the and y values over which it calculates. I discovered that you can't have a closed circle by using degrees unless you take small fractions of degrees, which can't be known in advance.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-11-25 02:33:18
Your offset time is: 2024-11-25 02:33:18