Results 1 to 19 of 19

Thread: How can you get the color at a certain point on the screen?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    How can you get the color at a certain point on the screen?

    How can i specify a X and Y coordinate on the screen and determine what color is there?

  2. #2
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    VB Code:
    1. Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

    Now, if you want the RGB values, use this function:
    VB Code:
    1. Sub obtainRGB(cValue, varRed, varGreen, varBlue)
    2.  
    3. varBlue = Int(cValue / 65536)
    4. cValue = cValue Mod 65536
    5. varGreen = Int(cValue / 256)
    6. cValue = cValue Mod 256
    7. varRed = cValue
    8.  
    9. End Sub

    Pass the value you get from the GetPixel function, then the variable to store the red value in, then green variable, then blue.

    Hope this helps,
    cjqp
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Probably a bunch of APIs... I guess you'll need the following APIs :

    GetForegroundWindow or GetActiveWindow
    GetDC
    GetPixel


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    Originally posted by manavo11
    Probably a bunch of APIs... I guess you'll need the following APIs :

    GetForegroundWindow or GetActiveWindow
    GetDC
    GetPixel
    I don't mean to sound rude but, did you actually read his post? He wants to know how to get a color from a pixel, not from a certain window.

    cjqp
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I read it... I've never tried that though so I don't know exactly what is needed... I don't mean to sound rude either, but with the GetPixel API you posted, what is supposed to be passed as the hdc? That's why I thought of the GetActiveWindow API... On second thought maybe the GetDesktopWindow would work...


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    Ahhh, right, forgot about needing the hDC, yeah, I'd go with GetDesktopWindow for that.

    cjqp
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    4. Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    5. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, _
    6.                                                ByVal x As Long, _
    7.                                                ByVal y As Long) _
    8.                                                As Long
    9.  
    10. Private Type POINTAPI
    11.     x As Long
    12.     y As Long
    13. End Type
    14.  
    15. Private Type RGBA
    16.     r As Byte
    17.     g As Byte
    18.     b As Byte
    19.     a As Byte
    20. End Type
    21.  
    22. Private Type RGBL
    23.     rgb As Long
    24. End Type
    25.  
    26. Private Sub Timer1_Timer()
    27. Dim clr As Long
    28. Dim rgb As RGBA
    29. Dim t   As RGBL
    30. Dim p   As POINTAPI
    31.    
    32.     Call GetCursorPos(p)
    33.    
    34.     clr = GetPixel(GetWindowDC(0&), p.x, p.y)
    35.    
    36.     t.rgb = clr
    37.     LSet rgb = t
    38.  
    39.     Me.BackColor = clr
    40.     Me.Caption = "R:" & rgb.r & ", G:" & rgb.g & ", B:" & rgb.b
    41.  
    42. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Why do you declare a?

    VB Code:
    1. Private Type RGBA
    2.     r As Byte
    3.     g As Byte
    4.     b As Byte
    5.     a As Byte
    6. End Type


    Has someone helped you? Then you can Rate their helpful post.

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Why not?

    Helps me to better remember what's what.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    But what does it do? From what I know, R is for Red, G for Green and B for Blue... A is for what?


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    A is for the alpha.

    Its the alpha bit.. Ha ha, get it?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  12. #12
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    *rolls eyes*, anyway, blade, where in your code does it separate the values??

    cjqp
    Last edited by cjqp; Mar 31st, 2004 at 06:05 PM.
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  13. #13
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Right here...

    Code:
        t.rgb = clr
        LSet rgb = t
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  14. #14
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    But how the heck does that split? There is no Property Let or the like that would split it.

    cjqp
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  15. #15
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by cjqp
    But how the heck does that split? There is no Property Let or the like that would split it.

    cjqp
    In VB, you can exchange data in compatible UDTs. A Long is 4 bytes, so if you have one Type with a Long, and one with 4 Bytes, you can break that Long into its individual Bytes.

    Red byte
    Green byte
    Blue byte
    Alpha byte


    See it?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  16. #16
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    ahhh, tyvm. Up until your example, my code was by far the shortest for this type of thing.

    cjqp
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  17. #17
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by crptcblade
    Ha ha, get it?
    No

    I never knew of the Alpha part


    Has someone helped you? Then you can Rate their helpful post.

  18. #18
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I suppose technically you don't need it as part of the Type, since it should still just copy over the first 3 bytes anyway, but again, its good to know what's what.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  19. #19
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by crptcblade
    I suppose technically you don't need it as part of the Type, since it should still just copy over the first 3 bytes anyway, but again, its good to know what's what.
    OK. Good to know... I think...


    Has someone helped you? Then you can Rate their helpful post.

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