Results 1 to 8 of 8

Thread: RGB question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    shiraz
    Posts
    163

    Question RGB question

    Hi guys
    Well I need to find out the darkness or brightness of each single pixle of a given image file in my app. To do this I use a picturebox, I load the image into it then I get each pixel’s color using point method. It returns a long RGB number which is the result of merging red, green and blue I guess. 4 example if the point is RGB(192,255,12) it returns 192255012. but there is one problem:
    When it is RGB(0,255,0)(pure green) it returns 225
    When it is RGB(0,0,255) (pure blue) it returns 225 also
    So u see there’s no difference in green and blue using this. So I decided to anlaize these rgb colors. For example I have a point now I want to find out how much red is in it? How much green and how much blue? Do u know how to this?
    Thanks aloot

  2. #2
    AIS_DK
    Guest
    This will do it

    VB Code:
    1. lngRed = (lngColor And &HFF&)
    2. lngGreen = (lngColor And &HFF00&) / &H100
    3. lngBlue = (lngColor And &HFF0000) / &H10000

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    shiraz
    Posts
    163
    can u explain a little more please?
    thank u

  4. #4
    AIS_DK
    Guest
    Weel, I'm not sure what else I can say about the code, it is rather self explaning.

    But here goes:
    lngColor is a colorcode ie. 192255012
    lngRed is the value of red in the RGB colorcode
    lngGreen is the value of green in the RGB colorcode
    lngBlue is the value of blue in the RGB colorcode

    VB Code:
    1. lngRed = (lngColor And &HFF&) 'lngRed is now the value of red
    2. lngGreen = (lngColor And &HFF00&) / &H100 'lngGreen is now the value of green
    3. lngBlue = (lngColor And &HFF0000) / &H10000 'lngBlue is now the value of blue

    lngRed, Green and Blue are just varibles, in which I store the value, you can choose your own varibles, debug it or whatever.

    The code just does some bitwise operations on the number, and retrieves the bits representing the value of the Red Green and Blue value.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    shiraz
    Posts
    163
    u know i'm a little confused with the format of the codelngRed = 4 example i can't see any functions or stuff. i'm looking 4 a way to analize the valum that point method returns to me (lngColor And &HFF&)
    hey it seems i'm getting old at 20

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    OK. I implemented the code that AIS_DK provided into a simple to use function. Add the following code to your Form
    VB Code:
    1. Public Enum eRGBcolor
    2.     rgbRed = 1
    3.     rgbGreen
    4.     rgbBlue
    5. End Enum
    6.    
    7. Private Function GetColorValue(nColor As Long, nReturnColor As eRGBcolor) As Integer
    8.     Select Case nReturnColor
    9.         Case rgbRed
    10.             GetColorValue = (nColor And &HFF&)
    11.         Case rgbGreen
    12.             GetColorValue = (nColor And &HFF00&) / &H100
    13.         Case rgbBlue
    14.             GetColorValue = (nColor And &HFF0000) / &H10000
    15.         Case Else
    16.             Err.Raise vbObjectError + 512, "GetColorValue", "Incorrect color for return value"
    17.     End Select
    18. End Function
    Now to get, for example, the value of red at the position 1, 1 in a picturebox named Picture1 you'll use code simular to this:
    VB Code:
    1. MsgBox GetColorValue(Picture1.Point(1, 1), rgbRed)
    Best regards

  7. #7
    AIS_DK
    Guest
    It's not a function. It's just 3 calculations, that's it.

    So how about this then.

    VB Code:
    1. Dim lngPointColor as long
    2.  
    3. lngPointColor = Me.PictureBox1.Point(X_Coor, Y_Coor)
    4.  
    5. Debug.print "Amount of Red in Coordinate: " & (lngPointColor And &HFF&)
    6. Debug.print "Amount of Green in Coordinate: " & (lngPointColor And &HFF00&) / &H100
    7. Debug.print "Amount of Blue in Coordinate: " & (lngPointColor And &HFF0000) / &H10000

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    shiraz
    Posts
    163

    Thumbs up thanks

    thanks i'll try it

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