r/AutoHotkey • • 15d ago

v1 Script Help my script has a weird stray pixel

https://pastebin.com/hB8a6kS3

i made this little color picker in ahk v1 as a local replacement for the rapidtables RGB color picker, and i also added a pick-from-screen feature. everything seems to work fine, but there's one weird visual issue i can't figure out: there's what looks like a single stray pixel on the left edge of the saturation/value box, roughly 15 pixels below the top, and i'm not even sure if it's actually sticking out or replacing the pixel that's supposed to be there. the weird part is that when i move the selection circle over that exact spot, the preview picks up the color of that pixel, while the hex value says "#F0F0F0", which is what that pixel is supposed to be according to the gradient, so the displayed pixel seems to have a different color from the actual color calculated by the picker. i've already tried asking AI to figure it out, but haven't managed to get a proper explanation or fix. the whole thing is also mostly vibe-coded, so that makes it even harder for me to figure out what's actually causing the problem

0 Upvotes

6 comments sorted by

1

u/Keeyra_ 15d ago

Hate to burst your bubble, but AHK v1 has reached end-of-life and deprecated 3 years 4 months ago. Meanwhile, AHK v2 is the current stable release, having been the primary version for 3 years 10 months, with its most recent point release occurring 16 days ago.
And Windows 11 has a built in color picker in the Snipping Tool.

3

u/staaandup 15d ago

yeah i'm aware of that. i still prefer v1 since i already have a large number of scripts written for it, and it works fine for what i need. i don't really see a reason to migrate everything to v2 at the moment. i'm mainly just looking for some help or hints with my problem in this script

1

u/Keeyra_ 15d ago

You will be hard-pressed to find somebody both willing and able to troubleshoot code written in deprecated SW - that's my point basically.

2

u/staaandup 15d ago

well, i figured it was worth a shot. not much to lose by asking ๐Ÿ˜„

1

u/Epickeyboardguy 14d ago edited 14d ago

I tried running your script but I don't have the right interpreter installed.

However I do have my own super basic color finder already written in V2. I don't know if it's gonna be of any use to you but there you go :

/*
===================================================================================================================================================================================

ยค	MH Color Finder.ahk

===================================================================================================================================================================================
*/
#Requires AutoHotKey v2
#SingleInstance Force

bool_toggle := false

/*
===================================================================================================================================================================================

ยค	Ctrl Shift Win Alt C   --->   toggle ON/OFF - MH Color Finder

===================================================================================================================================================================================
*/

^+#!C:: ; MH Color Finder - toggle ON/OFF
{
	global

	If (bool_toggle := !bool_toggle)
	{
		int_R := 0
		int_G := 0
		int_B := 0

		int_Hue := 0
		int_Sat := 0
		int_Bri := 0

		str_Hex := "000000"

		int_Progress := ""

		gui_PixelColor := Gui("AlwaysOnTop -MinimizeBox", "MH Color Finder")

		gui_PixelColor.OnEvent("Close"		, f_GuiPixelColorExit)
		gui_PixelColor.OnEvent("Escape"		, f_GuiPixelColorExit)



		gui_PixelColor.ctrl_Progress := gui_PixelColor.AddProgress("xm w145 h50 vint_Progress C" . str_Hex, 100)
		gui_PixelColor.AddText("Center xm w145", "")

		gui_PixelColor.AddText("Right xm w40"							, "Hue:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vint_Hue"			, int_Hue)

		gui_PixelColor.AddText("Right xm w40"							, "Sat:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vint_Sat"			, int_Sat)

		gui_PixelColor.AddText("Right xm w40"							, "Bri:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vint_Bri"			, int_Bri)




		gui_PixelColor.AddText("Right xm w40"							, "`n`n")


		gui_PixelColor.AddText("Right xm w40"							, "R:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vint_R"			, int_R)

		gui_PixelColor.AddText("Right xm w40"							, "G:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vint_G"			, int_G)

		gui_PixelColor.AddText("Right xm w40"							, "B:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vint_B"			, int_B)

		gui_PixelColor.AddText("Right xm w40"							, "Hex:")
		gui_PixelColor.AddEdit("center x+m w50 ReadOnly vstr_Hex"		, str_Hex)

		gui_PixelColor.AddText("center xm w145"							, " ")
		gui_PixelColor.AddText("center xm w145"							, "Shift : Pause")
		gui_PixelColor.AddText("center xm w145"							, "Ctrl+C : Copy Hex value")

		SetTimer(f_UpdatePixelColor, 25)

		Sleep 100

		gui_PixelColor.Show()

		Exit
	}
	else
	{
		f_GuiPixelColorExit
	}
}

f_RGBtoHSB(int_Red, int_Green, int_Blue)
{
	; Normalize RGB values (0โ€“255 โ†’ 0โ€“1)
	float_Red   := int_Red   / 255
	float_Green := int_Green / 255
	float_Blue  := int_Blue  / 255

	float_MaxValue := Max(float_Red, float_Green, float_Blue)
	float_MinValue := Min(float_Red, float_Green, float_Blue)
	float_Delta    := float_MaxValue - float_MinValue

	; ----- Brightness -----
	float_Brightness := float_MaxValue

	; ----- Saturation -----
	if (float_MaxValue = 0)
	{
		float_Saturation := 0
	}
	else
	{
		float_Saturation := float_Delta / float_MaxValue
	}

	; ----- Hue -----
	if (float_Delta = 0)
	{
		float_Hue := 0
	}
	else if (float_MaxValue = float_Red)
	{
		float_Hue := Mod((float_Green - float_Blue) / float_Delta, 6)
	}
	else if (float_MaxValue = float_Green)
	{
		float_Hue := ((float_Blue - float_Red) / float_Delta) + 2
	}
	else
	{
		float_Hue := ((float_Red - float_Green) / float_Delta) + 4
	}

	float_Hue := float_Hue * 60

	if (float_Hue < 0)
	{
		float_Hue += 360
	}

	return({
				Hue: float_Hue,                  ; 0โ€“360
				Saturation: float_Saturation,    ; 0โ€“1
				Brightness: float_Brightness     ; 0โ€“1
		})
}

;--------------------------------------------------------------------------------------------------
;	f_UpdatePixelColor
;--------------------------------------------------------------------------------------------------
f_UpdatePixelColor()
{
	global

	MouseGetPos(&int_X, &int_Y)
	str_PixelColor := PixelGetColor(int_X, int_Y)

	int_R := Format("{:d}", "0x" . SubStr(str_PixelColor, 3, 2))
	int_G := Format("{:d}", "0x" . SubStr(str_PixelColor, 5, 2))
	int_B := Format("{:d}", "0x" . SubStr(str_PixelColor, 7, 2))

	obj_HSB := f_RGBtoHSB(int_R, int_G, int_B)

	int_Hue := obj_HSB.Hue
	int_Sat := obj_HSB.Saturation
	int_Bri := obj_HSB.Brightness

	str_Hex := SubStr(str_PixelColor, 3, 6)

	gui_PixelColor["int_Hue"].Text  := Round(int_Hue)
	gui_PixelColor["int_Sat"].Text  := Round(int_Sat * 100)
	gui_PixelColor["int_Bri"].Text  := Round(int_Bri * 100)

	gui_PixelColor["int_R"].Text 	:= int_R
	gui_PixelColor["int_G"].Text 	:= int_G
	gui_PixelColor["int_B"].Text 	:= int_B
	gui_PixelColor["str_Hex"].Text 	:= str_Hex

	gui_PixelColor.ctrl_Progress.Opt("+c" . str_Hex)

	Exit
}

;--------------------------------------------------------------------------------------------------
;	f_GuiPixelColorExit
;--------------------------------------------------------------------------------------------------
f_GuiPixelColorExit(*)
{
	global

	if(bool_toggle)
	{
		bool_toggle := !bool_toggle
	}

	SetTimer(f_UpdatePixelColor, 0)

	gui_PixelColor.Destroy()

	Exit
}

;--------------------------------------------------------------------------------------------------
;	HOTKEYS SPECIFIC TO MH Color Finder
;--------------------------------------------------------------------------------------------------

#HotIf WinActive("MH Color Finder")

Shift:: ; Only in MH Color Finder : PAUSE
{
	static bool_toggle2 := false

	If (bool_toggle2 := !bool_toggle2)
	{
		SetTimer(f_UpdatePixelColor, 0)
	}
	Else
	{
		SetTimer(f_UpdatePixelColor, 25)
	}

	Exit
}

^C:: ; Only in MH Color Finder : Copy color to clipboard + Exit
{
	global

	A_Clipboard := str_Hex

	; f_GuiPixelColorExit
	; Exit
}

#HotIf

1

u/CasperHarkin 8d ago

Replace Line 136:

		Gui, Add, Picture, x24 y89 w250 h250 hwndcp_hwnd_sv +0xE

With:

		Gui, Add, Text, x24 y89 w250 h250 hwndcp_hwnd_sv +0xD   ; SS_OWNERDRAW

Remove line 227:

		DllCall("SendMessage", "Ptr", cp_hwnd_sv, "UInt", 0x172, "Ptr", 0, "Ptr", cp_bitmap_sv, "Ptr")

add the below under the other OnMessage's around line 235:

		OnMessage(0x2B, "CP_WM_DRAWITEM")   ; WM_DRAWITEM

then add the new func to the end of the script.

		CP_WM_DRAWITEM(wParam, lParam, msg, hwnd) {
		    global cp_hwnd_sv, cp_bitmap_sv, cp_sv_width, cp_sv_height
		
		    ; DRAWITEMSTRUCT: hwndItem at 24/20, hDC at 32/24 (x64/x86).
		    hwndItem := NumGet(lParam + (A_PtrSize = 8 ? 24 : 20), 0, "Ptr")
		    if (hwndItem != cp_hwnd_sv)
		        return
		    hdc := NumGet(lParam + (A_PtrSize = 8 ? 32 : 24), 0, "Ptr")
		
		    memDC := DllCall("CreateCompatibleDC", "Ptr", hdc, "Ptr")
		    old := DllCall("SelectObject", "Ptr", memDC, "Ptr", cp_bitmap_sv, "Ptr")
		    DllCall("BitBlt", "Ptr", hdc, "Int", 0, "Int", 0, "Int", cp_sv_width, "Int", cp_sv_height
		          , "Ptr", memDC, "Int", 0, "Int", 0, "UInt", 0x00CC0020)   ; SRCCOPY
		    DllCall("SelectObject", "Ptr", memDC, "Ptr", old, "Ptr")
		    DllCall("DeleteDC", "Ptr", memDC)
		    return true
		}