devxlogo

Translate Color Values

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 LongEnd TypeFunction 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 WithEnd 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.BEnd 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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist