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

Last reviewed: August 29, 1997
Article ID: Q112061
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes a sample user-defined Visual Basic for Applications 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().

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.

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.

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 (or NWIND.MDB in versions 1.x and 2.0).

  2. Create a module and type the following line in the Declarations section:

          Option Explicit
    

  3. Type the following procedure:

          '-----------------------------------------------------------------
          '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
    
    

  4. Save the module as MyUtilities, and then close it.

  5. Create the following macro with the actions listed:

          Macro Name  Action   Comment
          ---------------------------------------------
          GetRGB      MsgBox   Display red component
                      MsgBox   Display green component
                      MsgBox   Display blue component
    
          GetRGB Actions
          -------------------------------------------------------------------
          MsgBox:
            Message: =GetRGB(Forms![Orders].Section(0).Backcolor,1) & ": Red"
          MsgBox:
            Message: =GetRGB(Forms![Orders].Section(0).Backcolor,2) & ": Green"
          MsgBox:
            Message: =GetRGB(Forms![Orders].Section(0).Backcolor,3) & ": Blue"
    
    

  6. Save the macro as GetRGB, and then close it.

  7. Open the Orders form in Form view.

  8. From the Database window, run the GetRGB macro. 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.

REFERENCES

For more information about the RGB() function, search the Help Index for "RGB Function."


Additional query words: video screen
Keywords : kbprg PgmHowTo
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.