MsgBox to display pixel color in Hex & RBG

Get help with using AutoHotkey and its commands and hotkeys
WeThotUWasAToad
Posts: 260
Joined: 19 Nov 2013, 08:44

MsgBox to display pixel color in Hex & RBG

21 Feb 2017, 21:29

Hello,

What formula is used for the conversion of color in Hex form to color in Decimal form and how would you include the conversion in a script?

I use the MsgBox script shown below to obtain current mouse coords (1st line) and the color of the pixel at that location (2nd line). The color returned by AHK is a Hex code in BGR. However, I would like to also include a 3rd line which displays the color in RGB Decimal so that it's ready to insert into MS Office apps if needed.

Code: Select all

^F6::			; GetMousePosition and pixel color_Screen		
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
CoordMode, Pixel, Screen
PixelGetColor, color, %xpos%, %ypos%
Msgbox, The cursor is located at x = %xpos% & y = %ypos%.
	(Join,
	`nThe color of that pixel is (BGR) %color%
	`nor (RGB)  ###  ###  ###
	)
Return
Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: MsgBox to display pixel color in Hex & RBG

21 Feb 2017, 23:58

Try this:

Code: Select all

^F6::			; GetMousePosition and pixel color_Screen
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
CoordMode, Pixel, Screen
PixelGetColor, color, %xpos%, %ypos%
RGB := Format("0x{:06X}", (Color & 0xFF) << 16 | Color & 0xFF00 | (Color >> 16) & 0xFF)
Msgbox, The cursor is located at x = %xpos% & y = %ypos%.
	(Join,
	`nThe color of that pixel is (BGR) %color%
	`nor (RGB)  %RGB%
	)
Return
I hope that helps.
TravisQ
Posts: 15
Joined: 17 May 2015, 23:51

Re: MsgBox to display pixel color in Hex & RBG

22 Feb 2017, 01:12

Code: Select all

F1::			; GetMousePosition and pixel color_Screen		
CoordMode,Mouse,Screen
MouseGetPos,xpos,ypos
CoordMode,Pixel,Screen
PixelGetColor,bgr,%xpos%,%ypos%
PixelGetColor,rgb,%xpos%,%ypos%,rgb
rgbDec:=hex2dec(rgb)
coords:=xpos "," ypos
Msgbox, The cursor is located at x = %xpos% & y = %ypos%.
	(Join,
	`nThe color of that pixel is (BGR) %bgr%
	`nor (RGB)  %rgbDec%
	)
Return

F2::
	Menu,F1,add,coords,coords
	Menu,F1,add,rgb,rgb
	Menu,F1,add,rgbDec,rgbDec	
	Menu,F1,add,bgr,bgr
	Menu,F1,show
return
coords:
	Clipboard:=coords
return
rgb:
	Clipboard:=rgb
return
rgbDec:
	Clipboard:=rgbDec
return
bgr:
	Clipboard:=bgr
return

hex2dec(in){
i:=in ,j:=[]
h:={ 0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8
	,9:9,a:10,b:11,c:12,d:13,e:14,f:15 }
StringTrimLeft,o,i,2
o:=StrSplit(o)
for l,u in o {
	for k,v in h
		if (u=k) {
			j.push(v)
}}
r:=j.1*16+j.2
g:=j.3*16+j.4
b:=j.5*16+j.6
rgb:= r " " g " " b
return rgb
}
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: MsgBox to display pixel color in Hex & RBG

22 Feb 2017, 04:16

A basic concept of conversion:

Code: Select all

BGR :=     "0xFF8000"
R := (BGR & 0x0000FF)         ; 0x00 = 0
G := (BGR & 0x00FF00) >> 8    ; 0x80 = 128
B := (BGR & 0xFF0000) >> 16   ; 0xFF = 255
MsgBox, BGR: %BGR%`nR: %R%`nG: %G%`nB: %B%

Return to “Ask For Help”

Who is online

Users browsing this forum: StefanD, YoucefHam and 146 guests