As I could not find a precise and easy pixel ruler.
I decided to make one myself.
Here is the code:
Spoiler
Shortcuts:Code: Select all
;---------------------------------------------------------------------------------------
;Name................ Advanced Pixel Ruler and Color Picker
;Description......... Measure the size (width and height) between 2 pixels and show their location and color.
;Sript version....... 1.2
;AHK Version ........ 1.1.24.03 (U32bit)
;Author:............. Speedmaster
;
;Shortcuts:
;
;Clicks on Grid
;--------------------------
; shift & left click or F1... Take point A
; shift & right click or F2... Take point B
; right click................. Switch between point A and B
; control & left click........ Display colors
; alt & left click............ Display XY positions
;clicks on Displayed infos
;---------------------------
; right click................. View next infos
; control & left click....... Copy value to clipboard
; control+alt & left click.... Append value to clipboard
;
;----------------------------------------------------------------------------------------
#singleinstance, force
SetBatchLines -1
gui 1: -dpiscale
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;setup here rows, columns, cell width and cell height for the grid
rows:=11, cols:=11, cllw:=20, cllh:=20,
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
clln:="1_", cmj:=0
gpx:=5, gpy:=5, wsp:=-1, hsp:=-1,
r:=0, c:=0 , opt:="0x200 center BackGroundTrans border gclickcell "
While r++ < rows {
while c++ < cols{
gui 1: add, text, % opt " w"cllw " h"cllh " v" clln r "_" c " Hwnd" clln r "_" c ((c=1 && r=1) ? " x"gpx " y"gpy " section"
: (c!=1 && r=1) ? " x+"wsp " yp" : (c=1 && r!=1) ? " xs" " y+"hsp : " x+"wsp " yp"),
} c:=0
} r:=0, c:=0
gui, font, s12
rows:=rows, cols:=cols, cllw:=cllw, cllh:=cllh,
clln:="2_", cmj:=0
gpx:=5, gpy:=5, wsp:=-1, hsp:=-1,
r:=0, c:=0 , opt:="0x200 center BackGroundTrans border gclickcell "
While r++ < rows {
while c++ < cols{
gui 1: add, text, % opt " w"cllw " h"cllh " v" clln r "_" c " Hwnd" clln r "_" c ((c=1 && r=1) ? " x"gpx " y"gpy " section"
: (c!=1 && r=1) ? " x+"wsp " yp" : (c=1 && r!=1) ? " xs" " y+"hsp : " x+"wsp " yp"),
} c:=0
} r:=0, c:=0
switchinfo:=1
gui 1: add, text, xs y+2 w104 h20 vt_w section gclickcell border center 0x201, 0
gui 1: add, text, x+2 yp w104 h20 vt_h gclickcell border center 0x201, 0
gui 1: add, text, xs y+3 w210 h20 vt_w_h gclickcell border center 0x201, w,h:
gui 1: add, text, xs ys w104 h20 vt_xs gclickcell border center 0x201, x:
gui 1: add, text, x+2 ys w104 h20 vt_ys gclickcell border center 0x201, y:
gui 1: add, text, xs y+3 w210 h20 vt_xs_ys gclickcell border center 0x201, x,y:
gui 1: add, text, xs ys w210 h20 vt_rgbx gclickcell border center 0x201, RGBx:
gui 1: add, text, xs y+3 w210 h20 vt_bgrx gclickcell border center 0x201, BGRx:
gui 1: -Caption
gui 1: -dpiscale
Gui 1: +LastFound +AlwaysOnTop
gui 1: show, % "x" a_screenwidth//2+50 "y" a_screenheight//2 "w " cllw*cols
WinGetPos , XGui, YGui, WGui, HGui
OnMessage(0x201, "WM_LBUTTONDOWN")
; Allow moving the GUI by dragging any point in its client area.
WM_LBUTTONDOWN()
{
if A_Gui
PostMessage, 0xA1, 2 ; WM_NCLBUTTONDOWN
}
GuiControl, hide1 , t_xs
GuiControl, hide1 , t_ys
GuiControl, hide1 , t_xs_ys
GuiControl, hide1 , t_rgbx
GuiControl, hide1 , t_bgrx
GuiControl, hide1 , t_yw
GuiControl, hide1 , t_xw_yw
Gui 3: +LastFound +AlwaysOnTop -Caption +E0x08000000 ;+E0x20
gui 3: add, text, x0 y0 w%cols% h%rows% border gmove3,
Gui 3:Show, % " x" xgui " y" ygui-20 " w" cols " h" rows
WinSet, Transparent, 100
Gui, 3: Color, fuchsia
Gui 4: +LastFound +AlwaysOnTop -Caption +E0x08000000 ;+E0x20
gui 4: add, text, x0 y0 w%cols% h%rows% border gmove4,
Gui 4:Show, % " x" xgui+15 " y" ygui-20 " w" cols " h" rows
WinSet, Transparent, 100
Gui, 4: Color, aqua
cx:=ceil(cols/2)
cy:=ceil(rows/2)
ox:=cx-cx+1
oy:=cy-cy+1
owx:=cx-cx+1
owy:=cy-cy+1
gosub, snap3
return
snap3:
CoordMode, Mouse, Screen
CoordMode, Pixel
Loop {
MouseGetPos, pxx, pyy
if GetKeyState("LButton","P")
break
if !GetKeyState("LButton","P")
Gui, 3: Show, % " x" pxx-cx " y" pyy-cy
}
return
snap4:
CoordMode, Mouse, Screen
CoordMode, Pixel
Loop {
MouseGetPos, pxx, pyy
if GetKeyState("LButton","P")
break
if !GetKeyState("LButton","P")
Gui, 4: Show, % " x" pxx-cx " y" pyy-cy
}
return
~f1::
gosub, snap3
return
~f2::
gosub, snap4
return
; left click
;--------------------------------------------------------------
clickcell:
;if (debug)
;msgbox, % a_guicontrol
sp := StrSplit(a_guicontrol , "_" )
if (sp.1=1)
{
if GetKeyState("Lshift","P") {
gosub, snap3
switchinfo:=3
gosub, switch
return
}
if GetKeyState("Alt","P") {
switchinfo:=1
gosub, switch
}
if GetKeyState("Ctrl","P") {
switchinfo:=2
gosub, switch
}
if (prevposa)
drawchar(prevposa, "")
if (prevposb)
drawchar(prevposb, "")
selectedcolor:= "0x" . data[a_guicontrol].3
bestcontrast:=51
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "red")
bc:=compare("0xED1B24", selectedcolor) ;red
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "yellow")
bestcontrast:=bc
}
bc:=compare("0x2E3192", selectedcolor) ;blue
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "yellow")
bestcontrast:=bc
}
bc:=compare("0xFEF200", selectedcolor) ;yellow
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "blue")
bestcontrast:=bc
}
bc:=compare("0xF7941D", selectedcolor) ;orange
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "40311D")
bestcontrast:=bc
}
bc:=compare("0x0B9444", selectedcolor) ;green
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "white")
bestcontrast:=bc
}
bc:=compare("0x31C547", selectedcolor) ;light green 1
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "red")
bestcontrast:=bc
}
bc:=compare("0x73E786", selectedcolor) ;light green 2
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "red")
bestcontrast:=bc
}
bc:=compare("0x92278F", selectedcolor) ;purple
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "yellow")
bestcontrast:=bc
}
bc:=compare("0xF385B6", selectedcolor) ;pink
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "black")
bestcontrast:=bc
}
bc:=compare("0x603814", selectedcolor) ;brown
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "yellow")
bestcontrast:=bc
}
bc:=compare("0x000000", selectedcolor) ;black
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "yellow")
bestcontrast:=bc
}
bc:=compare("0x818286", selectedcolor) ;gray
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "white")
bestcontrast:=bc
}
bc:=compare("0xFFFFFF", selectedcolor) ;white
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "navy")
bestcontrast:=bc
}
bc:=compare("0xC3B59B", selectedcolor) ;light brown
If (bc) && (bc < 50) && (bc < bestcontrast)
{
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "navy")
bestcontrast:=bc
}
if (selectedcolor=0x000000) ; black
drawchar(sp.1+1 "_" sp.2 "_" sp.3, "+", "yellow")
if (guidat3)
prevposa:=sp.1+1 "_" sp.2 "_" sp.3
else
prevposb:=sp.1+1 "_" sp.2 "_" sp.3
guicontrol,, t_xs, % " x: " . data[sp.1 "_" sp.2 "_" sp.3].1
guicontrol,, t_ys, % " y: " . data[sp.1 "_" sp.2 "_" sp.3].2
guicontrol,, t_xs_ys, % " x,y: " . data[sp.1 "_" sp.2 "_" sp.3].1 "," data[sp.1 "_" sp.2 "_" sp.3].2
guicontrol,, t_rgbx, % " RGB: " . data[a_guicontrol].3
guicontrol,, t_xw, % " Xwin: " . data[a_guicontrol].4
guicontrol,, t_yw, % " Ywin: " . data[a_guicontrol].5
guicontrol,, t_bgrx, % " BGR: " . data[a_guicontrol].6
if a=
{
a:=data[a_guicontrol].1
c:=data[a_guicontrol].2
guicontrol,, t_w, % " w: " . "0"
guicontrol,, t_h, % " h: " . "0"
guicontrol,, t_w_h, % " wh:"
ab=
cd=
diag=
}
else if a !=
{
b:=data[a_guicontrol].1
d:=data[a_guicontrol].2
if (a>=b)
{
ab:=(a-b)+1
}
if (b>=a)
{
ab:=(b-a)+1
}
if (c>=d)
{
cd:=(c-d)+1
}
if (d>=c)
{
cd:=(d-c)+1
}
;Diag:=pxDist(a, b, c, d)
guicontrol,, t_w, % " w: " . ab
guicontrol,, t_h, % " h: " . cd
guicontrol,, t_w_h, % "w,h: " ab "," cd
a=
b=
c=
d=
}
}
if GetKeyState("Ctrl","P") && GetKeyState("Alt","P") ;append to clipboard
{
clipboardBefore = %clipboard%
ClipWait, 2
}
if getkeystate("Control", "P")
{
if (a_guicontrol="t_xs")
{
clipboard:=extractnumber(a_guicontrol)
TrayTip % (clipboardBefore ? "append " : "Copied ") "X screen", % clipboard
}
if (a_guicontrol="t_ys")
{
clipboard:=extractnumber(a_guicontrol)
TrayTip % (clipboardBefore ? "append " : "Copied ") "Y screen", % clipboard
}
if (a_guicontrol="t_w")
{
clipboard:=extractnumber(a_guicontrol)
TrayTip % (clipboardBefore ? "append " : "Copied ") "Width", % clipboard
}
if (a_guicontrol="t_h")
{
clipboard:=extractnumber(a_guicontrol)
TrayTip % (clipboardBefore ? "append " : "Copied ") "Height", % clipboard
}
if (a_guicontrol="t_w_h")
{
clipboard:=extractnumber("t_w") "," extractnumber("t_h")
TrayTip % (clipboardBefore ? "append " : "Copied ") "Width,Height", % clipboard
}
if (a_guicontrol="t_xs_ys")
{
GuiControlGet, xs_ys,, t_xs_ys
clipboard:=SubStr(xs_ys, 7)
TrayTip % (clipboardBefore ? "append " : "Copied ") "XY sreen", % clipboard
}
if (a_guicontrol="t_rgbx")
{
GuiControlGet, copycolor,, t_rgbx
copycolor:=SubStr(copycolor, -5 , 7)
clipboard:=copycolor
TrayTip % (clipboardBefore ? "append " : "Copied ") "Color", % "RGB: 0x" . copycolor
}
if (a_guicontrol="t_bgrx")
{
GuiControlGet, copycolor,, t_bgrx
copycolor:=SubStr(copycolor, -5 , 7)
clipboard:=copycolor
TrayTip % (clipboardBefore ? "append " : "Copied ") "Color", % "BGR: 0x" . copycolor
}
if clipboardBefore
clipboard = %clipboardBefore%`r`n%clipboard%
clipboardBefore = %clipboard%
ClipWait, 2
clipboardbefore:=""
}
return
switch:
(switchinfo < 3) ? switchinfo++ : switchinfo:=1
if (switchinfo=2) {
GuiControl, hide1 , t_w
GuiControl, hide1 , t_h
GuiControl, hide1 , t_w_h
GuiControl, hide0 , t_xs
GuiControl, hide0 , t_ys
GuiControl, hide0 , t_xs_ys
GuiControl, hide1 , t_rgbx
GuiControl, hide1 , t_bgrx
}
else if (switchinfo=1) {
GuiControl, hide0 , t_w
GuiControl, hide0 , t_h
GuiControl, hide0 , t_w_h
GuiControl, hide1 , t_xs
GuiControl, hide1 , t_ys
GuiControl, hide1 , t_xs_ys
GuiControl, hide1 , t_rgbx
GuiControl, hide1 , t_bgrx
}
else if (switchinfo=3) {
GuiControl, hide1 , t_w
GuiControl, hide1 , t_h
GuiControl, hide1 , t_w_h
GuiControl, hide1 , t_xs
GuiControl, hide1 , t_ys
GuiControl, hide1 , t_xs_ys
GuiControl, hide0 , t_rgbx
GuiControl, hide0 , t_bgrx
}
return
;Right click
;-----------------------------------------------------------------------------
GuiContextMenu:
if (a_guicontrol="1_1_" cols)
exitapp
if GetKeyState("Lshift","P") {
gui4data:=""
}
if ((sp := StrSplit(a_guicontrol , "_" ).1)=1)
{
guidat3:=!guidat3
if (guidat3)
data:=gui3data
else
data:=gui4data
if !(gui3data)
gosub, snap3
if !(gui4data)
gosub, snap4
else
Row:=0 Col:=0
loop, % cols {
Row:= ROw+1
loop, % rows {
Col:= Col+1
cell_to_color=1_%row%_%col%
CellFont(cell_to_color, "s20", "webdings")
if (guidat3)
drawchar(cell_to_color, "g", gui3data[cell_to_color].3)
else
drawchar(cell_to_color, "g", gui4data[cell_to_color].3)
}
Col:=0
}
if (guidat3) {
if (prevposa)
drawchar(prevposa, "+")
if (prevposb)
drawchar(prevposb, "")
if (prevposa){
val:=1 . SubStr(prevposa, 2)
update(val)
}
}
else {
if (prevposb)
drawchar(prevposb, "+")
if (prevposa)
drawchar(prevposa, "")
if (prevposb){
val:=1 . SubStr(prevposb, 2)
update(val)
}
}
}
else {
gosub, switch
}
update(val)
{
global
guicontrol,, t_xs, % " x: " . data[val].1
guicontrol,, t_ys, % " y: " . data[val].2
guicontrol,, t_xs_ys, % " x,y: " . data[val].1 "," data[val].2
guicontrol,, t_rgbx, % " RGB: " . data[val].3
;guicontrol,, t_xw, % " Xwin: " . data[val].4
;guicontrol,, t_yw, % " Ywin: " . data[val].5
guicontrol,, t_bgrx, % " BGR: " . data[val].6
}
;---------------------------------------
Blink:
;---------------------------------------
if (guidat3) {
settimer, blinkA, 250, on
settimer, blinkB, off
gui 4: color, green
}
else {
settimer, blinkB, 250, on
settimer, blinkA, off
sleep, 500
gui 3: color, fuchsia
}
return
blinkA:
blinkA:=!blinkA
if (blinkA)
gui 3: color, fuchsia
else
gui 3: color, aqua
return
blinkB:
blinkB:=!blinkB
if (blinkB)
gui 4: color, green
else
gui 4: color, aqua
gui 3: color, fuchsia
return
return
scanff:
CoordMode, Mouse, Window
MouseGetPos, pwX, pwY
CoordMode, Mouse, Screen
MouseGetPos, pX, pY
;o = origin (the first cell on grid)
ox:=(px-cx+1)-1
oy:=(py-cy+1)-1
owx:=(pwx-cx+1)-1
owy:=(pwy-cy+1)-1
; all guis must stay hidden during this process
Row:=0 Col:=0
data:={}
loop, % rows {
Row:= ROw+1
oy := oy+1 ;grid origin screen
owy := owy+1 ;grid origin window
loop, % cols {
Col:= Col+1
ox := ox+1
owx := owx+1
cell_to_color=1_%row%_%col%
mycolor:=getcolor()
mycolorbgr:=rgbx_to_bgrx(mycolor)
objrawset(data, cell_to_color, [ox, oy, mycolor, owx, owy, mycolorbgr])
}
Col:=0
ox:=px-floor(cx/2)*2
owx:=pwx-floor(cx/2)*2
}
Gui, 1: +LastFound
gui, 1:default
gui 1: -dpiscale
gui1active:=1
; write color on grid
Row:=0 Col:=0
loop, % rows {
Row:= ROw+1
loop, % cols {
Col:= Col+1
cell_to_color=1_%row%_%col%
CellFont(cell_to_color, "s20", "webdings")
drawchar(cell_to_color, "g", data[cell_to_color].3)
}
Col:=0
}
return
MOVE3:
g4:=0
g3:=1
guidat3:=1
PostMessage,0xA1,2,,,A
Return
MOVE4:
g3:=0
g4:=1
guidat3:=0
PostMessage,0xA1,2,,,A
Return
~LButton Up::
start:
If (g3=1) {
gui 3:hide
gosub, scanff
gui3data:=ObjFullyClone(data)
g3:=0
g4:=0
gui 3: show
}
If (g4=1) {
gui 4:hide
gosub, scanff
gui4data:=ObjFullyClone(data)
g3:=0
g4:=0
gui 4: show
}
gosub, blink
return
getcolor()
{
global ox, oy
CoordMode, Pixel
PixelGetColor, OutputVar, ox, oy, rgb
StringTrimLeft, OutputVar, OutputVar, 2
return outputvar
}
pxDist(x1, x2, y1, y2) {
global
DiffX := x1 - x2, DiffY := y1 - y2
Diffx :=Abs(Diffx), Diffy :=Abs(Diffy)
if diffx!=0
Diffx :=Diffx+1
if diffy!=0
Diffy := Diffy+1
return Sqrt(DiffX * DiffX + DiffY * DiffY)
}
rgbx_to_bgrx(string)
{
stringsplit, x, string
outx:= x5 . x6 . x3 . x4 . x1 . x2
return outx
}
drawchar(varname, chartodraw:="@", color:="")
{
global
stringsplit, draw, varname, _
out:= draw1 . "_" . draw2 . "_" draw3
guicontrol,, %out%, %chartodraw%
colorcell(out, color)
}
ColorCell(cell_to_paint, color:="red")
{
GuiControl, +c%color% , %cell_to_paint%
GuiControl, MoveDraw, % cell_to_paint
}
CellFont(cell, params:="", fontname:="")
{
Gui, Font, %params%, %fontname%
GuiControl, font , %cell%
GuiControl, MoveDraw, % cell
}
ObjFullyClone(obj)
{
nobj := obj.Clone()
for k,v in nobj
if IsObject(v)
nobj[k] := A_ThisFunc.(v)
return nobj
}
extractnumber(var)
{
GuiControlGet, varout,, %var%
NewVar := RegExReplace(varout, "\D")
return newvar
}
COMPARE(color1,color2) ;;http://www.dcmembers.com/skrommel/download/labelcontrol/
{
Loop,2
{
param:=A_Index
StringTrimLeft,color%param%,color%param%,2
Loop,3
{
StringLeft,c%param%%A_Index%,color%param%,2
value:=c%param%%A_Index%
c%param%%A_Index%=0x%value%
StringTrimLeft,color%param%,color%param%,2
}
}
difference:=(Abs(c11-c21)+Abs(c12-c22)+Abs(c13-c23))/3
Return difference
}
return
guiclose:
~esc::
exitapp
return
Clicks on Grid
shift & left click or F1.... Take point A
shift & right click or F2... Take point B
right click................... Switch between point A and B
control & left click........ Display colors
alt & left click.............. Display XY positions
clicks on Displayed infos
right click.................... View next infos
control & left click......... Copy value to clipboard
control+alt & left click.... Append value to clipboard
Feedback and suggestions to improve the code are welcome
Cheers
screenshots:
- 2.png (27.19 KiB) Viewed 2672 times
- 1.PNG (4.7 KiB) Viewed 2861 times