Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Get Red, Green, Blue Components from RGB Value


View products that this article applies to.

Summary

This article shows you how to create a sample user-defined function, GetRGB(), that returns the red, green, or blue component from an RGB color value. Note that this behavior is the opposite of the Visual Basic for Applications function RGB().

↑ Back to the top


More information

RGB color values are mathematical combinations of red, green, and blue components. The intensities of these components can range from 0 to 255. For example, the RGB color value for white is 16777215, which has component intensities of 255 for red, green, and blue.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example demonstrates how to use the GetRGB() function to return the RGB color components from the RGB color value in the Order form's detail section BackColor property:
  1. Open the sample database Northwind.mdb.
  2. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  3. Type the following procedures:
    '-----------------------------------------------------------------
    'PURPOSE: Returns red/green/blue color from RGB color value.
    '
    'ACCEPTS: RGB color value as Long, and component number as integer
    '         that represents the component color to return (1=red,
    '         2=green, 3=blue).
    '
    'RETURNS: The intensity of the color component (0 - 255) as an
    '         integer or -1 indicating that an argument was invalid.
    '-----------------------------------------------------------------
    
    Function GetRGB(RGBval As Long, Num As Integer) As Integer
       ' Check if Num, RGBval are valid.
       If Num > 0 And Num < 4 And RGBval > -1 And RGBval < 16777216 Then
         GetRGB = RGBval \ 256 ^ (Num - 1) And 255
       Else
         ' Return True (-1) if Num or RGBval are invalid.
         GetRGB = True
       End If
    End Function
    
    '-----------------------------------------------------------------
    'PURPOSE: Open the Orders form in Form view, call the GetRGB
    '         function, and then switch the Orders form to Design view.
    '
    'ACCEPTS: RGB color value as Long, and component number as integer
    '         that represents the component color to return (1=red,
    '         2=green, 3=blue).
    '
    'RETURNS: Displays a message box with the RGB components
    '         of the detail section of the Orders form.
    '-----------------------------------------------------------------
    
    Sub TestOrders()
    
       DoCmd.OpenForm "Orders", acNormal
       
       MsgBox "Red: " & GetRGB(Forms![Orders].Section(0).BackColor, 1) & _
              vbCrLf & _
              "Green: " &  GetRGB(Forms![Orders].Section(0).BackColor, 2) & _
              vbCrLf & _
              "Blue: " &   GetRGB(Forms![Orders].Section(0).BackColor, 3)
    
       DoCmd.OpenForm "Orders", acDesign
       
    End Sub
    					
  4. Save the module as MyUtilities.
  5. On the Run menu, click Run Sub/UserForm.
  6. From the Database window, click TestOrders, and then click Run.

    Note that you can change the BackColor property of the detail section of the Orders form, and then run the macro again to see the results change.

↑ Back to the top


References

For more information about the RGB function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type RGB function in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB210420, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 210420
Revision : 5
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 346