ACC2000: How to Get Red, Green, Blue Components from RGB Value
ID: Q210420
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).
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().
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, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals 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 needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the
following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
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:
- Open the sample database Northwind.mdb.
- Create a module and type the following line in the Declarations
section if it is not already there:
Option Explicit
- 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
- Save the module as MyUtilities.
- On the Run menu, click Run Sub/UserForm.
- 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.
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.
Additional query words:
inf video screen
Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto