Jump to content

Finding a certain color


kbrown
 Share

Recommended Posts

I was wondering if anyone knew of a good way to find a known color at a known pixel. I am very new to autoit and scripting and I am trying to write a program for a game bot. It is not really a script as this program will be pretty big. I chose to use autoit as it is easy to understand and you do not need an expensive editor or compiler to use. My question is I am looking for a certain color at known pixel locations within the game. The colors I am looking for are exactly Red, Green, Blue, and Black. Is there an easy way to determine if these colors exist in a known location? I could not find a Autoit function that did it, nor could I find an UDF that did what I wanted to do. Pixelchecksum wasnt what I was looking for either. I wrote a couple functions that will do what I want, but was just wondering if i was missing something. Is this functionality built into autoIt and I just missed it? Anyway, here is the code that I wrote. Any suggestions to help with my coding and improve the code would be greatly appreciated. Please remember, I am just beginning to use autoit. My formatting got changed around when i Posted the code. :whistle:

CODE
;----------------------------------------------------------------------------------------

;

; Description : Determine if a color is Red

; Syntax : _IsRed($Color, $Shade)

; Parameter(s) : $Color - Color to be checked in decimal

; : $Shade - Amount that color can be off and still be considered Red

; Requirement(s) : None

; Return Value(s) : On Success - Returns 1

; On Failure - Returns -1

; Author(s) : Keith Brown

; Note(s) : $Shade should be set a value between 0 and 150, a value of 0 means

; that the color is exactly Red, i.e. its hex value is FF0000

;

;----------------------------------------------------------------------------------------

;

Func _IsRed($Color, $Shade)

;

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT

$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT

$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

;

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

;

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT

$ColorGreen = _ColorGetGreen($Color) ; GET THE GREEN COMPONENT

$ColorBlue = _ColorGetBlue($Color) ; GET THE BLUE COMPONENT

;

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

;

If $ColorRed >= (255 - $Shade) AND $ColorGreen <= $Shade AND $ColorBlue <= $Shade Then

Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED RED

Else

Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED RED

EndIf

;

EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

;

;

;----------------------------------------------------------------------------------------

;

; Description : Determine if a color is Blue

; Syntax : _IsGreen($Color, $Shade)

; Parameter(s) : $Color - Color to be checked in decimal

; : $Shade - Amount that color can be off and still be considered Blue

; Requirement(s) : None

; Return Value(s) : On Success - Returns 1

; On Failure - Returns -1

; Author(s) : Keith Brown

; Note(s) : $Shade should be set a value between 0 and 150, a value of 0 means

; that the color is exactly Blue, i.e. its hex value is 0000FF

;

;----------------------------------------------------------------------------------------

;

Func _IsBlue($Color, $Shade)

;

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT

$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT

$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

;

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

;

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT

$ColorGreen = _ColorGetGreen($Color) ; GET GREEN COMPONENT

$ColorBlue = _ColorGetBlue($Color) ; GET RED COMPONENT

;

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

;

If $ColorBlue >= (255 - $Shade) AND $ColorGreen <= $Shade AND $ColorRed <= $Shade Then

Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED BLUE

Else

Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED BLUE

EndIf

;

EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

;

;

;

;----------------------------------------------------------------------------------------

;

; Description : Determine if a color is Green

; Syntax : _IsGreen($Color, $Shade)

; Parameter(s) : $Color - Color to be checked in decimal

; : $Shade - Amount that color can be off and still be considered Red

; Requirement(s) : None

; Return Value(s) : On Success - Returns 1

; On Failure - Returns -1

; Author(s) : Keith Brown

; Note(s) : $Shade should be set a value between 0 and 150, a value of 0 means

; that the color is exactly Green, i.e. its hex value is 00FF00

;

;----------------------------------------------------------------------------------------

;

Func _IsGreen($Color, $Shade)

;

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT

$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT

$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

;

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

;

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT

$ColorGreen = _ColorGetGreen($Color) ; GET THE GREEN COMPONENT

$ColorBlue = _ColorGetBlue($Color) ; GET THE BLUE COMPONENT

;

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

;

If $ColorGreen >= (255 - $Shade) AND $ColorRed <= $Shade AND $ColorBlue <= $Shade Then

Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED GREEN

Else

Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED GREEN

EndIf

;

EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

;

;

;----------------------------------------------------------------------------------------

;

; Description : Determine if a color is White

; Syntax : _IsWhite($Color, $Shade)

; Parameter(s) : $Color - Color to be checked in decimal

; : $Shade - Amount that color can be off and still be considered Red

; Requirement(s) : None

; Return Value(s) : On Success - Returns 1

; On Failure - Returns -1

; Author(s) : Keith Brown

; Note(s) : $Shade should be set a value between 0 and 150, a value of 0 means

; that the color is exactly White, i.e. its hex value is FFFFFF

;

;----------------------------------------------------------------------------------------

;

Func _IsWhite($Color, $Shade)

;

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT

$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT

$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

;

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

;

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT

$ColorGreen = _ColorGetGreen($Color) ; GET THE GREEN COMPONENT

$ColorBlue = _ColorGetBlue($Color) ; GET THE BLUE COMPONENT

$Shade = (255 - $Shade)

;

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

;

If $ColorRed >= $Shade AND $ColorGreen >= $Shade AND $ColorBlue >= $Shade Then

Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED WHITE

Else

Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED WHITE

EndIf

;

EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

;

;

;----------------------------------------------------------------------------------------

;

; Description : Determine if a color is Black

; Syntax : _IsBlack($Color, $Shade)

; Parameter(s) : $Color - Color to be checked in decimal

; : $Shade - Amount that color can be off and still be considered Black

; Requirement(s) : None

; Return Value(s) : On Success - Returns 1

; On Failure - Returns -1

; Author(s) : Keith Brown

; Note(s) : $Shade should be set a value between 0 and 150, a value of 0 means

; that the color is exactly Black, i.e. its hex value is 000000

;

;----------------------------------------------------------------------------------------

;

Func _IsBlack($Color, $Shade)

;

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT

$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT

$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

;

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

;

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT

$ColorGreen = _ColorGetGreen($Color) ; GET THE GREEN COMPONENT

$ColorBlue = _ColorGetBlue($Color) ; GET THE BLUE COMPONENT

;

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

;

If $ColorRed <= $Shade AND $ColorGreen <= $Shade AND $ColorBlue <= $Shade Then

Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED BLACK

Else

Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED BLACK

EndIf

;

EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

I forgot to mention that that I added this to the Color.au3 script in the includes folder. Otherwise you will need to add an #include <color.au3>

Edited by kbrown
Link to comment
Share on other sites

BTW, welcome to the forums

to help simplify your over written functions....

Func _IsColor($Test, $Color, $Shade)

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT
$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT
$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT
$ColorGreen = _ColorGetGreen($Color) ; GET THE GREEN COMPONENT
$ColorBlue = _ColorGetBlue($Color) ; GET THE BLUE COMPONENT

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

If $Test = "red" and $ColorRed >= (255 - $Shade) AND $ColorGreen <= $Shade AND $ColorBlue <= $Shade Then Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED RED
If $Test = "blue" and $ColorBlue >= (255 - $Shade) AND $ColorGreen <= $Shade AND $ColorRed <= $Shade Then ReDim 1
Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED $Test Color


EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

8)

NEWHeader1.png

Link to comment
Share on other sites

Very nice program, but I am looking to write a full fledged GUI to run my script. The game I am writing it for is Everquest II. I have already made all of the appropriate User Interface modifications in the game to display certain colors when a exact event occurs. I need AutoIt to read that event through the colors and then react to it. The script will react differently depending on what color combonation it reads on the screen.

One other question. Which UI mode would be better for a program like this. The user will be able to change a bunch of settings through the GUI, but once those settings are made, a button would be pressed that will start up the main script. The script will loop through several times and will require no input from the user until it is done, or the script is paused or stopped. Just from what I have read in the help file, its seems that OnEvent mode will be best for what I would like to do. Any thoughts or suggestions? Thanks beforehand. :whistle:

Link to comment
Share on other sites

BTW, welcome to the forums

to help simplify your over written functions....

Func _IsColor($Test, $Color, $Shade)

$ColorRed = 0 ; TEMP VARIABLE TO HOLD RED COMPONENT
$ColorGreen = 0 ; TEMP VARIABLE TO HOLD GREEN COMPONENT
$ColorBlue = 0 ; TEMP VARIABLE TO HOLD BLUE COMPONENT

; THE FIRST THING WE NEED TO DO IS BREAK THE COLOR DOWN INTO ITS INDIVIDUAL COMPNENTS

$ColorRed = _ColorGetRed($Color) ; GET THE RED COMPONENT
$ColorGreen = _ColorGetGreen($Color) ; GET THE GREEN COMPONENT
$ColorBlue = _ColorGetBlue($Color) ; GET THE BLUE COMPONENT

; CHECK TO SEE IF THE VALUE IS IN THE RANGE SPECIFIED BY $SHADE

If $Test = "red" and $ColorRed >= (255 - $Shade) AND $ColorGreen <= $Shade AND $ColorBlue <= $Shade Then Return 1 ; VALUE IS IN RANGE AND IS CONSIDERED RED
If $Test = "blue" and $ColorBlue >= (255 - $Shade) AND $ColorGreen <= $Shade AND $ColorRed <= $Shade Then ReDim 1
Return -1 ; VALUE IS OUT OF RANGE AND IS NOT CONSIDERED $Test Color
EndFunc ;--------------------------- END OF FUNCTION ----------------------------------

8)

If i am reading your code correctly, you are saying write one function that will cover all the colors that I might need to look for? I assume that the ReDim 1 should be Return. A good idea eitherway. I will only be looking for those 4 colors and maybe white, so putting everything into one function sounds reasonable. I am trying to make the program very readable and easy to understand so that others may follow it and make any changes. This should help by cutting down the amount of coding to do. Thanks for your help. Edited by kbrown
Link to comment
Share on other sites

either approach should work GUIGetMsg() or "On Event Mode"

most often scripts of this type use hotkeyset() for start, stop, and pause functions

these hot keys will over-ride execution, while GUIGetMsg() will not over-ride / interrupt execution in functions

8)

yes ReDim should have been Return

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...