Advertisement
Najeebsk

COLOR2.ahk

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