TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Language: Visual Basic
Expertise: Beginner
Mar 12, 1999

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Translate Color Values

With the RGB function, VB provides a neat and valuable tool for converting separate Red, Green, and Blue values into a single Long color value.. However, VB won't let you translate back from its this color value to back to its constituent RGB values. But, you can pick the individual colors out of a hexadecimal representation of the Long value produced by RGB. The colors fall in "BBGGRR" order. Put this code in a module:
``````
Type RGB_Type
R As Long
G As Long
B As Long
End Type

Function ToRGB(ByVal Color As Long) As RGB_Type
Dim ColorStr As String
ColorStr = Right\$("000000" & Hex\$(Color), 6)
With ToRGB
.R = Val("&h" & Right\$(ColorStr, 2))
.G = Val("&h" & Mid\$(ColorStr, 3, 2))
.B = Val("&h" & Left\$(ColorStr, 2))
End With
End Function
``````
To use this function, put a picture in a form's Picture property, and insert this code in that form:
``````
Private Sub Form_MouseUp(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
Dim RGB_Point As RGB_Type
RGB_Point = ToRGB(Point(X, Y))
Caption = RGB_Point.R & " " & RGB_Point.G & " " & _
RGB_Point.B
End Sub
``````
Click on different places on the picture. VB3 users must return the values differently, because VB didn't support the return of a user-defined type until VB4.
Brian Donovan

 Submit a Tip Browse "XML" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date