C++Learn C++

Learn to Generate Beautiful Color Gradients in C++ Builder

How we can generate our own color gradient as simple as possible? In this post we will explain how to use Color Gradients in Modern C++, in C++ Builder.

Colors are very important in application development on both displaying and editing/analyzing operations. All images (pictures, photos, drawings, icons, emojis, all UI elements …) are consist of pixels in colors. You just need to change the colors of a pixel to draw a beautiful drawing or to edit a photo. You can set your drawings, bitmaps, images, you can create colorful BMP, JPG, PNG pictures, you can edit or analyze photos, you can analyze videos or camera buffer images in real-time operations. C++ Builder is very good and faster on these pixel operations in Colors. This feature is very important on dynamic operations to reduce time of analyze or edition.

In our previous posts explained well about Colors In Modern C++ For Windows Development, we also explained Color Components Quick Look At Basic Color Components On Windows In C++ Builder (Part 6), and we explained using Alpha Color in Bitmaps in this Learn To Create Alpha Color Bitmap From A Bitmap By A Given Color In Modern C++ post. If you want to learn more about colors please check these posts.

Color Gradients are changes in colors like spectrum of prism in a light or rainbow, and they help to level your shapes. Gradients may give a 3rd dimension to your 2D objects. Sometimes they are good to understand how sea level is there, where has the higher values in a view, or what is the temperature that point, what is the pressure distribution etc. Gradients also helps to generate stress maps, to display magnitude of vectors, to display changes, differences etc. in views.

TAlphaColorRec
TAlphaColorRec provides access to the color channels record. You can get colors by these variables you can read or write each ARGB values in this variable. By changing Alpha parameter you can make your photos semi transparent by its value.

Some Examples to ARGB colors
0x00000000 Fully transparent black
0x88000000 Half transparent black
0xFF000000 Black
0xFFFFFFFF White

  • If the highest-order byte is zero, then the low three bytes represent RGB color intensities for blue, green, and red, respectively. The value $00FF0000 (Delphi) or 0x00FF0000 (C++) represents full-intensity, pure blue, $0000FF00 (Delphi) or 0x0000FF00 (C++) is pure green, and $000000FF (Delphi) or 0x000000FF (C++) is pure red. $00000000 (Delphi) or 0x00000000 (C++) is black and $00FFFFFF (Delphi) or 0x00FFFFFF (C++) is white.
  • If the highest-order byte is $FF (the SystemColor constant), then the low three bytes represent Windows system colors like SysWindow or SysMenu. These constants for system colors work only under Windows platforms.

    http://docwiki.embarcadero.com/Libraries/Sydney/en/System.UITypes.TColor

With these information, we can create a spectrum array spec[] and we can set each A,R,G,B values of each gradient as below,

Here is a full example to use this simple gradient.

You can extend this spec[] spectrum array to size of 512 or 1024 and you can add more colors.


Did you know you can download a free trial of C++ Builder? Why not try it out today?

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish