Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Mar 12, 1999



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 & " " & _
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
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