Results 1 to 6 of 6

Thread: RGB, etc.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    81

    Cool

    Now then, I know how to convert from RGB to long by using RGB(Red As Integer, Green As Integer, Blue As Integer), but how can I convert from a long value back to RGB?

    It's puzzled me for a while and the only way I can think to do it is like this:

    Code:
    Dim redVal As Integer, greenVal As Integer, blueVal As Integer
    Dim lngColour As Long
    For redVal = 0 To 255
       For greenVal = 0 To 255
          For blueVal = 0 To 255
          If RGB(redVal, greenVal, blueVal) = lngColour Then
             'the RGB version of lngColour is redVal, greenVal, blueVal
             Exit Sub
          Else:
             'wrong colour, keep trying
          End If
          Next blueVal
       Next greenVal
    Next redVal
    This will work, but surely there's an easier way of doing it - this loops through 16 and a half million colours, and checks each one, and my computer crashes every time I try. I need to find it at the click of a mouse button, otherwise the purpose of the application is defied.

    Anybody?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I answered this here:

    http://forums.vb-world.net/showthrea...threadid=27064

    although you may need to swap the blue and red around in the function, because it parses BGR values from the API.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    there are several ways of doing it. i can think of two, but this is my favorite:
    Code:
    Sub Value2RGB(color As Long, r As Long, g As Long, b As Long)
        Dim a As String
        
        a = Hex(color)
        a = String(6 - Len(a), "0") + a
        r = Val("&h" + Right(a, 2))
        g = Val("&h" + Mid(a, 3, 2))
        b = Val("&h" + Left(a, 2))
    End Sub
    you call it like this
    Code:
    Value2RGB color, r, g, b
    'r, g, and b now contain the value of red, green, and blue (0 to 255)

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    agent - I don't like that method. Mine is safer, and considerably faster, because there is no need for string conversions - it's all done on the number, with integer arithmetic.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    When I need RGB conversion I wrote my own code for it, pretty much the same as agent's and it worked perfectly.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes, but if you do it a lot, it really slows it down. Anyway, it's best to use the integer manipulations just from a tidiness point.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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