News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Generating gradients programmatically

Started by NoCforMe, August 27, 2023, 02:50:22 PM

Previous topic - Next topic

NoCforMe

This might be useful to somebody: a simply way to generate gradients that can be used as window backgrounds or for other graphic purposes. Demo program attached. The top part shows windows of various sizes with the same gradient to see how that appears.

The nice thing is that all these can be created without any fancy-schmancy techniques, no lookup tables, just looping through a 256-entry list of RGB values. (The code to render the gradient in a window uses a very small number of FPU instructions, otherwise very straightforward.)
Assembly language programming should be fun. That's why I do it.

Biterider

Hi NoCforMe
Really nice and useful! 
Looking through the code, I noticed the good coding style.
 
Thank you for sharing  :thumbsup:

Biterider

fearless

Here is an example using ModernUI library. The background gradient is painted with a function called MUIGDIPaintGradient, which itself is a wrapper for GradientFill which is located in the msimg32.lib. The dwColorRefGradientFrom and dwColorRefGradientTo COLORREF values can be changed and recompiled or alternatively they could be updated at runtime to change the background on the fly. The orientation of the gradient can be adjusted: horizontal or vertical.

This site has some more information on gradients: http://www.flounder.com/gradientfiller.htm - but the app requires some older mfc dll to run.

I dont know if there is a way to change the angle of the gradient, i guess using the third vertex (from looking at that flounder site) is the main way, or perhaps some transform operation to rotate the dc being painted to before the blt call?

I included the ModernUI include and lib for the example.



You cannot view this attachment.

NoCforMe

New version attached to original post. Added 3 colors, colors now arranged in more or less rainbow order. (Remember "Roy G. Biv"?)

Bonus feature: park your cursor over the combobox and spin your mouse wheel, watch the colors change. This was not intentional on my part ...
Assembly language programming should be fun. That's why I do it.

NoCforMe

#4
It's pretty easy generating 45º gradients. Same basic technique (256-element gradient list), just with a couple more loops. Probably you could generate other angles using a little trig.

I was a little surprised that the result was as smooth as it is; I expected maybe some blotches or other artifacts. You can see some very faint diagonal striping, but overall it looks pretty damn good. Apparently LineTo() is a pretty well worked-out piece of code.
Assembly language programming should be fun. That's why I do it.

daydreamer

Quote from: NoCforMe on August 28, 2023, 12:51:12 PMIt's pretty easy generating 45º gradients. Same basic technique (256-element gradient list), just with a couple more loops. Probably you could generate other angles using a little trig.
great NoCforMe :thumbsup:
with fpu its simple using fsincos to generate gradient any angle
isometric view is simplified sine 30 degrees=0.5 :easy bitshift on old slow nonfpu hardware
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

LiaoMi