Click to See Complete Forum and Search --> : radial gradient


lakgani
May 2nd, 2008, 02:39 AM
hi all,
I need to create a radial color gradient. I have colors(rgba) and corresponding ratios(values between 0-255) to do that. but i dont know how to create a radial gradient using this information. I am developing this in C language.

is there any algorithm to do this.
Im completely new to 2d graphics.
please anybody guide me on this issue.

thank you

Cybrax
May 2nd, 2008, 03:28 AM
http://support.microsoft.com/kb/128637

Syslock
May 3rd, 2008, 08:11 AM
http://support.microsoft.com/kb/128637

That draws a linear gradient, not a radial one.

For each pixel, calculate its distance from the center pixel, and use that as a percentage of the gradient color delta. So color would be:

pixel color = d1/d2 * gradient color delta,

where d1 is center - pixel,
d2 is center - min(w,h) of bitmap dimensions,
and gradient color delta is (r2-r1) or (g2-g1) or (b2-b1) of your gradient.

srelu
May 5th, 2008, 07:00 AM
First of all, there is no API support for what you want to do.

If the image is very small you can go on the way pointed by Syslock. (Well, you have your own ratios, so you can skip the ratio computing.)
If the bitmap is larger, C cannot offer you a reasonable algorithm regarding the time of execution.

If you want only one or a few bitmaps, my suggestion is to try to use a graphic editor to create them, then just display them using BitBlt. This is not a suitable technique if your ratios are nonstandard and their acuracy is important.

If the shape and size of the image is always the same, you can write a C program to generate one single bitmap with 256 colors - enough for a two color gradient. It will be painfully slow, but you will execute it only once. After that, having the bitmap, for different colors you just modify the bitmap's color table. Having only 256 entries, the color table can be processed in a reasonable amount of time.

If none of those solutions can satisfy you, the only way to obtain the bitmap in a reasonable amount of time is the assembly language. This is the technique used by the major graphic editors. You don't need to code everything in that language, just the pixel by pixel processing loop.

Create a bitmap using the CreateDIBSection, that will allow you direct access to the memory where the bitmap's bytes are. You'll need to understand how the bitmap is represented in the memory (the color format is crucial).

You don't need to know full Assembler language. It's enough to understand the basics about the registers, how can you address the memory and access its content, and a few commands: mov, inc, loop, add, sub, div, mul, or, and. That's all you need to know, nothing more.

Well, if you never used them, it will take several days to understand those things, maybe even more, but I think it worths the effort. After that you'll be able to process almost any pixel by pixel graphic operation in an acceptable amount of time.

The Assembler language is d*mn fast, it's the native language of the processor and will allow you an incredible increase in the speed of the algorithm. Depending on what are you doing it can be hundreds or thousand of times faster than C. It accesses directly the processor and the memory. Using it, you bypass all the sophisticated but lenghty and slow Windows procedures.