![pexels𝐕𝐞𝐧𝐮𝐬𝐇𝐃𝐌𝐚𝐤𝐞𝐮𝐩𝐏𝐞𝐫𝐟𝐮𝐦𝐞1749452 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/pexels-𝐕𝐞𝐧𝐮𝐬-𝐇𝐃-𝐌𝐚𝐤𝐞-𝐮𝐩-𝐏𝐞𝐫𝐟𝐮𝐦𝐞-1749452-scaled.jpg?resize=1140%2C694&ssl=1)
![pexels𝐕𝐞𝐧𝐮𝐬𝐇𝐃𝐌𝐚𝐤𝐞𝐮𝐩𝐏𝐞𝐫𝐟𝐮𝐦𝐞1749452 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/pexels-𝐕𝐞𝐧𝐮𝐬-𝐇𝐃-𝐌𝐚𝐤𝐞-𝐮𝐩-𝐏𝐞𝐫𝐟𝐮𝐦𝐞-1749452-scaled.jpg?resize=1140%2C694&ssl=1)
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 realtime 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 this post we present how to use Colors in Modern C++.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
TAlphaColorRec acr; // setting each of ARGB AlphaColorRec parameters acr.A = 255; // 0..255 0 is full transparent, 255 is full solid, acr.R = 255; // 0..255 255 is full RED acr.G = G/n; // 0..255 255 is full GREEN acr.B = B/n; // 0..255 255 is full BLUE // getting each of ARGB AlphaColorRec parameters unsigned short int A,R,G, B; A= acr.A; // copy alpha value to A R= acr.R; // copy RED value to R G= acr.G; // copy GREEN value to G B= acr.B; // copy BLUE value to B |
![Colors | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/Colors.png?resize=343%2C108&ssl=1)
![Colors | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/Colors.png?resize=343%2C108&ssl=1)
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) or0x00FF0000
(C++) represents full-intensity, pure blue,$0000FF00
(Delphi) or0x0000FF00
(C++) is pure green, and$000000FF
(Delphi) or0x000000FF
(C++) is pure red.$00000000
(Delphi) or0x00000000
(C++) is black and$00FFFFFF
(Delphi) or0x00FFFFFF
(C++) is white. - If the highest-order byte is
$FF
(theSystemColor
constant), then the low three bytes represent Windows system colors likeSysWindow
orSysMenu
. These constants for system colors work only under Windows platforms.
http://docwiki.embarcadero.com/Libraries/Sydney/en/System.UITypes.TColor
TColorRec
The TColorRec type defines useful constants identifying different types of colors for TColor. Some of these constants map directly to the closest matching color in the system palette (for example, the Blue
constant maps to the blue color). You can see the actual colors themselves in the Colors table in TColorRec.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TColorRec cr; // setting each of RGB ColorRec parameters cr.R = 255; // 0..255 255 is full RED cr.G = G/n; // 0..255 255 is full GREEN cr.B = B/n; // 0..255 255 is full BLUE // getting each of RGB ColorRec parameters unsigned short int A,R,G, B; R= cr.R; // copy RED value to R G= cr.G; // copy GREEN value to G B= cr.B; // copy BLUE value to B |
Using Colors in Bitmaps
This example shows how to read pixel colors in ARGB format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
TBitmapData bitmapData; unsigned long int R=0,G=0,B=0; TAlphaColorRec acr; int x,y,n=0; if( Image1->Bitmap->Map(0, bitmapData)) // Lock bitmap and retrive bitmap data { for (y=0; y<Image1->Bitmap->Height; y+=1) { for (x=0; x<Image1->Bitmap->Width; x+=1) { acr.Color = bmpData.GetPixel( x, y); R+=acr.R; G+=acr.G; B+=acr.B; n++; } } Image1->Bitmap->Unmap(bitmapData); acr.R = R/n; acr.G = G/n; acr.B = B/n; acr.A = 255; } |
TColor
In C++ Builder TColor is used to specify the color of a control. It is used by the Color property of many components and by a number of other properties that specify color values. Color is generally a property of components. You can specify TColor as a specific 4-byte hexadecimal number instead of using the constants defined in the TColorRec type:
1 2 3 4 5 6 |
TAlphaColorRec acr; acr.Color =Form1->Fill->Color; if(acr.Color==claBlack) ShowMessage(L"It is full black ARGB"); |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition