Results 1 to 7 of 7

Thread: how to find the Red, Green Blue ( RGB()) value of a color?

  1. #1

    Thread Starter
    Addicted Member Michel Jr's Avatar
    Join Date
    Jan 2000
    Location
    Brazil
    Posts
    175

    Question

    Hi,


    How can I extract the value of Red, Green and Blue
    that composes an specific color?

    Example:

    Suppose the variable vColor stores the color below:
    vColor = &H80000014

    How can I find the value of Red, Green and Blue that
    I should use in RGB(999,999,999) to produce the same color?


    Thanks

    Michel Jr.

  2. #2
    Guest
    Code:
    'Very useful code to convert long to rgb,
    rgb to long, rgb to hex and qbcolor to rgb
    
    Public Red As Integer
    Public Green As Integer
    Public Blue As Integer
    
    
    Public Sub ConvertQBColorToRGB(QBColorValue As Integer)
    
        If QBColorValue < 0 Or QBColorValue > 15 Then MsgBox "There's error in your
    QBColor.", vbCritical, "Error"
    
        ConvertLongToRGB (QBColor(QBColorValue))
    
    End Sub
    
    
    Public Function ConvertRGBToHex(RedColor As Integer, GreenColor As Integer,
    BlueColor As Integer)
    
        On Error GoTo ErrorInLongColor
    
        ConvertRGBToHex = Right(0 & Hex(RedColor), 2) & Right(0 & Hex(GreenColor),
    2) & Right(0 & Hex(BlueColor), 2)
        Exit Function
    
    ErrorInLongColor:
        MsgBox "There's error in your RGB color.", vbCritical, "Error"
    
    End Function
    
    
    Public Sub ConvertLongToRGB(Color As Long)
    
        On Error GoTo ErrorInLongColor
    
        Blue = Color \ 65536
        Green = (Color - Blue * 65536) \ 256
        Red = Color - (Blue * 65536) - (Green * 256)
    
        Exit Sub
    
    ErrorInLongColor:
        MsgBox "There's error in your long color.", vbCritical, "Error"
    
    End Sub
    
    
    Public Function ConvertRGBToLong(RedColor As Integer, GreenColor As Integer,
    BlueColor As Integer)
    
        On Error GoTo ErrorInLongColor
    
        ConvertRGBToLong = RGB(RedColor, GreenColor, BlueColor)
        Exit Function
    
    ErrorInLongColor:
        MsgBox "There's error in your RGB color.", vbCritical, "Error"
    
    End Function

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Talking Cheeky method...

    OK, not chheky exactly, but a bit different to Matthew's and with no error checking etc!

    in a module, declare the following
    Code:
    Public Type MyRGBColor  
      Red As Byte
      green As Byte
      blue As Byte
    End Type
    
    Public Type MyColor
      RGB As Long
    End Type
    Now, in any code where you want to "convert" from an RGB color to Long or vice versa, just do the following:

    Code:
    ' RGB to Long
      Dim mycol As MyColor
      Dim rgbcol As MyRGBColor
      rgbcol.red = 200
      rgbcol.green = 30
      rgbcol.blue = 220
      lset mycol = rgbcol 
      debug.print mycol.rgb
    
    ' Long to RGB
      Dim mycol As MyColor
      Dim rgbcol As MyRGBColor
      mycol.rgb = 14425800
      lset rgbcol = mycol
      debug.print "Use RGB(" & rgbcol.red & "," & rgbcol.green & "," & rgbcol.blue & ")"
    Of course, you could easily make error checking conversion functions to handle this for you, but this method is relying on the lset command to "convert" for us.

    Nice eh?

    Paul Lewis

  4. #4
    Guest
    I found a better code .

    Code:
    Public Enum RGBColor
        gcRed = 1
        gcBlue = 2
        gcGreen = 3
    End Enum
    
    Public Function GetRGBColor(ByVal Color As String, ColorPart As RGBColor) As Long
        Dim strColor As String
        Select Case ColorPart
            Case gcRed
                strColor = Right$("000000" & Hex$(Color), 6)
                GetRGBColor = Val("&h" & Right$(strColor, 2))
            Case gcBlue
                strColor = Right$("000000" & Hex$(Color), 6)
                GetRGBColor = Val("&h" & Left$(strColor, 2))
            Case gcGreen
                strColor = Right$("000000" & Hex$(Color), 6)
                GetRGBColor = Val("&h" & Mid$(strColor, 3, 2))
        End Select
    End Function

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Seems like you guys like to make it look complicated

    Code:
    Red=Color mod 256
    Green=int(Color/256) mod 256
    Blue=int(Color/&H10000)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Fanatic Member
    Join Date
    Oct 2005
    Posts
    586

    Re: Seems like you guys like to make it look complicated

    Quote Originally Posted by kedaman View Post
    Code:
    Red=Color mod 256
    Green=int(Color/256) mod 256
    Blue=int(Color/&H10000)
    OK that's genius. Thanks very much. Helped me with a control that uses 3 stupid RGB numbers rather than Hex.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: how to find the Red, Green Blue ( RGB()) value of a color?

    Darkbob, replying to a 2-decade old thread

    Anyway, this is better because it doesn't use the Mod() operator or Int() or non-integer division. It's more efficient/faster
    Code:
    Red = (Color And &HFF&)  ' take lower byte
    Green = (Color And &HFF00&) \ &H100&  ' shift next byte to lower & take that
    Blue = (Color And &HFF0000) \ &H10000  ' shift next byte to lower & take that
    When you print out a color in hex, keeping 6-8 characters, and then look at above code, you can see how simple the formula really is

    Edited: No offense towards kedaman (posted the code), that is probably the worst/slowest way to get the individual RGB values. Why? Using doubles throughout the formula (due to calculations) & doubles are slower than longs. Bitshifting is among the quickest operations as shown in my example. Additionally, and this doesn't apply with all applications, kedaman's formula fails miserably if an alpha byte above 127 exists in the color, i.e., this value for vbGreen with 100% opacity: &HFF00FF00
    Last edited by LaVolpe; Feb 2nd, 2019 at 10:44 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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