CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    Convert system color to RGB

    How can I convert a color constant (such as vbWindowBackground) to RGB? The method I'm now using (see below) only works with colors retrieved from a CommonDialog color dialog selector.

    Code:
    RVal = Int(lSourceColor Mod 256)
    GVal = Int((lSourceColor \ 256) Mod 256)
    BVal = lSourceColor \ 65536
    But this doesn't work with when lSourceColor is a system color constant. Do I have to convert the color to another format before using that RGB function? How?
    Last edited by Andrex; December 25th, 2006 at 12:02 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Convert system color to RGB

    Take a look at this. I've included the source so that you don't have to make the form.
    Code:
    Option Explicit
    
    Private Type HSL
    	Hue As Long
    	Saturation As Long
    	Luminance As Long
    End Type
    
    Private Function HSLToRGB(ByVal Hue As Long, ByVal Saturation As Long, ByVal Luminance As Long) As Long
    Dim R As Long, G As Long, B As Long
    Dim lMax As Long, lMid As Long, lMin As Long
    Dim Delta As Single
    lMax = (Luminance * 255) / 100
    lMin = (100 - Saturation) * lMax / 100
    Delta = (lMax - lMin) / 60
    Select Case Hue
    Case 0 To 60
    	lMid = (Hue - 0) * Delta + lMin
    	R = lMax: G = lMid: B = lMin
    Case 60 To 120
    	lMid = -(Hue - 120) * Delta + lMin
    	R = lMid: G = lMax: B = lMin
    Case 120 To 180
    	lMid = (Hue - 120) * Delta + lMin
    	R = lMin: G = lMax: B = lMid
    Case 180 To 240
    	lMid = -(Hue - 240) * Delta + lMin
    	R = lMin: G = lMid: B = lMax
    Case 240 To 300
    	lMid = (Hue - 240) * Delta + lMin
    	R = lMid: G = lMin: B = lMax
    Case 300 To 360
    	lMid = -(Hue - 360) * Delta + lMin
    	R = lMax: G = lMin: B = lMid
    End Select
    HSLToRGB = B * &H10000 + G * &H100& + R
    End Function
    
    Private Function RGBToHSL(ByVal RGBValue As Long) As HSL
    Dim R As Long, G As Long, B As Long
    Dim lMax As Long, lMin As Long
    Dim Delta As Single
    R = RGBValue And &HFF
    G = (RGBValue And &HFF00&) \ &H100&
    B = (RGBValue And &HFF0000) \ &H10000
    If R > G Then
    	lMax = R: lMin = G
    Else
    	lMax = G: lMin = R
    End If
    If B > lMax Then
    	lMax = B
    ElseIf B < lMin Then
    	lMin = B
    End If
    RGBToHSL.Luminance = lMax * 100 / 255
    If lMax > lMin Then
    	RGBToHSL.Saturation = (lMax - lMin) * 100 / lMax
    	Delta = 60 / (lMax - lMin)
    	Select Case lMax
    	Case R
    	If B > G Then
    		RGBToHSL.Hue = Delta * (G - B) + 360
    	Else
    		RGBToHSL.Hue = Delta * (G - B)
    	End If
    	Case G
    		RGBToHSL.Hue = Delta * (B - R) + 120
    	Case B
    		RGBToHSL.Hue = Delta * (R - G) + 240
    	End Select
    End If
    End Function
    
    Private Sub cmdGo_Click()
      Dim Color As HSL
      Color = RGBToHSL(Val("&H" & txtRGB.Text))
      For Color.Hue = 0 To 49
    	  Picture1(Color.Hue).BackColor = HSLToRGB(Color.Hue, Color.Saturation, Color.Luminance)
      Next Color.Hue
    End Sub
    
    Sub form_load()
      Dim colour As Long, red As Byte, green As Byte, blue As Byte
      Dim str As String
    	 red = 11
    	 green = 22
    	 blue = 33
      
      str = "Red : " & red & vbCrLf
      str = str & "Green : " & green & vbCrLf
      str = str & "Blue : " & blue & vbCrLf
      MsgBox str
      colour = RGB(red, green, blue)
      blue = (colour And &HFF0000) \ &H10000
      green = (colour And &HFF00&) \ &H100&
      red = colour And &HFF
      str = "Red : " & red & vbCrLf
      str = str & "Green : " & green & vbCrLf
      str = str & "Blue : " & blue & vbCrLf
      MsgBox str
      txtRGB.Text = RGB(100, 100, 100)  ' colour
    End Sub
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Convert system color to RGB

    I don't think it's necessary to convert from HSL coordinates.

    I think the problem was that the OP is using modular division instead of shift-followed-by-AND:

    Code:
    colRed = lSourceColor  And &HFF
    colGreen = (lSourceColor  / &H100) And &HFF
    colBlue = (lSourceColor  / &H10000) And &HFF
    See http://www.codeguru.com/forum/showthread.php?t=402310

    Mike

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Convert system color to RGB

    That's why I posted this:
    Code:
     colour = RGB(red, green, blue)
      blue = (colour And &HFF0000) \ &H10000
      green = (colour And &HFF00&) \ &H100&
      red = colour And &HFF
      str = "Red : " & red & vbCrLf
      str = str & "Green : " & green & vbCrLf
      str = str & "Blue : " & blue & vbCrLf
    MsgBox str
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    Re: Convert system color to RGB

    Thanks, but it seems I didn't explain myself correctly. Neither of the functions posted worked.

    Let's suppose I have a system color constant such as vbWindowBackground that I want to convert to RGB. All I need is a function to decompose this color to get the values:

    Red=255
    Green=255
    Blue=255

    which equals the white background of Windows' objects.

    Thanks in advance (and merry christmas, )
    Last edited by Andrex; December 25th, 2006 at 03:51 PM.

  6. #6
    Join Date
    Oct 2006
    Posts
    327

    Re: Convert system color to RGB

    So ?

    Colour is a long which you can express by a long or by a vbcolour or RGB(r,g,b) as you whish !

    The valid range for a normal RGB color is 0 to 16,777,215 (&HFFFFFF). The high byte of a number in this range equals 0; the lower 3 bytes, from least to most significant byte, determine the amount of red, green, and blue, respectively. The red, green, and blue components are each represented by a number between 0 and 255 (&HFF). If the high byte isn't 0, Visual Basic uses the system colors, as defined in the user's Control Panel settings and by constants listed in the Visual Basic (VB) object library in the Object Browser.
    edit : you may also use the TranslateColor function of the olepro32.dll library
    Last edited by moa; December 25th, 2006 at 04:06 PM.

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Convert system color to RGB

    System colors (like vbWindowBackground) are stored differently.

    See "Title: RGB values for vbWindowBackground, vbButtonFace" at http://www.experts-exchange.com/Prog..._10205021.html

    Mike
    Last edited by MikeAThon; December 25th, 2006 at 04:37 PM.

  8. #8
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    Re: Convert system color to RGB

    Quote Originally Posted by MikeAThon
    System colors (like vbWindowBackground) are stored differently.

    See "Title: RGB values for vbWindowBackground, vbButtonFace" at http://www.experts-exchange.com/Prog..._10205021.html

    Mike
    Thanks, I finally made it work using that function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured