PixelGetPrimaryColor & PixelSearchPrimaryColor RGB

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

PixelGetPrimaryColor & PixelSearchPrimaryColor RGB

04 Dec 2019, 00:54

This fun little script I made helped me understand and manage, how RGB works with PixelGetColor and PixelSearch while being able to compare the two simultaneously. So, I thought it might help others as well.
By splitting the RGB into 3 channels Primary, Secondary, and Tertiary, they can be better managed and black can be added as a shader to the Primary channel, while setting tolerance for the Secondary, and Tertiary channels, offering more control than using PixelSearch or PixelGetColor in their traditional way.
Adjust the ShaderVariance to add/subtract more/less black to the Primary channel.
ShaderVariance also sets the PixelSearch Variance for comparison with PixelGetColor.
Adjust the ColorVariance to add/subtract more/less of Secondary & Tertiary colors to the Primary.

Here is a visual representation of what to expect upon running the script as-is.
YellowLines = PixelGetColor Range
GreenLines = PixelSearch Range
Spoiler

Code: Select all

;<======================================== PixelGetPrimaryColor =========================================>
#SingleInstance Force
PID := DllCall("GetCurrentProcessId")	;PID Of Script.
Process, Priority, %PID%, Above Normal	;Improves Performance Of Script.
SetWinDelay, -1 						;Improves Performance Of Script.
SetMouseDelay, -1						;Improves Performance Of Script.
SetBatchLines, -1						;Improves Performance Of Script.
SetControlDelay, -1						;Improves Performance Of Script.
CoordMode, Mouse, Screen				;Improves Performance Of Script.
CoordMode, Pixel, Screen				;Improves Performance Of Script.
IfNotExist, %USERPROFILE%\Downloads\color-wheel-chromatic-scale-clip-art-colours.jpg
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=8426, %USERPROFILE%\Downloads\color-wheel-chromatic-scale-clip-art-colours.jpg
} SplashImage, %USERPROFILE%\Downloads\color-wheel-chromatic-scale-clip-art-colours.jpg, B FS18, Hold Esc To Exit.
Loop,
{	MouseGetPos, X, Y
	PixelGetColor, Color, X, Y, RGB, Fast
	GetRGB(Color, Red, Green, Blue)		;Split Color Hex Values Into Red Green Blue.
} Return
GetRGB(RGBColor, Red, Green, Blue) 
{	Red   := (RGBColor>>16&255)			;Calculate Red From Hex Value.
	Green := (RGBColor>>8&255)			;Calculate Green From Hex Value.
	Blue  := (RGBColor&255)				;Calculate Blue From Hex Value.
	;<==================== Color Channels ====================>
	;Primary Channel Set For The Primary Color.
	Primary = Red	;Set One Of 3 Primary Colors On The Primary Channel (e.g., Red, Green, or Blue).
	ShaderVariance = 120		;Set Between 0-255. Black Shader, Smaller Nubers Will Trigger With Darker Colors (e.g., 0 = Black).
	;Secondary & Tertiary Channels Are The Color Variance From Primary Channel.
	ColorVariance = 26		;Set Between 0-255. Color Mixer. (e.g., How Much Red To Look For Befor Orange).
	;If Secondary & Tertiary Channels Exceed The ColorVariance = False
	;If Secondary & Tertiary Channels DONOT Exceed ColorVariance = True
	If Primary = Red
	{	(Primary := Red), (Secondary := Green), (Tertiary := Blue), (Prime := 0xFF0000)
	} Else, If Primary = Green
	{	(Primary := Green), (Secondary := Blue), (Tertiary := Red), (Prime := 0x00FF00)
	} Else, If Primary = Blue
	{	(Primary := Blue), (Secondary := Green), (Tertiary := Red), (Prime := 0x0000FF)
	} MouseGetPos, X, Y
	xPos:= (x+50), yPos:= (y+50)
	PixelGetColor, Color, X, Y, RGB, Fast
	If (((ShaderVariance<=Primary)AND(Primary<=255)) AND ((0<=Secondary)AND(Secondary<=ColorVariance)) AND ((0<=Tertiary)AND(Tertiary<=ColorVariance))) ;AND (Primary > Secondary) AND ( Tertiary > Secondary)
	{	ToolTip, PixelGetColor `nParameters Met `nRed = %Red% `nGreen = %Green% `nBlue = %Blue% `nRGB_HEX = %Color%, X%xPos%, Y%yPos%
	} Else,
	{	ToolTip, PixelGetColor `nParameters Exceeded `nRed = %Red% `nGreen = %Green% `nBlue = %Blue% `nRGB_HEX = %Color%, X%xPos%, Y%yPos%
	} WinSize := (40), WinlocX := (xPos+40), WinlocY := (yPos)
	Gui, 2: -Caption +AlwaysOnTop +Border +ToolWindow
	Gui, 2: Color, %Color%
	Gui, 2: Show, NA X%WinlocX% Y%WinlocY% H%WinSize% W%WinSize%, ColorGUI
	If(GetKeyState("Ctrl", "P"))		;Compare PixelSearch Variance With PixelGetColor Variance.
	{	MouseGetPos, X, Y
		PixelSearch, X, Y, X, Y, X, Y, Prime, ShaderVariance, RGB Fast
		If (!ErrorLevel)
		{	SoundBeep, 100, 20
}	}	} Return
~Esc::
KeyWait, Esc, T0.5
If (ErrorLevel)
{	SoundBeep, 777, 50
	ExitApp
} Return
;<================================================= END =================================================>
Spoiler

Alternatively, similar results can be seen with PixelSearch

Code: Select all

#SingleInstance Force
PID := DllCall("GetCurrentProcessId")				;PID Of Script.
Process, Priority, %PID%, Above Normal				;Improves Performance Of Script.
SetWinDelay, -1 									;Improves Performance Of Script.
SetMouseDelay, -1									;Improves Performance Of Script.
SetBatchLines, -1									;Improves Performance Of Script.
SetControlDelay, -1									;Improves Performance Of Script.
CoordMode, Mouse, Screen							;Improves Performance Of Script.
CoordMode, Pixel, Screen							;Improves Performance Of Script.
IfNotExist, %USERPROFILE%\Downloads\PixelGetPrimaryColor - Copy (2).ahk
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=8481, %USERPROFILE%\Downloads\PixelGetPrimaryColor - Copy (2).ahk
} Run, "%USERPROFILE%\Downloads\PixelGetPrimaryColor - Copy (2).ahk"
Deviation = 40
SetTimer, Start, 1
LFColors = 0xD70000|0xAF0000|0x870000|0x5F0000		;(FF0000 - 40 = D70000),(D70000 - 40 = AF0000), and so on... ;Red
Return
Start:
{	Loop, Parse, LFColors, |	
	{	MouseGetPos, X, Y
		PixelSearch, X, Y, X, Y, X, Y, A_LoopField, Deviation, RGB Fast
		If (!ErrorLevel)
		{	SoundBeep, 900, 7
			If (!FoundColor)
			(FoundColor := !FoundColor)
			Sleep, 100
	}	} If (FoundColor)
	{	(FoundColor := !FoundColor)
}	} Return
~Esc::ExitApp

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 66 guests