Advertisement
Najeebsk

COLOR.ahk

Dec 31st, 2021
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  3. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  4. #SingleInstance, Force
  5.  
  6. ; Setup
  7. OnExit, Exit
  8. ;~ FileCreateDir, data
  9. ;~ FileInstall, data\pick_click.wav, data\pick_click.wav
  10. ColoretteIcon := A_ScriptFullPath
  11.  
  12. ; Hotkeys
  13. Hotkey, Rbutton, CatchColor ; HEX (Default)
  14. Hotkey, ^Rbutton, CatchColor ; RGB
  15.  
  16. ; Initiation
  17. Traytip, Colorette:, RIGHTCLICK to copy HEX value`nAdd CTRL for RGB value, 5
  18. SetSystemCursor("IDC_Cross") ; Reset in OnExit
  19.  
  20. If (FileExist("colorette.exe"))
  21.    Menu, Tray, Icon, Colorette.exe
  22.  
  23. ; MAIN LOOP: Pick Color
  24.  
  25. Loop
  26. {
  27.    CoordMode, Mouse, Screen
  28.    MouseGetPos X, Y
  29.    PixelGetColor Color, X, Y, RGB
  30.    ColorD := Color ; Build an int based variable
  31.    StringRight, color, color, 6 ; Removes 0x prefix
  32.    SetFormat, IntegerFast, d
  33.    ColorD += 0  ; Sets Var (which previously contained 11) to be 0xb.
  34.    ColorD .= ""  ; Necessary due to the "fast" mode.
  35.    ;~ ModColor := HexModify(Color, 1)
  36.    GetKeyState("LControl") ? ColorMessage := HextoRGB(Color, "Message") : ColorMessage := Color
  37.    Gui, 2:Color, %color%
  38.    Tooltip, %ColorMessage% ; (%ModColor%)
  39.    CoordMode, Pixel
  40.    mX := X - 30 ; Offset Tooltip from Mouse
  41.    mY := Y - 80
  42.    Gui, 2:-Caption +ToolWindow +LastFound +AlwaysOnTop +Border ; +0x400000 OR +Border
  43.    Gui, 2:Show, NoActivate x%mX% y%mY% w60 h60
  44. }
  45. return
  46.  
  47. CatchColor: ; Catch Hover'd color
  48. If (A_ThisHotkey = "^Rbutton")
  49.    Out := "RGB"
  50. If (FileExist("data\pick_click.wav"))
  51.    SoundPlay, data\pick_click.wav
  52. ; Continue
  53.  
  54. ColorPicked:
  55. StringRight, color, color, 6 ; Color HEX to RGB (or RGB to RGB)
  56.  
  57. If (Out = "RGB")
  58. {
  59.    OutColor := HexToRGB(Color)
  60.    OutMsg := HexToRGB(Color, "Message")
  61.    Clipboard := OutMsg
  62.    ;~ OutParse := HexToRGB(Color, "Parse") ; Returns "R,G,B"
  63. }
  64. else
  65. {
  66.    OutColor := Color
  67.    OutMsg :=  "#" . Color  
  68.    Clipboard := OutColor
  69. }
  70.  
  71. Traytip, Colorette:, %outmsg% picked
  72. RestoreCursors()
  73. Gui, 2:Destroy
  74. Sleep 500
  75. Hotkey, ^Rbutton, Off
  76. Hotkey, Rbutton, Off
  77.  
  78. Sleep 1500
  79. Gosub, Exit
  80. Return
  81.  
  82. esc::
  83. Exit:
  84. RestoreCursors()
  85. ExitApp
  86. return
  87.  
  88. ; FUNCTIONS
  89. ; : SetSystemCursor() and RestoreCursors()
  90. HexToRGB(Color, Mode="") ; Input: 6 characters HEX-color. Mode can be RGB, Message (R: x, G: y, B: z) or parse (R,G,B)
  91. {
  92.    ; If df, d is *16 and f is *1. Thus, Rx = R*16 while Rn = R*1
  93.    Rx := SubStr(Color, 1,1), Rn := SubStr(Color, 2,1)
  94.    Gx := SubStr(Color, 3,1), Gn := SubStr(Color, 4,1)
  95.    Bx := SubStr(Color, 5,1), Bn := SubStr(Color, 6,1)
  96.    
  97.    AllVars := "Rx|Rn|Gx|Gn|Bx|Bn"
  98.    Loop, Parse, Allvars, | ; Add the Hex values (A - F)
  99.    {
  100.       StringReplace, %A_LoopField%, %A_LoopField%, a, 10
  101.       StringReplace, %A_LoopField%, %A_LoopField%, b, 11
  102.       StringReplace, %A_LoopField%, %A_LoopField%, c, 12
  103.       StringReplace, %A_LoopField%, %A_LoopField%, d, 13
  104.       StringReplace, %A_LoopField%, %A_LoopField%, e, 14
  105.       StringReplace, %A_LoopField%, %A_LoopField%, f, 15
  106.    }
  107.    R := Rx*16+Rn
  108.    G := Gx*16+Gn
  109.    B := Bx*16+Bn
  110.    
  111.    If (Mode = "Message") ; Returns "R: 255 G: 255 B: 255"
  112.       Out := "R:" . R . " G:" . G . " B:" . B
  113.    else if (Mode = "Parse") ; Returns "255,255,255"
  114.       Out := R . "," . G . "," . B
  115.    else
  116.       Out := R . G . B ; Returns 255255255
  117.     return Out
  118. }
  119.  
  120. ; ToBase / FromBase by Lazslo @ http://www.autohotkey.com/forum/post-276241.html#276241
  121. ToBase(n,b) { ; n >= 0, 1 < b <= 36
  122.    Return (n < b ? "" : ToBase(n//b,b)) . ((d:=mod(n,b)) < 10 ? d : Chr(d+87))
  123. }
  124.  
  125. FromBase(s,b) { ; convert base b number s=strings of 0..9,a..z, to AHK number
  126.    Return (L:=StrLen(s))=0 ? "":(L>1 ? FromBase(SubStr(s,1,L-1),b)*b:0) + ((c:=Asc(SubStr(s,0)))>57 ? c-87:c-48)
  127. }
  128.  
  129. HexModify(n, Add="") ; MsgBox % HexModify("ffffff", -55)
  130. {
  131.    ;~ Hex := "0123456789abcdef"
  132.    R := ToBase(FromBase(SubStr(n, 1, 2), 16) + Add, 16)
  133.    G := ToBase(FromBase(SubStr(n, 3, 2), 16) + Add, 16)
  134.    B := ToBase(FromBase(SubStr(n, 5, 2), 16) + Add, 16)
  135.    return R . G . B
  136. }
  137.  
  138. RestoreCursors()
  139. {
  140.    SPI_SETCURSORS := 0x57
  141.    DllCall( "SystemParametersInfo", UInt,SPI_SETCURSORS, UInt,0, UInt,0, UInt,0 )
  142. }
  143.  
  144. SetSystemCursor( Cursor = "", cx = 0, cy = 0 )
  145. {
  146.    BlankCursor := 0, SystemCursor := 0, FileCursor := 0 ; init
  147.    
  148.    SystemCursors = 32512IDC_ARROW,32513IDC_IBEAM,32514IDC_WAIT,32515IDC_CROSS
  149.    ,32516IDC_UPARROW,32640IDC_SIZE,32641IDC_ICON,32642IDC_SIZENWSE
  150.    ,32643IDC_SIZENESW,32644IDC_SIZEWE,32645IDC_SIZENS,32646IDC_SIZEALL
  151.    ,32648IDC_NO,32649IDC_HAND,32650IDC_APPSTARTING,32651IDC_HELP
  152.    
  153.    If Cursor = ; empty, so create blank cursor
  154.    {
  155.       VarSetCapacity( AndMask, 32*4, 0xFF ), VarSetCapacity( XorMask, 32*4, 0 )
  156.       BlankCursor = 1 ; flag for later
  157.    }
  158.    Else If SubStr( Cursor,1,4 ) = "IDC_" ; load system cursor
  159.    {
  160.       Loop, Parse, SystemCursors, `,
  161.       {
  162.          CursorName := SubStr( A_Loopfield, 6, 15 ) ; get the cursor name, no trailing space with substr
  163.          CursorID := SubStr( A_Loopfield, 1, 5 ) ; get the cursor id
  164.          SystemCursor = 1
  165.          If ( CursorName = Cursor )
  166.          {
  167.             CursorHandle := DllCall( "LoadCursor", Uint,0, Int,CursorID )  
  168.             Break              
  169.          }
  170.       }  
  171.       If CursorHandle = ; invalid cursor name given
  172.       {
  173.          Msgbox,, SetCursor, Error: Invalid cursor name
  174.          CursorHandle = Error
  175.       }
  176.    }  
  177.    Else If FileExist( Cursor )
  178.    {
  179.       SplitPath, Cursor,,, Ext ; auto-detect type
  180.       If Ext = ico
  181.          uType := 0x1  
  182.       Else If Ext in cur,ani
  183.          uType := 0x2      
  184.       Else ; invalid file ext
  185.       {
  186.          Msgbox,, SetCursor, Error: Invalid file type
  187.          CursorHandle = Error
  188.       }      
  189.       FileCursor = 1
  190.    }
  191.    Else
  192.    {  
  193.       Msgbox,, SetCursor, Error: Invalid file path or cursor name
  194.       CursorHandle = Error ; raise for later
  195.    }
  196.    If CursorHandle != Error
  197.    {
  198.       Loop, Parse, SystemCursors, `,
  199.       {
  200.          If BlankCursor = 1
  201.          {
  202.             Type = BlankCursor
  203.             %Type%%A_Index% := DllCall( "CreateCursor"
  204.             , Uint,0, Int,0, Int,0, Int,32, Int,32, Uint,&AndMask, Uint,&XorMask )
  205.             CursorHandle := DllCall( "CopyImage", Uint,%Type%%A_Index%, Uint,0x2, Int,0, Int,0, Int,0 )
  206.             DllCall( "SetSystemCursor", Uint,CursorHandle, Int,SubStr( A_Loopfield, 1, 5 ) )
  207.          }        
  208.          Else If SystemCursor = 1
  209.          {
  210.             Type = SystemCursor
  211.             CursorHandle := DllCall( "LoadCursor", Uint,0, Int,CursorID )  
  212.             %Type%%A_Index% := DllCall( "CopyImage"
  213.             , Uint,CursorHandle, Uint,0x2, Int,cx, Int,cy, Uint,0 )      
  214.             CursorHandle := DllCall( "CopyImage", Uint,%Type%%A_Index%, Uint,0x2, Int,0, Int,0, Int,0 )
  215.             DllCall( "SetSystemCursor", Uint,CursorHandle, Int,SubStr( A_Loopfield, 1, 5 ) )
  216.          }
  217.          Else If FileCursor = 1
  218.          {
  219.             Type = FileCursor
  220.             %Type%%A_Index% := DllCall( "LoadImageA"
  221.             , UInt,0, Str,Cursor, UInt,uType, Int,cx, Int,cy, UInt,0x10 )
  222.             DllCall( "SetSystemCursor", Uint,%Type%%A_Index%, Int,SubStr( A_Loopfield, 1, 5 ) )        
  223.          }          
  224.       }
  225.    }  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement