Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Using a union to retrieve the RGB components of a color

Total Hit ( 3036)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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.

Click here to copy the following block
<StructLayout(LayoutKind.Explicit)> Structure ARGBColor
 <FieldOffset(0)> Dim Red As Byte
 <FieldOffset(1)> Dim Green As Byte
 <FieldOffset(2)> Dim Blue As Byte
 <FieldOffset(3)> Dim Alpha As Byte
 <FieldOffset(0)> Dim Value As Integer
End 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:

Click here to copy the following block
' Split a color into its components.
Dim rgb As ARGBColor
rgb.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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.