Microsoft KB Archive/112061

From BetaArchive Wiki
Knowledge Base


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

Article ID: 112061

Article Last Modified on 1/18/2007



APPLIES TO

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition



This article was previously published under Q112061

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

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().

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 if it is not already there:

    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: kbhowto kbprogramming KB112061