Using a union to retrieve the RGB components of a color

Using a union to retrieve the RGB components of a color

In VB.NET you can create what in C is called a union, i.e. a particular structure where you can access the same memory with different names. Here’s how you declare such a structure by using the StructLayout attribute, and specifying that you want to define the structure layout explicitly, namely how the fields will be saved in memory within the type.

 Structure ARGBColor   Dim Red As Byte   Dim Green As Byte   Dim Blue As Byte   Dim Alpha As Byte   Dim Value As IntegerEnd Structure

If you set the Value field, you’re actually setting all the 4 bytes of the structure in a single step. Otherwise you can get and set the single bytes of the structure with the other fields. Here’s how you can set the Value field of a variable of type ARGBColor to an hexadecimal value that represents a color, and then read the other fields to retrieve its RGB components:

' Split a color into its components.Dim rgb As ARGBColorrgb.Value = &H112233         ' This is equal to 1122867.Console.WriteLine(String.Format("Red={0}, Green={1}, Blue={2}", rgb.Red, _    rgb.Green, rgb.Blue))    ' => Red=51, Green=34, Blue=17

————————–
Note: this code is taken from Francesco Balena’s Programming Microsoft Visual Basic .NET book (MS Press 2002)

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes