Advertisement
Najeebsk

COLOR-PICKER2.ahk

Dec 31st, 2021
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance force
  2. ProgramName = Najeeb Color Picker
  3. Version = 2.2
  4. OnMessage(0x0133, "Control_Colors") ;WM_CTLCOLOREDIT = 0x0133, WM_CTLCOLORLISTBOX = 0x0134, ;WM_CTLCOLORSTATIC = 0x0138
  5. OnMessage(0x0138, "Control_Colors")
  6. Gui 1: font, s10 , Verdana
  7. Gui 1: default
  8. Gui 1: Add, Text, x12 y12 w280 h50 , Select a preset color from the drop down list. The Hex-Code will appear in the Edit Box.
  9. Gui 1: Add, DropDownList, x22 y82 w120 vdropdownlist gDropDownList hwndhcbx H400 Sort Choose13,Red|Yellow|Blue|Green|Orange|Black|Silver|Lime|Gray|Olive|White|Maroon|Navy|Purple|Teal|Fuchsia|  
  10. Gui 1: Add, Text, x12 y132 w280 h50 , Press the button "Color Picker" to get the exact color you want. The Hex-Code will appear in the Edit Box.
  11. Gui 1: Add, Button, x22 y202 w120 h30 , Color Picker
  12. Gui 1: Add, Text, x12 y252 w280 h50  , Type a Hex-Code in the Edit Box, and click Apply Color to see that color.
  13. Gui 1: Add, Button, x22 y322 w120 h30 , Apply Color
  14. Gui 1: Add, Text, x12 y372 w280 h50 , Real Time will put the Hex-Code of the color under your mouse cursor in the Edit Box.
  15. Gui 1: Add, Button, x22 y432 w120 h30 , Real Time
  16. Gui 1: Add, Button, x85 y+25 gCopyToClipboard ,Copy to Clipboard
  17.  
  18. Gui 1: font, s11 , Verdana
  19. Gui 1: Add, Edit, x152 y82  w130 h30 center veditbox1, C0C0C0
  20. Gui 1: Add, Edit, x152 y202 w130 h30 center veditbox2,
  21. Gui 1: Add, Edit, x152 y322 w130 h30 center veditbox3 gHexEditBox,
  22. Gui 1: Add, Edit, x152 y432 w130 h30 center veditbox4,
  23. loop 4
  24.  Control_Colors("Editbox" a_index, "Set", "0xC0C0C0", "0x000000")
  25.  
  26. Gui 1: Show, h530 w306, %ProgramName%
  27. winset, AlwaysOnTop, on, %ProgramName%
  28. WinGet,WinTitle2,ID,%ProgramName%
  29. Gui 1: color, c0c0c0
  30. PostMessage, 0x153, -1, 25,, ahk_id %hcbx%  ; Set height of selection field for dropdownlist.
  31. PostMessage, 0x153,  0, 20,, ahk_id %hcbx%  ; Set height of list items for dropdownlist.
  32. SetTimer,PreventFocusOnNoInputEditBoxes,50
  33. Return
  34. PreventFocusOnNoInputEditBoxes: ;prevents user input to editboxes. Disabling 3 out of 4 cause all 4 not to match
  35. ControlGetFocus, OutputVar , ahk_id %WinTitle2%
  36. if OutPutVar = Edit1
  37.  ControlFocus, button4,ahk_id %WinTitle2%
  38. if OutPutVar = Edit2
  39.  ControlFocus, button4,ahk_id %WinTitle2%
  40. if OutPutVar = Edit4
  41.  ControlFocus, button4,ahk_id %WinTitle2%
  42. return
  43. DropDownList: ;Editbox1
  44. stopbreak = 1 ;just incase Real Time is still looping
  45. Guicontrolget, dropdownlist
  46. Gui 1: font, c000000 s10, Verdana
  47. loop 4
  48.  {
  49.   GuiControl,font,static%A_Index%
  50.   ControlSetText , Edit%a_index%, , %ProgramName%
  51.  }
  52.  
  53. if dropdownlist = Red
  54.  ColorCode = ff0000
  55. if dropdownlist = Yellow
  56.  ColorCode = FFFF00
  57. if dropdownlist = Blue
  58.  ColorCode = 0000FF
  59. if dropdownlist = Green
  60.  ColorCode = 008000
  61. if dropdownlist = Purple
  62.  ColorCode = 800080
  63. if dropdownlist = Orange
  64.  ColorCode = ff8000
  65. if dropdownlist = Silver
  66.  ColorCode = C0C0C0
  67. if dropdownlist = Lime
  68.  ColorCode = 00FF00
  69. if dropdownlist = Gray
  70.  ColorCode = 808080
  71. if dropdownlist = Olive  
  72.  ColorCode = 808000
  73. if dropdownlist = Maroon
  74.  ColorCode = 800000
  75. if dropdownlist = Purple
  76.  ColorCode = 800080
  77. if dropdownlist = Teal
  78.  ColorCode = 008080  
  79. if dropdownlist = Fuchsia
  80.  ColorCode = FF00FF
  81. if dropdownlist = White
  82.  ColorCode = FFFFFF
  83. if dropdownlist = Navy
  84.  ColorCode = 000080
  85. if dropdownlist = Black
  86.  {
  87.   ColorCode = 000000
  88.   Gui 1: font, cFFFFFF , Verdana
  89.   loop 4
  90.     GuiControl,font,static%A_Index%
  91.  }
  92.  
  93. Gui 1: color, %colorcode%,
  94. ControlSetText , Edit1, %ColorCode%, %ProgramName%
  95. StringSplit,Digit,ColorCode
  96. ColorCode := ( Digit5 Digit6 Digit3 Digit4 Digit1 Digit2)
  97. loop 4
  98.  {
  99.   Control_Colors("Editbox" a_index, "Set", "0x" ColorCode, "0x000000")
  100.   WinSet,Redraw,,%ProgramName%
  101.  }
  102. return
  103.  
  104. ButtonColorPicker: ;Editbox2
  105. stopbreak = 1 ;just incase Real Time is still looping
  106. Gui 1: font, c000000 s10, Verdana
  107. loop 4
  108.  {
  109.   GuiControl,font,static%A_Index%
  110.   ControlSetText , Edit%a_index%, , %ProgramName%
  111.  }
  112. CmnDlg_Color( color:=0000FF)
  113. if color =
  114.  Return
  115. Gui 1: Color, %Color%
  116. StringTrimleft, color, color, 2
  117. ControlSetText , Edit2, %color%, %ProgramName%
  118. StringSplit,Digit,Color
  119. Color := ( Digit5 Digit6 Digit3 Digit4 Digit1 Digit2)
  120. loop 4
  121.  {
  122.   Control_Colors("Editbox" a_index, "Set", "0x" Color, "0x000000")
  123.   WinSet,Redraw,,%ProgramName%
  124.  }
  125. Return
  126.  
  127. ButtonApplyColor: ;Editbox3
  128. stopbreak = 1 ;just incase Real Time is still looping
  129. NotAValidColor1 = 0
  130. NotAValidColor2 = 0
  131. ControlGettext,Value,Edit3,%ProgramName%
  132. gosub ValidateHexColorCode
  133.   if ( NotAValidColor1 = 0 and NotAValidColor2 = 0 )
  134.    {
  135.     Gui 1: font, c000000 s10, Verdana
  136.     StringSplit,Digit,Value
  137.     Color := ( Digit5 Digit6 Digit3 Digit4 Digit1 Digit2)
  138.     loop 4
  139.      {
  140.       GuiControl,font,static%A_Index%
  141.       Control_Colors("Editbox" a_index, "Set", "0x" color, "0x000000")
  142.       WinSet,Redraw,,%ProgramName%
  143.       if A_index <> 3
  144.        ControlSetText , Edit%a_index%, , %ProgramName%
  145.      }  
  146.     Gui 1: Color, %Value%
  147.    }
  148. Return
  149. HexEditBox: ;clears Edit1,2, and 4 if anything is typed in Editbox3
  150. If RealTimeIsRunning = 1
  151.  return
  152. ControlSettext, Edit1,,%ProgramName%
  153. ControlSettext, Edit2,,%ProgramName%
  154. ControlSettext, Edit4,,%ProgramName%
  155. return
  156.  
  157. ButtonRealTime: ;Editbox 4
  158. Control, disable, , ComboBox1, %ProgramName%
  159. Control, disable, , Button1, %ProgramName%
  160. Control, disable, , Button2, %ProgramName%
  161. Control, disable, , Button3, %ProgramName%
  162. Control, disable, , Button4, %ProgramName%
  163. RealTimeIsRunning = 1 ;stop HexEditBox label glabel from running when ControlSetText is executed
  164. stopbreak = 0
  165.  
  166. gui 1: font, c000000 s10 , Verdana
  167. loop 4
  168.  GuiControl,font,static%A_Index%
  169. ControlSetText , Edit1, , %ProgramName%
  170. ControlSetText , Edit2, , %ProgramName%
  171. ControlSetText , Edit3, , %ProgramName%
  172.  
  173. ToolTip,Press Ctrl e to stop Real Time
  174. Loop
  175.  {
  176.   MouseGetPos , xpos, ypos, WinTitle1, ControlID,
  177.   PixelGetColor , color, %xpos%, %ypos%, rgb
  178.   StringSplit,Digit,Color
  179.   ControlColor := (Digit1 Digit2 Digit7 Digit8 Digit5 Digit6 Digit3 Digit4)
  180.   if ( Color1 <> Color and WinTitle1 <> WinTitle2 )
  181.    {
  182.     StringTrimLeft,Color,Color,2
  183.     controlsettext, edit4, %Color%, %ProgramName%
  184.     Gui 1: Color, %Color%
  185.     loop 4
  186.      Control_Colors("Editbox" a_index, "Set", ControlColor, "0x000000")
  187.     Color1 = %Color%
  188.    }
  189.   if stopbreak = 1
  190.    {
  191.     RealTimeIsRunning = 0
  192.     Control, enable, , ComboBox1, %ProgramName%
  193.     Control, enable, , Button1, %ProgramName%
  194.     Control, enable, , Button2, %ProgramName%
  195.     Control, enable, , Button3, %ProgramName%
  196.     Control, enable, , Button4, %ProgramName%
  197.     ToolTip
  198.     break
  199.    }
  200.  }
  201. stopbreak = 0
  202. Return
  203.  
  204. CopyToClipboard:
  205. NotAValidColor1 = 0
  206. NotAValidColor2 = 0
  207. FoundEditBox3Value = 0
  208. Loop 4
  209.  {
  210.   ControlGettext,Value,Edit%a_index%,%ProgramName%
  211.   FoundEditBoxValue = %a_index% ;need to know if EditBox3 is the one with contents so it can be checked with gosub ValidateHexColorCode below
  212.   if Value <>
  213.    break
  214.  }
  215.  
  216.  if FoundEditBoxValue = 3
  217.   gosub ValidateHexColorCode ;sets NotAValidColor1 and/or NotAValidColor2 to 1 if EditBox3 color hex code is not a valid color code
  218.  
  219.  If ( NotAValidColor1 = 0 and NotAValidColor2 = 0 )
  220.   Clipboard = %Value%
  221.  return
  222.  
  223. ValidateHexColorCode:
  224. if Value <>
  225.  {
  226.   Value2 = %Value%
  227.   StringLen,Len,Value
  228.   if (Len > 6)
  229.    {                                                                                                        
  230.     TempCount := ( Len - 2 )
  231.     StringTrimRight,1st2CharsOnLeft,Value2, %TempCount%
  232.     if ( 1st2CharsOnLeft <> "0x" )
  233.      NotAValidColor1 = 1
  234.     else
  235.      StringTrimLeft,Value,Value,2
  236.    }
  237.   var := RegExMatch(Value, "i)^[0-9a-fA-F]+$") ;thanks to majkinetor, skan and polyethene, code found here ;http://www.autohotkey.com/community/viewtopic.php?f=1&t=13545&start=60
  238.   if ( Var = 0 or NotAValidColor1 = 1 or NotAValidColor2 = 1 or ( Len <> 6 and Len <> 8) or Len > 8 )
  239.    {
  240.     Gui 1: +Disabled
  241.     Gui 2: -MinimizeBox
  242.     Gui 2: font, s12, Verdana
  243.     Gui 2: add, text,, "%Value2%" is not a valid color code
  244.     Gui 2: show,autosize, Invalid Entry
  245.     Gui 2: color, DAC69F
  246.     winset, AlwaysOnTop, On, Invalid Entry
  247.     WinWaitClose,Invalid Entry
  248.     NotAValidColor2 = 1
  249.    }
  250.  }
  251. else
  252.  NotAValidColor1 = 1
  253. return
  254.  
  255. ^e::
  256. stopbreak=1
  257. return
  258.  
  259. GuiClose:
  260. ExitApp
  261.  
  262. 2GuiClose:
  263. Gui 1: -Disabled
  264. Gui 2: destroy
  265. return
  266.  
  267.  ;color function returns user choise in var color
  268. ;usage   CmnDlg_Color( color:=0000FF)
  269. CmnDlg_Color(ByRef pColor, hGui=0){
  270.   ;covert from rgb
  271.     clr := ((pColor & 0xFF) << 16) + (pColor & 0xFF00) + ((pColor >> 16) & 0xFF)
  272.  
  273.     VarSetCapacity(sCHOOSECOLOR, 0x24, 0)
  274.     VarSetCapacity(aChooseColor, 64, 0)
  275.  
  276.     NumPut(0x24,         sCHOOSECOLOR, 0)      ; DWORD lStructSize
  277.     NumPut(hGui,         sCHOOSECOLOR, 4)      ; HWND hwndOwner (makes dialog "modal").
  278.     NumPut(clr,          sCHOOSECOLOR, 12)     ; clr.rgbResult
  279.     NumPut(&aChooseColor,sCHOOSECOLOR, 16)     ; COLORREF *lpCustColors
  280.     NumPut(0x00000103,   sCHOOSECOLOR, 20)     ; Flag: CC_ANYCOLOR || CC_RGBINIT
  281.  
  282.     nRC := DllCall("comdlg32\ChooseColorA", str, sCHOOSECOLOR)  ; Display the dialog.
  283.     if (errorlevel <> 0) || (nRC = 0)
  284.        return  false
  285.  
  286.  
  287.     clr := NumGet(sCHOOSECOLOR, 12)
  288.    
  289.     oldFormat := A_FormatInteger
  290.     SetFormat, integer, hex  ; Show RGB color extracted below in hex format.
  291.  
  292.  ;convert to rgb
  293.     pColor := (clr & 0xff00) + ((clr & 0xff0000) >> 16) + ((clr & 0xff) << 16)
  294.     StringTrimLeft, pColor, pColor, 2
  295.     loop, % 6-strlen(pColor)
  296.         pColor=0%pColor%
  297.     pColor=0x%pColor%
  298.     SetFormat, integer, %oldFormat%
  299.  
  300.     return true
  301. }
  302. ;Control_Colors--------------------------------------------------------
  303. Control_Colors(wParam, lParam, Msg, Hwnd) {
  304.     Static Controls := {}
  305.    If (lParam = "Set") {
  306.       If !(CtlHwnd := wParam + 0)
  307.          GuiControlGet, CtlHwnd, Hwnd, %wParam%
  308.       If !(CtlHwnd + 0)
  309.          Return False
  310.       Controls[CtlHwnd, "CBG"] := Msg + 0
  311.       Controls[CtlHwnd, "CTX"] := Hwnd + 0
  312.       Return True
  313.    }
  314.    ; Critical
  315.    If (Msg = 0x0133 Or Msg = 0x0134 Or Msg = 0x0138) {
  316.       If Controls.HasKey(lParam) {
  317.          If (Controls[lParam].CTX >= 0)
  318.             DllCall("Gdi32.dll\SetTextColor", "Ptr", wParam, "UInt", Controls[lParam].CTX)
  319.          DllCall("Gdi32.dll\SetBkColor", "Ptr", wParam, "UInt", Controls[lParam].CBG)
  320.          Return DllCall("Gdi32.dll\CreateSolidBrush", "UInt", Controls[lParam].CBG)
  321.       }
  322.    }
  323.  }
  324. ;----------------------------------------------------------------------
  325.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement