How To Convert a 32 Bit Integer into a Color

How To Convert a 32 Bit Integer into a Color








How To Convert a 32 Bit integer into a color

Since a 32 bit integer consumes exactly 4 bytes and an ARGB color is also exactly 4 bytes, every 32 bit integer represents a color. This article will outline how to convert those integers into colors.

*As a statement of clarification, this article is aware of the Color.FromArgb(value as Integer) method, this article, however is intended to explain on a lower level how to convert a 32 bit integer into a color.

Creating a method to convert integers into colors

Using a .Net language of your choice, you can follow these steps to create a Function that will convert 32 bit integers into colors. *This example will be given in Visual Basic.Net

  • Declare a Function named "IntegerToColor"
    • a. This function should have the following:
      • A single 32 bit integer parameter named "RGB"
      • A return type of System.Drawing.Color
      • So far you should have something that looks like this:*
      • 1.Function IntegerToColor(ByRef RGB As Int32) As System.Drawing.Color
        2.  
        3.End Function
  • The first line of code we will add to this will utilize the BitConverter class to convert the Int32 into an array of Byte
      • To do this, pass the RGB Int32 argument to BitConverter.GetBytes method, and assign the output of that method to an array of byte
      • You should now have something that looks like this:
      • 1.Function IntegerToColor(ByRef RGB As Int32) As System.Drawing.Color
        2.    Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
        3.End Function
  • An interesting point to make is that regardless of the character length of the Int32, the byte count will always be the same. This is why the 32 bit number 1 can still represent a color that contains 4 values. This can be deceptive to people at first glance, but I assure you that even the 32 bit number 0 represents a color(Black).
  • Now that we have retrieved the bytes of that Int32, we want to seperate them into something a bit more understandable to our eyes. Lets create 4 byte variables named the following: Alpha, Red, Green, Blue
  • Because of the endianness of an Int32, the bytes that correspond to the Alpha, Red, Green, and Blue bytes will be stored in the array, in reversed order. How do I know its in reversed order? Because you say RGB... Red, Blue, Green is the order, and alpha comes first, so its ARGB. But the Alpha byte is not stored at the first index in the array, it's stored at the 4th index! Red at the third, green at the second, and blue is stored at the first. So now that we know which index the byte is stored at, we can assign that byte to each one of our variables. We also do not need to check the length of the array, because we know that if 1 byte is 8 bits long, then a 32 bit integer has 4 bytes, always.
    • Your code should look something like this now:*
    • 1.Function IntegerToColor(ByRef RGB As Int32) As System.Drawing.Color
      2.    Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
      3.    Dim Alpha As Byte = Bytes(3)
      4.    Dim Red As Byte = Bytes(2)
      5.    Dim Green As Byte = Bytes(1)
      6.    Dim Blue As Byte = Bytes(0)
      7.End Function
  • So we have now isolated the 4 bytes from the Int32 into 4 byte variables called Alpha,Red,Green,Blue. Our function can now return the result of the System.Drawing.Color.FromArgb Function. This is the final line we should add to our function:
      • 1.Return Color.FromArgb(Alpha, Red, Green, Blue)
  • Our final function should look like this:
    • 1.Function IntegerToColor(ByRef RGB As Int32) As System.Drawing.Color
      2.    Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
      3.    Dim Alpha As Byte = Bytes(3)
      4.    Dim Red As Byte = Bytes(2)
      5.    Dim Green As Byte = Bytes(1)
      6.    Dim Blue As Byte = Bytes(0)
      7.    Return Color.FromArgb(Alpha, Red, Green, Blue)
      8.End Function
  • Please note: Not all controls support transparency(the alpha component). Observe the following example that throws a " Control does not support transparent background colors." error.
      • 01.Option Strict On
        02.Public Class Form1
        03.    Function IntegerToColor(ByRef RGB As Int32) As System.Drawing.Color
        04.        Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
        05.        Dim Alpha As Byte = Bytes(3)
        06.        Dim Red As Byte = Bytes(2)
        07.        Dim Green As Byte = Bytes(1)
        08.        Dim Blue As Byte = Bytes(0)
        09.        Return Color.FromArgb(Alpha, Red, Green, Blue)
        10.    End Function
        11.    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        12.        Dim RGB As Integer = -2130771968 'Alpha is 128
        13.        Dim MyColor As Color = IntegerToColor(RGB)
        14.        Me.BackColor = MyColor
        15.    End Sub
        16.End Class
  • You can get around this problem by setting the color's Alpha property to 255(0% transparency), see the same example now, with alpha set to 255, does not cause an error.
    • 01.Option Strict On
      02.Public Class Form1
      03.    Function IntegerToColor(ByRef RGB As Int32) As System.Drawing.Color
      04.        Dim Bytes As Byte() = BitConverter.GetBytes(RGB)
      05.        Dim Alpha As Byte = Bytes(3)
      06.        Dim Red As Byte = Bytes(2)
      07.        Dim Green As Byte = Bytes(1)
      08.        Dim Blue As Byte = Bytes(0)
      09.        Return Color.FromArgb(Alpha, Red, Green, Blue)
      10.    End Function
      11.    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      12.        Dim RGB As Integer = -2130771968 'Alpha is 128
      13.        Dim MyColor As Color = IntegerToColor(RGB)
      14.        If Not Me.GetStyle(ControlStyles.SupportsTransparentBackColor) Then
      15.            MyColor = Color.FromArgb(255, MyColor)
      16.        End If
      17.        Me.BackColor = MyColor
      18.    End Sub
      19.End Class
  • This time, we checked to see if the control supported transparent backcolors, and since it didn't, we changed the alpha component, effectively removing transparency.
Thanks for reading, I hope you enjoyed.

Please view my other wiki articles!
Please update this article if you see any mistakes.

See Also

An important place to find a huge amount of Visual Basic related articles is the TechNet Wiki itself. The best entry point is
 Visual Basic Resources on the TechNet Wiki



browser and hit stats
Joomla Counter

Sort by: Published Date | Most Recent | Most Useful
Comments
  • Paul,

    This is obviously written for a lay person, The bit manipulation could be speed up if it weren't written for new programmers.

  • I was wondering if there is a reason you have used ByRef instead of ByVal in your Function IntegerToColor?

    In the final code snippet regarding line14. I reckon it should read as follows:>>

    If Not Button1.GetStyle(ControlStyles.SupportsTransparentBackColor) Then

    as the "Me" you have used refers to Form1.

  • Sorry, please ignore that part of my last comment regarding "Me"

    as you are setting the BackColor of the FORM itself.

    That will make me re-read things two or three times in future or when I am less tired.

  • Congratulations for the TechNet Guru Medal

    social.technet.microsoft.com/.../23367.technet-guru-silver-winner-contributions.aspx

Page 1 of 1 (4 items)