• Log in or sign up for an account here: https://www.sythe.org/login

[SOURCE] Simple Find Pixel Color [/SOURCE]

Darthatron

Massive Troll
Joined
May 22, 2006
Sythe Gold
0
Messages
1,612
Age
35
Location
The internet.
[SOURCE] Simple Find Pixel Color [/SOURCE]
Alright, this is a VERY SIMPLE find pixel color function. It is slow and shiet, but hey, whatever floats your boat.

Add this to General Declarations...
Code:
Option Explicit

Private Type PointApi
    X As Long
    Y As Long
End Type

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointApi) As Long
Private Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
        
Dim PosXY As PointApi
Dim Retval As Long
Dim tPosXY As PointApi
Dim sTemp As String
Dim lColor As Long
Dim lDC As Long
Dim iX, iY As Integer
Dim ColorPixel As Long

To actually get the pixel you want to look for use this code... Put it in an Image_MouseUp or something.
Code:
    lDC = GetWindowDC(0)
    Call GetCursorPos(tPosXY)
    lColor = GetPixel(lDC, tPosXY.X, tPosXY.Y)
    Picture1.BackColor = lColor

This is the Function that will search the screen for that Pixel Color...
Code:
Function FindPixelColor(Color As Long)
    If Color > -1 And Color < 16777216 Then 'Checks if the Color is a Color or not.
        For iY = 0 To Screen.Height 'Gets the screens height.
            For iX = 0 To Screen.Width 'Gets the screens width.
                ColorPixel = GetPixel(lDC, iX, iY) 'Gets the pixel color at location.
                If Color = ColorPixel Then 'Checks if the colors match.
                        SetCursorPos Val(iX), Val(iY) 'Change this to a MoveMouseSmooth function, or whatever...
                    Exit Function 'Exits the Function.
                End If
            Next iX 'Tries the next X location.
        Next iY 'Starts again at the next Y location.
        MsgBox "The color was not found.", vbOKOnly, "Error."
    Else
        MsgBox "That color is invalid.", vbOKOnly, "Error."
    End If
End Function

You could edit that code to return the values of the X and Y values if you know how. Use it well.
 
[SOURCE] Simple Find Pixel Color [/SOURCE]
There are so many things you can do to make that code cleaner and run faster.
 
[SOURCE] Simple Find Pixel Color [/SOURCE]
It is slow and shiet
Ok, here's what you need to do to fix that. Use LONG TYPE variables for your For loops! It's that simple.

Declare them both as LONG variables and say goodbye to slow and shitty!:D
Code:
Dim iX As Long, iY As Long
 
[SOURCE] Simple Find Pixel Color [/SOURCE]
I know its bad guys, but I was tired and bored. (Sleep is for the weak.)
 
[SOURCE] Simple Find Pixel Color [/SOURCE]
Sausage.
 
[SOURCE] Simple Find Pixel Color [/SOURCE]
if u want to make findcolorTolerance i could help ..

Basically its just math counting the color (example: color 255)

Dim Red As Extended

Red = Color / 255;
Green = Color / 255 / 255;
Blue = Color / 255 / 255;

Im not entirely sure about this but.. Something like that..

How to check tolerance?
Basically its just some math

if u didnt understand i can give u some code..
 
[SOURCE] Simple Find Pixel Color [/SOURCE]
Thanks Jukka, but I already know how do Color Tolerance. =)

@TroubleSleep: Dude... really... don't spam... GTFO.
 
Runescape Gamblingrunescape gambling
Back
Top