[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1207
  • Last Modified:

Convert CommonDialog.color to RGB format?


Was just playing with the Common dialog box color picker. I noticed that it returns the color code in some other form rather than RGB.

Ex. Selecting 'Brown" color returned "4210816". Can I convert this value to RGB form?
  • 3
1 Solution
sramesh2kAuthor Commented:
Now reading this doc: http://www.vb-helper.com/tut10.htm
Public Function ColorCodeToRGB(lColorCode As Long, iRed As Integer, iGreen As Integer, iBlue As Integer) As Boolean
' 1996/01/16 Return the individual colors for lColorCode.
' 1996/07/15 Use Tip 171: Determining RGB Color Values, MSDN July 1996.
' Enter with:
'     lColorCode contains the color to be converted
' Return:
'     iRed    contains the red component
'     iGreen  the green component
'     iBlue   the blue component
    Dim lColor As Long
    lColor = lColorCode      'work long
    iRed = lColor Mod &H100  'get red component
    lColor = lColor \ &H100  'divide
    iGreen = lColor Mod &H100 'get green component
    lColor = lColor \ &H100  'divide
    iBlue = lColor Mod &H100 'get blue component

    ColorCodeToRGB = True
End Function

Source: http://www.buygold.net/v02n04/v02n04.html
sramesh2kAuthor Commented:
Thanks. But VB does not seem to display the value in Long format. I selected the 1st color (reddish) and get this output : 8421631


Text1.Text = cdlgColor.Color
sramesh2kAuthor Commented:
Got this resolved: This function helped

Sub ConverttoRGB()
  R = C And 255              ' Get lowest 8 bits  - Red
  G = Int(C / 256) And 255   ' Get middle 8 bits  - Green
  B = Int(C / 65536) And 255 ' Get highest 8 bits - Blue
  out = R & "  " & G & "   " & B
End Sub


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now