Automage

color picker.ahk

Nov 10th, 2026
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires AutoHotkey >=1.1.36 <1.2
  2. SetWorkingDir % A_ScriptDir
  3. #HotkeyInterval 99000000
  4. #KeyHistory 0
  5. #MaxHotkeysPerInterval 99000000
  6. #MaxThreads 255
  7. #MaxThreadsPerHotkey 1
  8. #NoEnv
  9. #Persistent
  10. #SingleInstance force
  11. CoordMode, Mouse, Screen
  12. CoordMode, Pixel, Screen
  13. CoordMode, Tooltip, Screen
  14. DetectHiddenWindows, On
  15. ListLines, Off
  16. Process, Priority,, A
  17. SendMode, Input
  18. SetBatchLines, -1
  19. SetControlDelay, 0
  20. SetKeyDelay, -1
  21. SetMouseDelay, -1
  22. SetWinDelay, -1
  23. Thread, Interrupt, 0
  24. #MenuMaskKey vkE8
  25.  
  26. ; ////////////////////////////////////////////////////////////////////////////
  27. ; ============================================================================
  28. ; COLOR PICKER
  29. ; Standalone RGB / HSV / HEX color picker for AutoHotkey v1.1.
  30. ; ============================================================================
  31.  
  32. global cp_hue := 126
  33. global cp_sat := 100
  34. global cp_val := 100
  35. global cp_r := 0
  36. global cp_g := 255
  37. global cp_b := 26
  38. global cp_hex := "00FF1A"
  39. global cp_updating := false
  40. global cp_drag_mode := ""
  41. global cp_drag_hwnd := 0
  42.  
  43. global cp_sv_width := 250
  44. global cp_sv_height := 250
  45. global cp_hue_width := 20
  46. global cp_hue_height := 250
  47. global cp_preview_width := 104
  48. global cp_preview_height := 76
  49.  
  50. global cp_hwnd_main := 0
  51. global cp_hwnd_sv := 0
  52. global cp_hwnd_hue := 0
  53. global cp_hwnd_preview := 0
  54. global cp_hwnd_edit_r := 0
  55. global cp_hwnd_edit_g := 0
  56. global cp_hwnd_edit_b := 0
  57. global cp_hwnd_edit_h := 0
  58. global cp_hwnd_edit_s := 0
  59. global cp_hwnd_edit_v := 0
  60. global cp_hwnd_edit_hex := 0
  61. global cp_hwnd_eyedropper_icon := 0
  62.  
  63. global cp_bitmap_sv := 0
  64. global cp_bitmap_sv_base := 0
  65. global cp_bitmap_hue := 0
  66. global cp_bitmap_hue_base := 0
  67. global cp_bitmap_preview := 0
  68. global cp_bitmap_overlay := 0
  69. global cp_bitmap_eyedropper := 0
  70.  
  71. global cp_bits_sv := 0
  72. global cp_bits_sv_base := 0
  73. global cp_bits_hue := 0
  74. global cp_bits_hue_base := 0
  75. global cp_bits_preview := 0
  76. global cp_bits_overlay := 0
  77.  
  78. global cp_last_sv_hue := -1
  79. global cp_hue_base_ready := false
  80.  
  81. global cp_drag_timer_pending := false
  82. global cp_pending_mouse_x := 0
  83. global cp_pending_mouse_y := 0
  84.  
  85. global cp_gdip_token := 0
  86. global cp_gdip_module := 0
  87. global cp_gdip_ready := false
  88. global cp_pen_outer := 0
  89. global cp_pen_inner := 0
  90. global cp_pen_preview := 0
  91.  
  92. ; --- Screen eyedropper state -------------------------------------------------
  93. global cp_screen_pick_active := false
  94. global cp_screen_pick_cursor_saved := false
  95.  
  96. ; --- Screen pick overlay (layered per-pixel-alpha swatch panel) --------------
  97. global cp_pp_w := 308
  98. global cp_pp_h := 94
  99. global cp_pp_hwnd := 0
  100. global cp_pp_dc := 0
  101. global cp_pp_dib := 0
  102. global cp_pp_bits := 0
  103. global cp_pp_obm := 0
  104. global cp_pp_graphics := 0
  105. global cp_pp_visible := false
  106. global cp_pp_font_families := {}
  107.  
  108. OnExit("CP_Cleanup")
  109.  
  110. ; -------------------------- GUI setup ----------------------------------------
  111.  
  112. Gui, Color, 191B20, 292D34
  113. Gui, Margin, 0, 0
  114. Gui, -MinimizeBox
  115.  
  116. ; Header: title left, primary action right, matching 24 px margins on both sides.
  117. Gui, Font, s15 Bold cF4F6FA, Segoe UI
  118. Gui, Add, Text, x24 y14 w420 h31 BackgroundTrans, COLOR PICKER
  119. Gui, Font, s9 c929BA8, Segoe UI
  120. Gui, Add, Text, x25 y43 w480 h19 BackgroundTrans, Pick a color visually or enter its exact values
  121.  
  122. ; --- Screen eyedropper button (top-right, aligned with the title row) --------
  123. ; Icon bitmap and label share the same 3A414C face so they read as one button.
  124. ; Right edge lands at 656, giving a 24 px margin symmetric with the left edge.
  125. Gui, Add, Picture, x516 y14 w28 h28 hwndcp_hwnd_eyedropper_icon gCP_ScreenPick +0xE
  126. Gui, Font, s9 cFFFFFF, Segoe UI
  127. Gui, Add, Text, x544 y14 w112 h28 Background3A414C +0x200 Center gCP_ScreenPick, PICK FROM SCREEN
  128.  
  129. Gui, Add, Text, x14 y63 w330 h285 Background22262D
  130. Gui, Add, Text, x354 y63 w312 h363 Background22262D
  131.  
  132. Gui, Font, s9 cAAB2C0, Segoe UI
  133. Gui, Add, Text, x24 y69 w280 h16 BackgroundTrans, SATURATION  →  /  VALUE  ↓
  134. Gui, Add, Text, x23 y88 w252 h252 Background626B79
  135. Gui, Add, Picture, x24 y89 w250 h250 hwndcp_hwnd_sv +0xE
  136. Gui, Add, Text, x283 y88 w22 h252 Background626B79
  137. Gui, Add, Picture, x284 y89 w20 h250 hwndcp_hwnd_hue +0xE
  138.  
  139. Gui, Add, Text, x365 y69 w250 h16 BackgroundTrans, LIVE PREVIEW
  140. Gui, Add, Picture, x365 y91 w104 h76 hwndcp_hwnd_preview +0xE +0x200
  141.  
  142. Gui, Font, s17 cFFFFFF, Consolas
  143. Gui, Add, Text, x484 y105 w176 h34 vCP_HexReadout BackgroundTrans, #00FF1A
  144. Gui, Font, s9 c929BA8, Segoe UI
  145. Gui, Add, Text, x486 y139 w150 h18 BackgroundTrans, HEX COLOR
  146.  
  147. Gui, Font, s9 cAAB2C0, Segoe UI
  148. Gui, Add, Text, x365 y190 w85 h18 BackgroundTrans, RGB  ·  0–255
  149. Gui, Add, Text, x465 y190 w190 h18 BackgroundTrans, HSV  ·  H 0–360 / S,V 0–100`%
  150.  
  151. Gui, Font, s9 cF0F2F5, Segoe UI
  152. Gui, Add, Text, x365 y216 w16 h25 +0x200 BackgroundTrans, R
  153. Gui, Add, Text, x365 y248 w16 h25 +0x200 BackgroundTrans, G
  154. Gui, Add, Text, x365 y280 w16 h25 +0x200 BackgroundTrans, B
  155. Gui, Add, Text, x465 y216 w16 h25 +0x200 BackgroundTrans, H
  156. Gui, Add, Text, x465 y248 w16 h25 +0x200 BackgroundTrans, S
  157. Gui, Add, Text, x465 y280 w16 h25 +0x200 BackgroundTrans, V
  158.  
  159. Gui, Font, s10 cF4F6FA, Consolas
  160. Gui, Add, Edit, x383 y212 w66 h29 vCP_EditR hwndcp_hwnd_edit_r gCP_RGBChanged Center Limit3
  161. Gui, Add, Edit, x383 y244 w66 h29 vCP_EditG hwndcp_hwnd_edit_g gCP_RGBChanged Center Limit3
  162. Gui, Add, Edit, x383 y276 w66 h29 vCP_EditB hwndcp_hwnd_edit_b gCP_RGBChanged Center Limit3
  163. Gui, Add, Edit, x483 y212 w66 h29 vCP_EditH hwndcp_hwnd_edit_h gCP_HSVChanged Center Limit3
  164. Gui, Add, Edit, x483 y244 w66 h29 vCP_EditS hwndcp_hwnd_edit_s gCP_HSVChanged Center Limit3
  165. Gui, Add, Edit, x483 y276 w66 h29 vCP_EditV hwndcp_hwnd_edit_v gCP_HSVChanged Center Limit3
  166.  
  167. Gui, Font, s9 cAAB2C0, Segoe UI
  168. Gui, Add, Text, x553 y216 w16 h25 +0x200 BackgroundTrans, °
  169. Gui, Add, Text, x553 y248 w16 h25 +0x200 BackgroundTrans, `%
  170. Gui, Add, Text, x553 y280 w16 h25 +0x200 BackgroundTrans, `%
  171.  
  172. Gui, Font, s9 cAAB2C0, Segoe UI
  173. Gui, Add, Text, x365 y323 w250 h18 BackgroundTrans, HEX  ·  #RRGGBB
  174. Gui, Font, s10 cF4F6FA, Consolas
  175. Gui, Add, Edit, x365 y345 w174 h30 vCP_EditHex hwndcp_hwnd_edit_hex gCP_HEXChanged Center Limit8
  176.  
  177. Gui, Font, s9 cFFFFFF, Segoe UI
  178. Gui, Add, Text, x548 y345 w96 h30 Background3A414C +0x200 Center gCP_CopyHex, COPY HEX
  179. Gui, Add, Text, x365 y390 w86 h27 Background343A43 +0x200 Center gCP_CopyRGB, COPY RGB
  180. Gui, Add, Text, x458 y390 w86 h27 Background343A43 +0x200 Center gCP_CopyCSS, COPY CSS
  181. Gui, Add, Text, x555 y390 w89 h27 Background343A43 +0x200 Center gCP_Random, RANDOM
  182.  
  183. Gui, Font, s8 c929BA8, Segoe UI
  184. Gui, Add, Text, x24 y351 w310 h34 BackgroundTrans, Drag the square to adjust saturation and value.`nDrag the hue strip to change the base color.
  185. Gui, Font, s8 cAAB2C0, Segoe UI
  186. Gui, Add, Text, x24 y402 w620 h17 vCP_Status BackgroundTrans, Ready  ·  Select a color, edit its values, or pick from screen.
  187.  
  188. Gui, +Hwndcp_hwnd_main
  189.  
  190. ; -------------------------- Bitmap initialization ----------------------------
  191.  
  192. cp_bitmap_sv := CP_CreateDIB(cp_sv_width, cp_sv_height, cp_bits_sv, 32)
  193. cp_bitmap_sv_base := CP_CreateDIB(cp_sv_width, cp_sv_height, cp_bits_sv_base, 32)
  194. cp_bitmap_hue := CP_CreateDIB(cp_hue_width, cp_hue_height, cp_bits_hue, 32)
  195. cp_bitmap_hue_base := CP_CreateDIB(cp_hue_width, cp_hue_height, cp_bits_hue_base, 32)
  196. cp_bitmap_preview := CP_CreateDIB(cp_preview_width, cp_preview_height, cp_bits_preview, 32)
  197. cp_bitmap_overlay := CP_CreateDIB(1, cp_sv_height, cp_bits_overlay, 32)
  198.  
  199. if (!cp_bitmap_sv || !cp_bitmap_sv_base || !cp_bitmap_hue || !cp_bitmap_hue_base || !cp_bitmap_preview || !cp_bitmap_overlay) {
  200.     MsgBox, 16, Color Picker, Windows could not create the color-picker bitmaps.
  201.     ExitApp
  202. }
  203.  
  204. if (!CP_BuildBlackOverlay()) {
  205.     MsgBox, 16, Color Picker, Windows could not initialize the color-picker overlay.
  206.     ExitApp
  207. }
  208.  
  209. ; GDI+ is used for antialiased selection indicators, the preview border, the
  210. ; eyedropper icon, and the screen-pick swatch panel.
  211. if (!CP_GdipInitialize())
  212.     GuiControl,, CP_Status, GDI+ is unavailable; using standard Windows drawing.
  213.  
  214. ; Draw the eyedropper icon into its own 28x28 bitmap (background baked in).
  215. cp_bitmap_eyedropper := CP_CreateEyedropperBitmap(28, 28)
  216. if (cp_bitmap_eyedropper) {
  217.     DllCall("SendMessage", "Ptr", cp_hwnd_eyedropper_icon, "UInt", 0x172, "Ptr", 0, "Ptr", cp_bitmap_eyedropper, "Ptr")
  218. }
  219.  
  220. Gui, Show, w680 h435, Color Picker
  221.  
  222. ; Attach each display bitmap once. Redraws update the DIB memory and invalidate
  223. ; the existing control instead of repeatedly replacing its image handle.
  224. DllCall("SendMessage", "Ptr", cp_hwnd_sv, "UInt", 0x172, "Ptr", 0, "Ptr", cp_bitmap_sv, "Ptr")
  225. DllCall("SendMessage", "Ptr", cp_hwnd_hue, "UInt", 0x172, "Ptr", 0, "Ptr", cp_bitmap_hue, "Ptr")
  226. DllCall("SendMessage", "Ptr", cp_hwnd_preview, "UInt", 0x172, "Ptr", 0, "Ptr", cp_bitmap_preview, "Ptr")
  227.  
  228. OnMessage(0x201, "CP_WM_LBUTTONDOWN")
  229. OnMessage(0x200, "CP_WM_MOUSEMOVE")
  230. OnMessage(0x202, "CP_WM_LBUTTONUP")
  231. OnMessage(0x215, "CP_WM_CAPTURECHANGED")
  232.  
  233. CP_RGBToHSV(cp_r, cp_g, cp_b, cp_hue, cp_sat, cp_val)
  234. CP_UpdateFields()
  235. CP_RedrawAll()
  236. return
  237.  
  238. ; -------------------------- GUI event handlers -------------------------------
  239.  
  240. GuiClose:
  241. GuiEscape:
  242.     ExitApp
  243. return
  244.  
  245. CP_RGBChanged:
  246.    if (cp_updating)
  247.         return
  248.  
  249.     GuiControlGet, rText,, CP_EditR
  250.     GuiControlGet, gText,, CP_EditG
  251.     GuiControlGet, bText,, CP_EditB
  252.  
  253.     if (!CP_IsIntegerInRange(rText, 0, 255) || !CP_IsIntegerInRange(gText, 0, 255) || !CP_IsIntegerInRange(bText, 0, 255))
  254.         return
  255.  
  256.     ; Ignore programmatic echoes. Without this guard, dragging the square to
  257.     ; V=0 fires a field update that re-runs RGB->HSV and snaps the marker to
  258.     ; the bottom-left corner because hue is undefined for black.
  259.     if (rText + 0 = cp_r && gText + 0 = cp_g && bText + 0 = cp_b)
  260.         return
  261.  
  262.     cp_r := rText + 0
  263.     cp_g := gText + 0
  264.     cp_b := bText + 0
  265.     CP_RGBToHSV(cp_r, cp_g, cp_b, cp_hue, cp_sat, cp_val)
  266.     CP_UpdateFields(DllCall("GetFocus", "Ptr"))
  267.     CP_RedrawAll()
  268. return
  269.  
  270. CP_HSVChanged:
  271.    if (cp_updating)
  272.         return
  273.  
  274.     GuiControlGet, hText,, CP_EditH
  275.     GuiControlGet, sText,, CP_EditS
  276.     GuiControlGet, vText,, CP_EditV
  277.  
  278.     if (!CP_IsIntegerInRange(hText, 0, 360) || !CP_IsIntegerInRange(sText, 0, 100) || !CP_IsIntegerInRange(vText, 0, 100))
  279.         return
  280.  
  281.     if (hText + 0 = cp_hue && sText + 0 = cp_sat && vText + 0 = cp_val)
  282.         return
  283.  
  284.     cp_hue := hText + 0
  285.     cp_sat := sText + 0
  286.     cp_val := vText + 0
  287.     CP_HSVToRGB(cp_hue, cp_sat, cp_val, cp_r, cp_g, cp_b)
  288.     CP_UpdateFields(DllCall("GetFocus", "Ptr"))
  289.     CP_RedrawAll()
  290. return
  291.  
  292. CP_HEXChanged:
  293.    if (cp_updating)
  294.         return
  295.  
  296.     GuiControlGet, hexText,, CP_EditHex
  297.  
  298.     if (!CP_ParseHex(hexText, newR, newG, newB))
  299.         return
  300.  
  301.     if (newR = cp_r && newG = cp_g && newB = cp_b)
  302.         return
  303.  
  304.     cp_r := newR
  305.     cp_g := newG
  306.     cp_b := newB
  307.     CP_RGBToHSV(cp_r, cp_g, cp_b, cp_hue, cp_sat, cp_val)
  308.     CP_UpdateFields(DllCall("GetFocus", "Ptr"))
  309.     CP_RedrawAll()
  310. return
  311.  
  312. ; -------------------------- Clipboard and random-color actions ---------------
  313.  
  314. CP_CopyHex:
  315.    Clipboard := "#" . cp_hex
  316.     GuiControl,, CP_Status, % "Copied #" . cp_hex . " to the clipboard."
  317. return
  318.  
  319. CP_CopyRGB:
  320.    Clipboard := cp_r . ", " . cp_g . ", " . cp_b
  321.     GuiControl,, CP_Status, Copied RGB values to the clipboard.
  322. return
  323.  
  324. CP_CopyCSS:
  325.    Clipboard := "rgb(" . cp_r . ", " . cp_g . ", " . cp_b . ")"
  326.     GuiControl,, CP_Status, Copied CSS rgb() to the clipboard.
  327. return
  328.  
  329. CP_Random:
  330.    Random, cp_hue, 0, 359
  331.     Random, cp_sat, 55, 100
  332.     Random, cp_val, 65, 100
  333.     CP_HSVToRGB(cp_hue, cp_sat, cp_val, cp_r, cp_g, cp_b)
  334.     CP_UpdateFields()
  335.     CP_RedrawAll()
  336.     GuiControl,, CP_Status, Random color generated.
  337. return
  338.  
  339. ; -------------------------- Screen eyedropper --------------------------------
  340.  
  341. CP_ScreenPick:
  342.    if (cp_screen_pick_active)
  343.         return
  344.     CP_StartScreenPick()
  345. return
  346.  
  347. CP_StartScreenPick() {
  348.     global cp_screen_pick_active, cp_screen_pick_cursor_saved
  349.  
  350.     cp_screen_pick_active := true
  351.  
  352.     ; Hide the main window so it never sits between the cursor and the target pixel.
  353.     Gui, Hide
  354.  
  355.     ; Crosshair cursor via a temporary system-wide override, restored with SPI_SETCURSORS.
  356.     cp_screen_pick_cursor_saved := CP_SetCrosshairCursor()
  357.  
  358.     ; Consume LButton/RButton/Escape so the picker click does not reach the
  359.     ; window underneath and so cancelling is reliable.
  360.     try {
  361.         Hotkey, *LButton, CP_ScreenPickConfirm, On UseErrorLevel
  362.         Hotkey, *RButton, CP_ScreenPickCancel, On UseErrorLevel
  363.         Hotkey, *Escape,  CP_ScreenPickCancel, On UseErrorLevel
  364.     }
  365.  
  366.     ; Seed the overlay at the current position and start live tracking.
  367.     MouseGetPos, mx, my
  368.     CP_ScreenPickShowOverlay(mx, my)
  369.     SetTimer, CP_ScreenPickTrack, 30
  370. }
  371.  
  372. CP_ScreenPickShowOverlay(mx, my) {
  373.     PixelGetColor, pickColor, mx, my, RGB
  374.     if (ErrorLevel)
  375.         return
  376.  
  377.     CP_ComputePickPanelPosition(mx, my, px, py)
  378.     CP_PickPanelShow(px, py, pickColor, mx, my)
  379. }
  380.  
  381. CP_ScreenPickTrack:
  382.    if (!cp_screen_pick_active) {
  383.         SetTimer, CP_ScreenPickTrack, Off
  384.         return
  385.     }
  386.     MouseGetPos, mx, my
  387.     CP_ScreenPickShowOverlay(mx, my)
  388. return
  389.  
  390. CP_ScreenPickConfirm:
  391.    if (!cp_screen_pick_active)
  392.         return
  393.  
  394.     MouseGetPos, mx, my
  395.     PixelGetColor, pickColor, mx, my, RGB
  396.     pickErr := ErrorLevel
  397.  
  398.     CP_EndScreenPick()
  399.     Gui, Show
  400.  
  401.     if (!pickErr)
  402.         CP_ApplyPickedColor(pickColor)
  403. return
  404.  
  405. CP_ScreenPickCancel:
  406.    if (!cp_screen_pick_active)
  407.         return
  408.     CP_EndScreenPick()
  409.     Gui, Show
  410. return
  411.  
  412. CP_ApplyPickedColor(pickColor) {
  413.     global cp_r, cp_g, cp_b, cp_hue, cp_sat, cp_val
  414.  
  415.     cp_r := (pickColor >> 16) & 0xFF
  416.     cp_g := (pickColor >> 8) & 0xFF
  417.     cp_b := pickColor & 0xFF
  418.  
  419.     CP_RGBToHSV(cp_r, cp_g, cp_b, cp_hue, cp_sat, cp_val)
  420.     CP_UpdateFields()
  421.     CP_RedrawAll()
  422.  
  423.     GuiControl,, CP_Status, % "Picked #" . Format("{:06X}", pickColor) . " from screen."
  424. }
  425.  
  426. CP_SetCrosshairCursor() {
  427.     ; Cover every standard system cursor ID. Explorer, Word, browsers, and
  428.     ; many other apps call SetCursor(LoadCursor(NULL, IDC_HAND/IBEAM/WAIT/...))
  429.     ; directly in their WM_SETCURSOR handler, which bypasses SetSystemCursor
  430.     ; for that specific ID. Replacing only IDC_ARROW therefore leaves the hand
  431.     ; and I-beam fully functional while the pick is running.
  432.     ;
  433.     ; The ID list mirrors screenshot_manager.ahk's set_system_cursor() so the
  434.     ; two subsystems behave identically. PIN and PERSON are appended because
  435.     ; Explorer uses them on Quick Access and People surfaces on newer Windows.
  436.     static cursorIds := [32512, 32513, 32514, 32515, 32516
  437.                        , 32640, 32641, 32642, 32643, 32644
  438.                        , 32645, 32646, 32648, 32649, 32650
  439.                        , 32651, 32671, 32672]
  440.  
  441.     hCursor := DllCall("LoadCursor", "Ptr", 0, "Int", 32515, "Ptr")
  442.     if (!hCursor)
  443.         return false
  444.  
  445.     ; anyApplied, not allApplied: IDC_PIN and IDC_PERSON only exist on
  446.     ; Windows 8+. If at least one cursor was replaced, restore is still
  447.     ; required on exit. A single failure must not prevent the restore.
  448.     anyApplied := false
  449.     for _, id in cursorIds {
  450.         ; CopyImage is required: SetSystemCursor takes ownership of hcur and
  451.         ; destroys it, so each ID needs its own fresh handle.
  452.         hCopy := DllCall("CopyImage", "Ptr", hCursor, "UInt", 2, "Int", 0, "Int", 0, "UInt", 0, "Ptr")
  453.         if (!hCopy)
  454.             continue
  455.  
  456.         if (DllCall("SetSystemCursor", "Ptr", hCopy, "Int", id))
  457.             anyApplied := true
  458.         else
  459.             DllCall("DestroyCursor", "Ptr", hCopy)
  460.     }
  461.  
  462.     return anyApplied
  463. }
  464.  
  465. CP_EndScreenPick() {
  466.     global cp_screen_pick_active, cp_screen_pick_cursor_saved
  467.  
  468.     cp_screen_pick_active := false
  469.     SetTimer, CP_ScreenPickTrack, Off
  470.  
  471.     try {
  472.         Hotkey, *LButton, Off
  473.         Hotkey, *RButton, Off
  474.         Hotkey, *Escape,  Off
  475.     }
  476.  
  477.     CP_PickPanelHide()
  478.     CP_RestoreSystemCursors()
  479. }
  480.  
  481. CP_RestoreSystemCursors() {
  482.     global cp_screen_pick_cursor_saved
  483.  
  484.     if (!cp_screen_pick_cursor_saved)
  485.         return
  486.  
  487.     ; SPI_SETCURSORS (0x0057) reloads the cursor table from the current user
  488.     ; scheme. SPIF_SENDCHANGE (0x02) broadcasts WM_SETTINGCHANGE so apps that
  489.     ; cached a cursor handle re-query it.
  490.     DllCall("SystemParametersInfo", "UInt", 0x0057, "UInt", 0, "Ptr", 0, "UInt", 0x02)
  491.  
  492.     ; Force the window under the cursor to re-run WM_SETCURSOR now. Without
  493.     ; this, Explorer and other apps keep the old cursor until the next mouse
  494.     ; move triggers WM_SETCURSOR naturally.
  495.     ;
  496.     ;   wParam = HWND under cursor
  497.     ;   lParam = MAKELPARAM(HTCLIENT, WM_MOUSEMOVE) = 0x02000001
  498.     ;
  499.     ; SendMessageTimeout with SMTO_ABORTIFHUNG (0x0002) prevents a deadlock
  500.     ; if the target thread is stuck.
  501.     MouseGetPos, , , hwndUnder
  502.     if (hwndUnder) {
  503.         DllCall("SendMessageTimeout"
  504.               , "Ptr", hwndUnder
  505.               , "UInt", 0x0020           ; WM_SETCURSOR
  506.               , "Ptr", hwndUnder
  507.               , "Ptr", 0x02000001
  508.               , "UInt", 0x0002           ; SMTO_ABORTIFHUNG
  509.               , "UInt", 100              ; 100 ms
  510.               , "Ptr*", result
  511.               , "Ptr")
  512.     }
  513.  
  514.     ; If our own GUI is under the cursor after this call returns, our thread
  515.     ; owns the visible cursor; set it to the restored arrow explicitly.
  516.     hArrow := DllCall("LoadCursor", "Ptr", 0, "Int", 32512, "Ptr")
  517.     if (hArrow)
  518.         DllCall("SetCursor", "Ptr", hArrow)
  519.  
  520.     cp_screen_pick_cursor_saved := false
  521. }
  522.  
  523. ; -------------------------- Screen pick overlay ------------------------------
  524.  
  525. ; Layered per-pixel-alpha panel that follows the cursor during a screen pick.
  526. ; Shows the sampled swatch, hex, RGB, and cursor position in one card so the
  527. ; user can see and read the color without looking away from the target.
  528.  
  529. CP_PickPanelEnsure() {
  530.     global
  531.     if (cp_pp_hwnd && cp_pp_graphics)
  532.         return true
  533.  
  534.     cp_pp_dib := CP_CreateDIB(cp_pp_w, cp_pp_h, cp_pp_bits, 32)
  535.     if (!cp_pp_dib)
  536.         return false
  537.  
  538.     cp_pp_dc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  539.     if (!cp_pp_dc) {
  540.         DllCall("DeleteObject", "Ptr", cp_pp_dib)
  541.         cp_pp_dib := 0
  542.         return false
  543.     }
  544.  
  545.     cp_pp_obm := DllCall("SelectObject", "Ptr", cp_pp_dc, "Ptr", cp_pp_dib, "Ptr")
  546.     cp_pp_graphics := CP_GdipCreateGraphics(cp_pp_dc)
  547.     if (!cp_pp_graphics) {
  548.         CP_PickPanelReleaseGdi()
  549.         return false
  550.     }
  551.  
  552.     ; WS_EX_LAYERED | WS_EX_TRANSPARENT. Transparent makes the panel
  553.     ; click-through, so the sampled pixel is never hidden behind the panel.
  554.     Gui, cp_pp:New, -Caption +ToolWindow +AlwaysOnTop +E0x00080020 +Hwndcp_pp_hwnd
  555.     Gui, cp_pp:Color, 000000
  556.  
  557.     return true
  558. }
  559.  
  560. CP_PickPanelReleaseGdi() {
  561.     global
  562.     if (cp_pp_graphics) {
  563.         DllCall("gdiplus\GdipDeleteGraphics", "Ptr", cp_pp_graphics, "UInt")
  564.         cp_pp_graphics := 0
  565.     }
  566.     if (cp_pp_dc) {
  567.         if (cp_pp_obm) {
  568.             DllCall("SelectObject", "Ptr", cp_pp_dc, "Ptr", cp_pp_obm, "Ptr")
  569.             cp_pp_obm := 0
  570.         }
  571.         DllCall("DeleteDC", "Ptr", cp_pp_dc)
  572.         cp_pp_dc := 0
  573.     }
  574.     if (cp_pp_dib) {
  575.         DllCall("DeleteObject", "Ptr", cp_pp_dib)
  576.         cp_pp_dib := 0
  577.     }
  578. }
  579.  
  580. CP_ComputePickPanelPosition(mx, my, ByRef outX, ByRef outY) {
  581.     global cp_pp_w, cp_pp_h
  582.  
  583.     ; Virtual desktop bounds cover every monitor, not only the primary one.
  584.     vx := DllCall("GetSystemMetrics", "Int", 76)   ; SM_XVIRTUALSCREEN
  585.     vy := DllCall("GetSystemMetrics", "Int", 77)   ; SM_YVIRTUALSCREEN
  586.     vw := DllCall("GetSystemMetrics", "Int", 78)   ; SM_CXVIRTUALSCREEN
  587.     vh := DllCall("GetSystemMetrics", "Int", 79)   ; SM_CYVIRTUALSCREEN
  588.     vRight  := vx + vw
  589.     vBottom := vy + vh
  590.  
  591.     offset := 24
  592.     outX := mx + offset
  593.     outY := my + offset
  594.  
  595.     ; Flip to the opposite side before clamping so the panel does not sit on
  596.     ; top of the cursor when the screen edge is close.
  597.     if (outX + cp_pp_w > vRight)
  598.         outX := mx - offset - cp_pp_w
  599.     if (outY + cp_pp_h > vBottom)
  600.         outY := my - offset - cp_pp_h
  601.  
  602.     ; Clamp for the case where the panel is larger than the remaining space.
  603.     if (outX < vx)
  604.         outX := vx
  605.     if (outY < vy)
  606.         outY := vy
  607.     if (outX + cp_pp_w > vRight)
  608.         outX := vRight - cp_pp_w
  609.     if (outY + cp_pp_h > vBottom)
  610.         outY := vBottom - cp_pp_h
  611. }
  612.  
  613. CP_PickPanelShow(x, y, pickColor, mx, my) {
  614.     global
  615.     if (!CP_PickPanelEnsure())
  616.         return false
  617.  
  618.     if (!CP_PickPanelRender(pickColor, mx, my))
  619.         return false
  620.  
  621.     if (!CP_PickPanelUpdateLayeredWindow(x, y))
  622.         return false
  623.  
  624.     if (!cp_pp_visible) {
  625.         DllCall("ShowWindow", "Ptr", cp_pp_hwnd, "Int", 8)  ; SW_SHOWNA
  626.         cp_pp_visible := true
  627.     }
  628.     return true
  629. }
  630.  
  631. CP_PickPanelHide() {
  632.     global
  633.     if (cp_pp_visible && cp_pp_hwnd) {
  634.         DllCall("ShowWindow", "Ptr", cp_pp_hwnd, "Int", 0)  ; SW_HIDE
  635.         cp_pp_visible := false
  636.     }
  637. }
  638.  
  639. CP_PickPanelUpdateLayeredWindow(x, y) {
  640.     global cp_pp_hwnd, cp_pp_dc, cp_pp_w, cp_pp_h
  641.  
  642.     VarSetCapacity(ptDst, 8, 0)
  643.     NumPut(x, ptDst, 0, "Int")
  644.     NumPut(y, ptDst, 4, "Int")
  645.  
  646.     VarSetCapacity(sz, 8, 0)
  647.     NumPut(cp_pp_w, sz, 0, "Int")
  648.     NumPut(cp_pp_h, sz, 4, "Int")
  649.  
  650.     VarSetCapacity(ptSrc, 8, 0)  ; source origin is 0,0
  651.  
  652.     ; BLENDFUNCTION packed as a DWORD:
  653.     ;   BlendOp = AC_SRC_OVER (0), BlendFlags = 0
  654.     ;   SourceConstantAlpha = 255, AlphaFormat = AC_SRC_ALPHA (1)
  655.     blend := 0x01FF0000
  656.  
  657.     return DllCall("UpdateLayeredWindow"
  658.         , "Ptr", cp_pp_hwnd
  659.         , "Ptr", 0
  660.         , "Ptr", &ptDst
  661.         , "Ptr", &sz
  662.         , "Ptr", cp_pp_dc
  663.         , "Ptr", &ptSrc
  664.         , "UInt", 0
  665.         , "UInt*", blend
  666.         , "UInt", 2)  ; ULW_ALPHA
  667. }
  668.  
  669. CP_PickPanelRender(pickColor, mx, my) {
  670.     global cp_pp_graphics, cp_pp_w, cp_pp_h
  671.  
  672.     g := cp_pp_graphics
  673.     if (!g)
  674.         return false
  675.  
  676.     ; Fully transparent base.
  677.     DllCall("gdiplus\GdipGraphicsClear", "Ptr", g, "UInt", 0x00000000)
  678.  
  679.     ; Panel background: soft translucent dark card, 94% opaque.
  680.     CP_GdipFillRectArgb(g, 0xF01A1D22, 0, 0, cp_pp_w, cp_pp_h)
  681.  
  682.     ; 1px border.
  683.     CP_GdipDrawRectArgb(g, 0xFF3E4550, 1.0, 0.5, 0.5, cp_pp_w - 1, cp_pp_h - 1)
  684.  
  685.     ; Swatch area: 68x68 at (10, 13), with its own 1px border.
  686.     swatchX := 10
  687.     swatchY := 13
  688.     swatchW := 68
  689.     swatchH := 68
  690.  
  691.     r := (pickColor >> 16) & 0xFF
  692.     gg := (pickColor >> 8) & 0xFF
  693.     b := pickColor & 0xFF
  694.     swatchArgb := 0xFF000000 | (r << 16) | (gg << 8) | b
  695.  
  696.     CP_GdipFillRectArgb(g, swatchArgb, swatchX, swatchY, swatchW, swatchH)
  697.     CP_GdipDrawRectArgb(g, 0xFF5A6270, 1.0, swatchX - 0.5, swatchY - 0.5, swatchW + 1, swatchH + 1)
  698.  
  699.     ; Text column starts right of the swatch.
  700.     textX := swatchX + swatchW + 12
  701.     textW := cp_pp_w - textX - 12
  702.  
  703.     hexStr := Format("#{:06X}", pickColor)
  704.     rgbStr := "rgb(" r ", " gg ", " b ")"
  705.     posStr := "x " mx "   y " my
  706.  
  707.     ; Hex: large, bright, primary readout.
  708.     CP_GdipDrawText(g, hexStr, textX, 12, textW, 22, 0xFFFFFFFF, 15, "Segoe UI", true)
  709.     ; RGB: mid-emphasis.
  710.     CP_GdipDrawText(g, rgbStr, textX, 36, textW, 16, 0xFFD0D6E0, 10, "Segoe UI", false)
  711.     ; Cursor position: minor.
  712.     CP_GdipDrawText(g, posStr, textX, 54, textW, 14, 0xFF9098A8, 9,  "Segoe UI", false)
  713.     ; Hint: dimmest.
  714.     CP_GdipDrawText(g, "Click to pick  ·  Right-click or Esc to cancel", textX, 72, textW, 14, 0xFF7A8290, 8, "Segoe UI", false)
  715.  
  716.     DllCall("gdiplus\GdipFlush", "Ptr", g, "Int", 1)
  717.     DllCall("GdiFlush")
  718.  
  719.     return true
  720. }
  721.  
  722. CP_PickPanelCleanup() {
  723.     global cp_pp_hwnd, cp_pp_visible, cp_pp_font_families
  724.  
  725.     CP_PickPanelHide()
  726.     CP_PickPanelReleaseGdi()
  727.  
  728.     if (cp_pp_hwnd) {
  729.         Gui, cp_pp:Destroy
  730.         cp_pp_hwnd := 0
  731.     }
  732.  
  733.     ; Free any font families cached for text rendering.
  734.     for _, pFamily in cp_pp_font_families {
  735.         if (pFamily)
  736.             DllCall("gdiplus\GdipDeleteFontFamily", "Ptr", pFamily, "UInt")
  737.     }
  738.     cp_pp_font_families := {}
  739. }
  740.  
  741. ; -------------------------- GDI+ primitives for the pick overlay -------------
  742.  
  743. CP_GdipFillRectArgb(g, argb, x, y, w, h) {
  744.     brush := 0
  745.     DllCall("gdiplus\GdipCreateSolidFill", "UInt", argb, "Ptr*", brush, "UInt")
  746.     if (!brush)
  747.         return false
  748.     DllCall("gdiplus\GdipFillRectangle", "Ptr", g, "Ptr", brush, "Float", x, "Float", y, "Float", w, "Float", h, "UInt")
  749.     DllCall("gdiplus\GdipDeleteBrush", "Ptr", brush, "UInt")
  750.     return true
  751. }
  752.  
  753. CP_GdipDrawRectArgb(g, argb, thickness, x, y, w, h) {
  754.     pen := 0
  755.     DllCall("gdiplus\GdipCreatePen1", "UInt", argb, "Float", thickness, "Int", 2, "Ptr*", pen, "UInt")
  756.     if (!pen)
  757.         return false
  758.     DllCall("gdiplus\GdipDrawRectangle", "Ptr", g, "Ptr", pen, "Float", x, "Float", y, "Float", w, "Float", h, "UInt")
  759.     DllCall("gdiplus\GdipDeletePen", "Ptr", pen, "UInt")
  760.     return true
  761. }
  762.  
  763. CP_GdipDrawText(g, text, x, y, w, h, argb, size, fontName, bold := false) {
  764.     global cp_pp_font_families
  765.  
  766.     if (!cp_pp_font_families.HasKey(fontName)) {
  767.         pFamily := 0
  768.         DllCall("gdiplus\GdipCreateFontFamilyFromName", "WStr", fontName, "UInt", 0, "Ptr*", pFamily, "UInt")
  769.         cp_pp_font_families[fontName] := pFamily
  770.     }
  771.     hFontFamily := cp_pp_font_families[fontName]
  772.     if (!hFontFamily)
  773.         return false
  774.  
  775.     ; AntiAlias (4) is the safe choice for layered per-pixel-alpha windows;
  776.     ; ClearType subpixel rendering does not composite correctly through ULW.
  777.     DllCall("gdiplus\GdipSetTextRenderingHint", "Ptr", g, "Int", 4, "UInt")
  778.  
  779.     style := bold ? 1 : 0
  780.     pFont := 0
  781.     DllCall("gdiplus\GdipCreateFont", "Ptr", hFontFamily, "Float", size, "Int", style, "Int", 2, "Ptr*", pFont, "UInt")
  782.     if (!pFont)
  783.         return false
  784.  
  785.     pStringFormat := 0
  786.     DllCall("gdiplus\GdipCreateStringFormat", "Int", 0, "Int", 0, "Ptr*", pStringFormat, "UInt")
  787.     if (pStringFormat) {
  788.         DllCall("gdiplus\GdipSetStringFormatAlign", "Ptr", pStringFormat, "Int", 0)
  789.         DllCall("gdiplus\GdipSetStringFormatLineAlign", "Ptr", pStringFormat, "Int", 0)
  790.         DllCall("gdiplus\GdipSetStringFormatTrimming", "Ptr", pStringFormat, "Int", 1)
  791.     }
  792.  
  793.     pBrush := 0
  794.     DllCall("gdiplus\GdipCreateSolidFill", "UInt", argb, "Ptr*", pBrush, "UInt")
  795.  
  796.     VarSetCapacity(rectF, 16, 0)
  797.     NumPut(x, rectF, 0, "Float")
  798.     NumPut(y, rectF, 4, "Float")
  799.     NumPut(w, rectF, 8, "Float")
  800.     NumPut(h, rectF, 12, "Float")
  801.  
  802.     DllCall("gdiplus\GdipDrawString", "Ptr", g, "WStr", text, "Int", -1, "Ptr", pFont, "Ptr", &rectF, "Ptr", pStringFormat, "Ptr", pBrush, "UInt")
  803.  
  804.     if (pBrush) DllCall("gdiplus\GdipDeleteBrush", "Ptr", pBrush, "UInt")
  805.     if (pStringFormat) DllCall("gdiplus\GdipDeleteStringFormat", "Ptr", pStringFormat, "UInt")
  806.     if (pFont) DllCall("gdiplus\GdipDeleteFont", "Ptr", pFont, "UInt")
  807.     return true
  808. }
  809.  
  810. ; -------------------------- Eyedropper button icon ---------------------------
  811.  
  812. CP_CreateEyedropperBitmap(width, height) {
  813.     VarSetCapacity(bi, 40, 0)
  814.     NumPut(40, bi, 0, "UInt")
  815.     NumPut(width, bi, 4, "Int")
  816.     NumPut(-height, bi, 8, "Int")
  817.     NumPut(1, bi, 12, "UShort")
  818.     NumPut(32, bi, 14, "UShort")
  819.     NumPut(0, bi, 16, "UInt")
  820.  
  821.     hdcScreen := DllCall("GetDC", "Ptr", 0, "Ptr")
  822.     hdc := DllCall("CreateCompatibleDC", "Ptr", hdcScreen, "Ptr")
  823.     DllCall("ReleaseDC", "Ptr", 0, "Ptr", hdcScreen)
  824.     if (!hdc)
  825.         return 0
  826.  
  827.     bits := 0
  828.     hbm := DllCall("CreateDIBSection", "Ptr", hdc, "Ptr", &bi, "UInt", 0, "Ptr*", bits, "Ptr", 0, "UInt", 0, "Ptr")
  829.     if (!hbm) {
  830.         DllCall("DeleteDC", "Ptr", hdc)
  831.         return 0
  832.     }
  833.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", hbm, "Ptr")
  834.  
  835.     ; The button face color is baked into the icon bitmap so the Picture control
  836.     ; blends seamlessly with the adjacent Text label.
  837.     bgArgb := 0xFF3A414C
  838.     Loop, % width * height {
  839.         NumPut(bgArgb, bits + ((A_Index - 1) * 4), 0, "UInt")
  840.     }
  841.  
  842.     ; Draw the eyedropper centered inside a 24x24 icon area.
  843.     iconSize := 24
  844.     offsetX := (width  - iconSize) // 2
  845.     offsetY := (height - iconSize) // 2
  846.  
  847.     graphics := CP_GdipCreateGraphics(hdc)
  848.     if (graphics) {
  849.         ; Bulb: filled circle with a light outline at the top-right.
  850.         bulbBrush := 0, bulbPen := 0
  851.         DllCall("gdiplus\GdipCreateSolidFill", "UInt", 0xFFEAF0FA, "Ptr*", bulbBrush, "UInt")
  852.         DllCall("gdiplus\GdipCreatePen1", "UInt", 0xFF8A95A5, "Float", 1.0, "Int", 2, "Ptr*", bulbPen, "UInt")
  853.  
  854.         bulbCx := offsetX + 16
  855.         bulbCy := offsetY + 6
  856.         bulbR  := 4.5
  857.         if (bulbBrush)
  858.             DllCall("gdiplus\GdipFillEllipse", "Ptr", graphics, "Ptr", bulbBrush, "Float", bulbCx - bulbR, "Float", bulbCy - bulbR, "Float", bulbR * 2, "Float", bulbR * 2, "UInt")
  859.         if (bulbPen)
  860.             DllCall("gdiplus\GdipDrawEllipse", "Ptr", graphics, "Ptr", bulbPen, "Float", bulbCx - bulbR, "Float", bulbCy - bulbR, "Float", bulbR * 2, "Float", bulbR * 2, "UInt")
  861.  
  862.         if (bulbBrush) DllCall("gdiplus\GdipDeleteBrush", "Ptr", bulbBrush, "UInt")
  863.         if (bulbPen)   DllCall("gdiplus\GdipDeletePen",   "Ptr", bulbPen,   "UInt")
  864.  
  865.         ; Tube: slanted parallelogram from the bulb down to the tip area.
  866.         tubeBrush := 0
  867.         DllCall("gdiplus\GdipCreateSolidFill", "UInt", 0xFFD2DAE6, "Ptr*", tubeBrush, "UInt")
  868.         if (tubeBrush) {
  869.             VarSetCapacity(tubePts, 32, 0)
  870.             NumPut(offsetX + 13.5, tubePts, 0,  "Float")
  871.             NumPut(offsetY +  8.0, tubePts, 4,  "Float")
  872.             NumPut(offsetX + 15.5, tubePts, 8,  "Float")
  873.             NumPut(offsetY + 10.0, tubePts, 12, "Float")
  874.             NumPut(offsetX +  7.0, tubePts, 16, "Float")
  875.             NumPut(offsetY + 18.0, tubePts, 20, "Float")
  876.             NumPut(offsetX +  5.0, tubePts, 24, "Float")
  877.             NumPut(offsetY + 16.0, tubePts, 28, "Float")
  878.             DllCall("gdiplus\GdipFillPolygon", "Ptr", graphics, "Ptr", tubeBrush, "Ptr", &tubePts, "Int", 4, "Int", 0, "UInt")
  879.             DllCall("gdiplus\GdipDeleteBrush", "Ptr", tubeBrush, "UInt")
  880.         }
  881.  
  882.         ; Tip: triangle at the bottom-left that connects to the tube's endpoint.
  883.         tipBrush := 0
  884.         DllCall("gdiplus\GdipCreateSolidFill", "UInt", 0xFFA0AEC0, "Ptr*", tipBrush, "UInt")
  885.         if (tipBrush) {
  886.             VarSetCapacity(tipPts, 24, 0)
  887.             NumPut(offsetX + 3.0, tipPts, 0,  "Float")
  888.             NumPut(offsetY + 21.0, tipPts, 4, "Float")
  889.             NumPut(offsetX + 7.0, tipPts, 8,  "Float")
  890.             NumPut(offsetY + 18.0, tipPts, 12, "Float")
  891.             NumPut(offsetX + 5.0, tipPts, 16, "Float")
  892.             NumPut(offsetY + 16.0, tipPts, 20, "Float")
  893.             DllCall("gdiplus\GdipFillPolygon", "Ptr", graphics, "Ptr", tipBrush, "Ptr", &tipPts, "Int", 3, "Int", 0, "UInt")
  894.             DllCall("gdiplus\GdipDeleteBrush", "Ptr", tipBrush, "UInt")
  895.         }
  896.  
  897.         DllCall("gdiplus\GdipDeleteGraphics", "Ptr", graphics, "UInt")
  898.     }
  899.  
  900.     ; GDI+ drawing does not touch the alpha byte; force every pixel opaque so
  901.     ; the Picture control renders the button face correctly.
  902.     Loop, % width * height {
  903.         NumPut(0xFF, bits + ((A_Index - 1) * 4) + 3, 0, "UChar")
  904.     }
  905.  
  906.     DllCall("GdiFlush")
  907.  
  908.     if (oldBitmap)
  909.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  910.     DllCall("DeleteDC", "Ptr", hdc)
  911.  
  912.     return hbm
  913. }
  914.  
  915. ; -------------------------- Mouse interaction --------------------------------
  916.  
  917. CP_WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {
  918.     global cp_hwnd_sv, cp_hwnd_hue, cp_drag_mode, cp_drag_hwnd
  919.  
  920.     if (cp_drag_mode != "")
  921.         CP_EndDrag()
  922.  
  923.     VarSetCapacity(point, 8, 0)
  924.     if (!DllCall("GetCursorPos", "Ptr", &point))
  925.         return
  926.  
  927.     mouseX := NumGet(point, 0, "Int")
  928.     mouseY := NumGet(point, 4, "Int")
  929.  
  930.     if (CP_PointInsideWindow(cp_hwnd_sv, mouseX, mouseY)) {
  931.         cp_drag_mode := "sv"
  932.         cp_drag_hwnd := cp_hwnd_sv
  933.     } else if (CP_PointInsideWindow(cp_hwnd_hue, mouseX, mouseY)) {
  934.         cp_drag_mode := "hue"
  935.         cp_drag_hwnd := cp_hwnd_hue
  936.     } else {
  937.         return
  938.     }
  939.  
  940.     DllCall("SetCapture", "Ptr", cp_drag_hwnd, "Ptr")
  941.     CP_ApplyScreenPoint(cp_drag_mode, mouseX, mouseY)
  942.     return 0
  943. }
  944.  
  945. CP_WM_MOUSEMOVE(wParam, lParam, msg, hwnd) {
  946.     global cp_drag_mode
  947.  
  948.     if (cp_drag_mode = "")
  949.         return
  950.  
  951.     if (!GetKeyState("LButton", "P")) {
  952.         CP_EndDrag()
  953.         return
  954.     }
  955.  
  956.     VarSetCapacity(point, 8, 0)
  957.     if (DllCall("GetCursorPos", "Ptr", &point)) {
  958.         mouseX := NumGet(point, 0, "Int")
  959.         mouseY := NumGet(point, 4, "Int")
  960.         CP_QueueScreenPoint(cp_drag_mode, mouseX, mouseY)
  961.     }
  962. }
  963.  
  964. CP_WM_LBUTTONUP(wParam, lParam, msg, hwnd) {
  965.     global cp_drag_mode, cp_drag_timer_pending
  966.     global cp_pending_mouse_x, cp_pending_mouse_y
  967.  
  968.     if (cp_drag_mode = "")
  969.         return
  970.  
  971.     SetTimer, CP_DragUpdate, Off
  972.     cp_drag_timer_pending := false
  973.  
  974.     VarSetCapacity(point, 8, 0)
  975.     if (DllCall("GetCursorPos", "Ptr", &point)) {
  976.         cp_pending_mouse_x := NumGet(point, 0, "Int")
  977.         cp_pending_mouse_y := NumGet(point, 4, "Int")
  978.         CP_ApplyScreenPoint(cp_drag_mode, cp_pending_mouse_x, cp_pending_mouse_y)
  979.     }
  980.  
  981.     CP_EndDrag()
  982.     return 0
  983. }
  984.  
  985. CP_WM_CAPTURECHANGED(wParam, lParam, msg, hwnd) {
  986.     global cp_drag_hwnd, cp_drag_mode, cp_drag_timer_pending
  987.  
  988.     if (hwnd = cp_drag_hwnd && cp_drag_mode != "") {
  989.         cp_drag_mode := ""
  990.         cp_drag_hwnd := 0
  991.         cp_drag_timer_pending := false
  992.         SetTimer, CP_DragUpdate, Off
  993.     }
  994. }
  995.  
  996. CP_EndDrag() {
  997.     global cp_drag_mode, cp_drag_hwnd, cp_drag_timer_pending
  998.  
  999.     SetTimer, CP_DragUpdate, Off
  1000.     cp_drag_timer_pending := false
  1001.     captureHwnd := cp_drag_hwnd
  1002.     cp_drag_mode := ""
  1003.     cp_drag_hwnd := 0
  1004.  
  1005.     if (captureHwnd && DllCall("GetCapture", "Ptr") = captureHwnd)
  1006.         DllCall("ReleaseCapture")
  1007. }
  1008.  
  1009. CP_QueueScreenPoint(mode, screenX, screenY) {
  1010.     global cp_pending_mouse_x, cp_pending_mouse_y, cp_drag_timer_pending
  1011.  
  1012.     cp_pending_mouse_x := screenX
  1013.     cp_pending_mouse_y := screenY
  1014.  
  1015.     if (!cp_drag_timer_pending) {
  1016.         cp_drag_timer_pending := true
  1017.         SetTimer, CP_DragUpdate, -16
  1018.     }
  1019. }
  1020.  
  1021. CP_DragUpdate:
  1022.    cp_drag_timer_pending := false
  1023.     if (cp_drag_mode != "")
  1024.         CP_ApplyScreenPoint(cp_drag_mode, cp_pending_mouse_x, cp_pending_mouse_y)
  1025. return
  1026.  
  1027. CP_PointInsideWindow(hwnd, screenX, screenY) {
  1028.     if (!hwnd || !DllCall("IsWindow", "Ptr", hwnd))
  1029.         return false
  1030.  
  1031.     VarSetCapacity(rect, 16, 0)
  1032.     if (!DllCall("GetWindowRect", "Ptr", hwnd, "Ptr", &rect))
  1033.         return false
  1034.  
  1035.     left := NumGet(rect, 0, "Int")
  1036.     top := NumGet(rect, 4, "Int")
  1037.     right := NumGet(rect, 8, "Int")
  1038.     bottom := NumGet(rect, 12, "Int")
  1039.  
  1040.     return (screenX >= left && screenX < right && screenY >= top && screenY < bottom)
  1041. }
  1042.  
  1043. CP_ApplyScreenPoint(mode, screenX, screenY) {
  1044.     global cp_hwnd_sv, cp_hwnd_hue
  1045.     global cp_sv_width, cp_sv_height, cp_hue_height
  1046.     global cp_hue, cp_sat, cp_val, cp_r, cp_g, cp_b
  1047.  
  1048.     controlHwnd := (mode = "sv") ? cp_hwnd_sv : cp_hwnd_hue
  1049.     if (!controlHwnd || !DllCall("IsWindow", "Ptr", controlHwnd))
  1050.         return
  1051.  
  1052.     VarSetCapacity(rect, 16, 0)
  1053.     if (!DllCall("GetWindowRect", "Ptr", controlHwnd, "Ptr", &rect))
  1054.         return
  1055.  
  1056.     left := NumGet(rect, 0, "Int")
  1057.     top := NumGet(rect, 4, "Int")
  1058.     right := NumGet(rect, 8, "Int")
  1059.     bottom := NumGet(rect, 12, "Int")
  1060.     controlWidth := right - left
  1061.     controlHeight := bottom - top
  1062.  
  1063.     if (controlWidth <= 1 || controlHeight <= 1)
  1064.         return
  1065.  
  1066.     sourceWidth := (mode = "sv") ? cp_sv_width : cp_hue_width
  1067.     sourceHeight := (mode = "sv") ? cp_sv_height : cp_hue_height
  1068.     screenX := CP_Clamp(screenX, left, right - 1)
  1069.     screenY := CP_Clamp(screenY, top, bottom - 1)
  1070.  
  1071.     x := Round((screenX - left) * (sourceWidth - 1) / (controlWidth - 1))
  1072.     y := Round((screenY - top) * (sourceHeight - 1) / (controlHeight - 1))
  1073.  
  1074.     if (mode = "sv") {
  1075.         x := CP_Clamp(x, 0, cp_sv_width - 1)
  1076.         y := CP_Clamp(y, 0, cp_sv_height - 1)
  1077.  
  1078.         newSat := Round((x / (cp_sv_width - 1)) * 100)
  1079.         newVal := Round((1 - (y / (cp_sv_height - 1))) * 100)
  1080.  
  1081.         if (newSat = cp_sat && newVal = cp_val)
  1082.             return
  1083.  
  1084.         cp_sat := newSat
  1085.         cp_val := newVal
  1086.     } else {
  1087.         y := CP_Clamp(y, 0, cp_hue_height - 1)
  1088.         newHue := Round((y / (cp_hue_height - 1)) * 360)
  1089.  
  1090.         if (newHue = cp_hue)
  1091.             return
  1092.  
  1093.         cp_hue := newHue
  1094.     }
  1095.  
  1096.     CP_HSVToRGB(cp_hue, cp_sat, cp_val, cp_r, cp_g, cp_b)
  1097.     CP_UpdateFields()
  1098.     CP_RedrawInteractive(mode)
  1099. }
  1100.  
  1101. CP_Clamp(value, minimum, maximum) {
  1102.     if (value < minimum)
  1103.         return minimum
  1104.     if (value > maximum)
  1105.         return maximum
  1106.     return value
  1107. }
  1108.  
  1109. ; -------------------------- DIB creation and refresh -------------------------
  1110.  
  1111. CP_CreateDIB(width, height, ByRef bits, bitCount := 32) {
  1112.     VarSetCapacity(bi, 40, 0)
  1113.     NumPut(40, bi, 0, "UInt")
  1114.     NumPut(width, bi, 4, "Int")
  1115.     NumPut(-height, bi, 8, "Int")
  1116.     NumPut(1, bi, 12, "UShort")
  1117.     NumPut(bitCount, bi, 14, "UShort")
  1118.     NumPut(0, bi, 16, "UInt")
  1119.  
  1120.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1121.     if (!hdc)
  1122.         return 0
  1123.  
  1124.     hbm := DllCall("CreateDIBSection", "Ptr", hdc, "Ptr", &bi, "UInt", 0, "Ptr*", bits, "Ptr", 0, "UInt", 0, "Ptr")
  1125.     DllCall("DeleteDC", "Ptr", hdc)
  1126.     return hbm
  1127. }
  1128.  
  1129. CP_RefreshPicture(hwnd) {
  1130.     if (!hwnd)
  1131.         return
  1132.     DllCall("InvalidateRect", "Ptr", hwnd, "Ptr", 0, "Int", 0)
  1133. }
  1134.  
  1135. CP_RedrawAll() {
  1136.     global cp_r, cp_g, cp_b, cp_hex
  1137.     global cp_hwnd_sv, cp_hwnd_hue, cp_hwnd_preview
  1138.  
  1139.     Critical, On
  1140.     cp_hex := Format("{:02X}{:02X}{:02X}", cp_r, cp_g, cp_b)
  1141.     CP_DrawSV()
  1142.     CP_DrawHue()
  1143.     CP_DrawPreview()
  1144.     DllCall("GdiFlush")
  1145.  
  1146.     CP_RefreshPicture(cp_hwnd_sv)
  1147.     CP_RefreshPicture(cp_hwnd_hue)
  1148.     CP_RefreshPicture(cp_hwnd_preview)
  1149.     Critical, Off
  1150.  
  1151.     GuiControl,, CP_HexReadout, % "#" . cp_hex
  1152. }
  1153.  
  1154. CP_RedrawInteractive(mode) {
  1155.     global cp_r, cp_g, cp_b, cp_hex
  1156.     global cp_hwnd_sv, cp_hwnd_hue, cp_hwnd_preview
  1157.  
  1158.     Critical, On
  1159.     cp_hex := Format("{:02X}{:02X}{:02X}", cp_r, cp_g, cp_b)
  1160.  
  1161.     CP_DrawSV()
  1162.     if (mode = "hue")
  1163.         CP_DrawHue()
  1164.     CP_DrawPreview()
  1165.     DllCall("GdiFlush")
  1166.  
  1167.     CP_RefreshPicture(cp_hwnd_sv)
  1168.     if (mode = "hue")
  1169.         CP_RefreshPicture(cp_hwnd_hue)
  1170.     CP_RefreshPicture(cp_hwnd_preview)
  1171.     Critical, Off
  1172.  
  1173.     GuiControl,, CP_HexReadout, % "#" . cp_hex
  1174. }
  1175.  
  1176. ; -------------------------- Native Win32 color-field rendering ---------------
  1177.  
  1178. CP_BuildBlackOverlay() {
  1179.     global cp_bits_overlay, cp_sv_height
  1180.  
  1181.     Loop, %cp_sv_height% {
  1182.         y := A_Index - 1
  1183.         alpha := Round((y / (cp_sv_height - 1)) * 255)
  1184.         pixel := alpha << 24
  1185.         NumPut(pixel, cp_bits_overlay + (y * 4), 0, "UInt")
  1186.     }
  1187.     return true
  1188. }
  1189.  
  1190. CP_BuildSVGradient(hue) {
  1191.     global cp_bitmap_sv_base, cp_sv_width, cp_sv_height
  1192.     global cp_bitmap_overlay, cp_bits_sv_base
  1193.  
  1194.     CP_HSVToRGB(hue, 100, 100, hueR, hueG, hueB)
  1195.  
  1196.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1197.     overlayDC := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1198.     if (!hdc || !overlayDC) {
  1199.         if (hdc)
  1200.             DllCall("DeleteDC", "Ptr", hdc)
  1201.         if (overlayDC)
  1202.             DllCall("DeleteDC", "Ptr", overlayDC)
  1203.         return false
  1204.     }
  1205.  
  1206.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", cp_bitmap_sv_base, "Ptr")
  1207.     oldOverlay := DllCall("SelectObject", "Ptr", overlayDC, "Ptr", cp_bitmap_overlay, "Ptr")
  1208.  
  1209.     VarSetCapacity(vertices, 32, 0)
  1210.     NumPut(0, vertices, 0, "Int")
  1211.     NumPut(0, vertices, 4, "Int")
  1212.     NumPut(65535, vertices, 8, "UShort")
  1213.     NumPut(65535, vertices, 10, "UShort")
  1214.     NumPut(65535, vertices, 12, "UShort")
  1215.     NumPut(0, vertices, 14, "UShort")
  1216.     NumPut(cp_sv_width, vertices, 16, "Int")
  1217.     NumPut(cp_sv_height, vertices, 20, "Int")
  1218.     NumPut(hueR * 257, vertices, 24, "UShort")
  1219.     NumPut(hueG * 257, vertices, 26, "UShort")
  1220.     NumPut(hueB * 257, vertices, 28, "UShort")
  1221.     NumPut(0, vertices, 30, "UShort")
  1222.  
  1223.     VarSetCapacity(mesh, 8, 0)
  1224.     NumPut(0, mesh, 0, "UInt")
  1225.     NumPut(1, mesh, 4, "UInt")
  1226.  
  1227.     gradientOK := DllCall("msimg32\GradientFill", "Ptr", hdc, "Ptr", &vertices, "UInt", 2, "Ptr", &mesh, "UInt", 1, "UInt", 0, "Int")
  1228.  
  1229.     blendOK := false
  1230.     if (gradientOK) {
  1231.         blendFunction := 0x01FF0000
  1232.         blendOK := DllCall("msimg32\AlphaBlend", "Ptr", hdc, "Int", 0, "Int", 0, "Int", cp_sv_width, "Int", cp_sv_height, "Ptr", overlayDC, "Int", 0, "Int", 0, "Int", 1, "Int", cp_sv_height, "UInt", blendFunction, "Int")
  1233.     }
  1234.  
  1235.     DllCall("GdiFlush")
  1236.  
  1237.     if (oldOverlay)
  1238.         DllCall("SelectObject", "Ptr", overlayDC, "Ptr", oldOverlay, "Ptr")
  1239.     if (oldBitmap)
  1240.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  1241.     DllCall("DeleteDC", "Ptr", overlayDC)
  1242.     DllCall("DeleteDC", "Ptr", hdc)
  1243.     return (gradientOK && blendOK)
  1244. }
  1245.  
  1246. CP_BuildSVGradientPixels(hue) {
  1247.     global cp_sv_width, cp_sv_height, cp_bits_sv_base
  1248.     CP_HSVToRGB(hue, 100, 100, hueR, hueG, hueB)
  1249.  
  1250.     Loop, %cp_sv_height% {
  1251.         y := A_Index - 1
  1252.         value := 1 - (y / (cp_sv_height - 1))
  1253.         rowOffset := y * cp_sv_width * 4
  1254.         Loop, %cp_sv_width% {
  1255.             x := A_Index - 1
  1256.             saturation := x / (cp_sv_width - 1)
  1257.             red := Round(value * (255 + (hueR - 255) * saturation))
  1258.             green := Round(value * (255 + (hueG - 255) * saturation))
  1259.             blue := Round(value * (255 + (hueB - 255) * saturation))
  1260.             pixelOffset := rowOffset + (x * 4)
  1261.             NumPut(blue, cp_bits_sv_base + pixelOffset, 0, "UChar")
  1262.             NumPut(green, cp_bits_sv_base + pixelOffset + 1, 0, "UChar")
  1263.             NumPut(red, cp_bits_sv_base + pixelOffset + 2, 0, "UChar")
  1264.             NumPut(255, cp_bits_sv_base + pixelOffset + 3, 0, "UChar")
  1265.         }
  1266.     }
  1267. }
  1268.  
  1269. CP_BuildHueGradient() {
  1270.     global cp_bitmap_hue_base, cp_hue_width, cp_hue_height, cp_hue_base_ready
  1271.  
  1272.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1273.     if (!hdc)
  1274.         return false
  1275.  
  1276.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", cp_bitmap_hue_base, "Ptr")
  1277.     success := true
  1278.  
  1279.     Loop, 6 {
  1280.         segment := A_Index - 1
  1281.         hue1 := segment * 60
  1282.         hue2 := (segment + 1) * 60
  1283.         CP_HSVToRGB(hue1, 100, 100, r1, g1, b1)
  1284.         CP_HSVToRGB(hue2, 100, 100, r2, g2, b2)
  1285.  
  1286.         y1 := Round(segment * cp_hue_height / 6)
  1287.         y2 := Round((segment + 1) * cp_hue_height / 6)
  1288.  
  1289.         VarSetCapacity(vertices, 32, 0)
  1290.         NumPut(0, vertices, 0, "Int")
  1291.         NumPut(y1, vertices, 4, "Int")
  1292.         NumPut(r1 * 257, vertices, 8, "UShort")
  1293.         NumPut(g1 * 257, vertices, 10, "UShort")
  1294.         NumPut(b1 * 257, vertices, 12, "UShort")
  1295.         NumPut(0, vertices, 14, "UShort")
  1296.         NumPut(cp_hue_width, vertices, 16, "Int")
  1297.         NumPut(y2, vertices, 20, "Int")
  1298.         NumPut(r2 * 257, vertices, 24, "UShort")
  1299.         NumPut(g2 * 257, vertices, 26, "UShort")
  1300.         NumPut(b2 * 257, vertices, 28, "UShort")
  1301.         NumPut(0, vertices, 30, "UShort")
  1302.  
  1303.         VarSetCapacity(mesh, 8, 0)
  1304.         NumPut(0, mesh, 0, "UInt")
  1305.         NumPut(1, mesh, 4, "UInt")
  1306.  
  1307.         result := DllCall("msimg32\GradientFill", "Ptr", hdc, "Ptr", &vertices, "UInt", 2, "Ptr", &mesh, "UInt", 1, "UInt", 1, "Int")
  1308.         if (!result)
  1309.             success := false
  1310.     }
  1311.  
  1312.     DllCall("GdiFlush")
  1313.  
  1314.     if (oldBitmap)
  1315.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  1316.     DllCall("DeleteDC", "Ptr", hdc)
  1317.  
  1318.     if (success)
  1319.         cp_hue_base_ready := true
  1320.     return success
  1321. }
  1322.  
  1323. CP_DrawSV() {
  1324.     global cp_hue, cp_sat, cp_val, cp_sv_width, cp_sv_height
  1325.     global cp_bitmap_sv, cp_bitmap_sv_base, cp_bits_sv, cp_bits_sv_base
  1326.     global cp_last_sv_hue
  1327.  
  1328.     if (cp_last_sv_hue != cp_hue) {
  1329.         if (!CP_BuildSVGradient(cp_hue))
  1330.             CP_BuildSVGradientPixels(cp_hue)
  1331.         cp_last_sv_hue := cp_hue
  1332.     }
  1333.  
  1334.     DllCall("RtlMoveMemory", "Ptr", cp_bits_sv, "Ptr", cp_bits_sv_base, "UPtr", cp_sv_width * cp_sv_height * 4)
  1335.  
  1336.     markX := Round((cp_sat / 100) * (cp_sv_width - 1))
  1337.     markY := Round((1 - (cp_val / 100)) * (cp_sv_height - 1))
  1338.     CP_DrawCircleMarker(markX, markY)
  1339. }
  1340.  
  1341. CP_DrawHue() {
  1342.     global cp_hue, cp_hue_width, cp_hue_height
  1343.     global cp_bitmap_hue, cp_bitmap_hue_base
  1344.     global cp_bits_hue, cp_bits_hue_base, cp_hue_base_ready
  1345.  
  1346.     if (!cp_hue_base_ready) {
  1347.         if (!CP_BuildHueGradient()) {
  1348.             Loop, %cp_hue_height% {
  1349.                 y := A_Index - 1
  1350.                 hueValue := (y / (cp_hue_height - 1)) * 360
  1351.                 if (hueValue >= 360)
  1352.                     hueValue := 0
  1353.                 CP_HSVToRGB(hueValue, 100, 100, red, green, blue)
  1354.                 rowOffset := y * cp_hue_width * 4
  1355.                 Loop, %cp_hue_width% {
  1356.                     x := A_Index - 1
  1357.                     pixelOffset := rowOffset + (x * 4)
  1358.                     NumPut(blue, cp_bits_hue_base + pixelOffset, 0, "UChar")
  1359.                     NumPut(green, cp_bits_hue_base + pixelOffset + 1, 0, "UChar")
  1360.                     NumPut(red, cp_bits_hue_base + pixelOffset + 2, 0, "UChar")
  1361.                     NumPut(255, cp_bits_hue_base + pixelOffset + 3, 0, "UChar")
  1362.                 }
  1363.             }
  1364.             cp_hue_base_ready := true
  1365.         }
  1366.     }
  1367.  
  1368.     DllCall("RtlMoveMemory", "Ptr", cp_bits_hue, "Ptr", cp_bits_hue_base, "UPtr", cp_hue_width * cp_hue_height * 4)
  1369.  
  1370.     markerY := Round((cp_hue / 360) * (cp_hue_height - 1))
  1371.     if (cp_hue = 0)
  1372.         markerY := 0
  1373.     CP_DrawHorizontalMarker(markerY)
  1374. }
  1375.  
  1376. CP_DrawPreview() {
  1377.     global cp_r, cp_g, cp_b, cp_preview_width, cp_preview_height
  1378.     global cp_bitmap_preview, cp_hwnd_preview
  1379.  
  1380.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1381.     if (!hdc)
  1382.         return
  1383.  
  1384.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", cp_bitmap_preview, "Ptr")
  1385.     colorRef := cp_r | (cp_g << 8) | (cp_b << 16)
  1386.     brush := DllCall("CreateSolidBrush", "UInt", colorRef, "Ptr")
  1387.  
  1388.     if (brush) {
  1389.         VarSetCapacity(rect, 16, 0)
  1390.         NumPut(0, rect, 0, "Int")
  1391.         NumPut(0, rect, 4, "Int")
  1392.         NumPut(cp_preview_width, rect, 8, "Int")
  1393.         NumPut(cp_preview_height, rect, 12, "Int")
  1394.         DllCall("FillRect", "Ptr", hdc, "Ptr", &rect, "Ptr", brush)
  1395.         DllCall("DeleteObject", "Ptr", brush)
  1396.         DllCall("GdiFlush")
  1397.     }
  1398.  
  1399.     if (oldBitmap)
  1400.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  1401.     DllCall("DeleteDC", "Ptr", hdc)
  1402.  
  1403.     if (cp_gdip_ready)
  1404.         CP_DrawPreviewBorder()
  1405. }
  1406.  
  1407. CP_DrawPreviewBorder() {
  1408.     global cp_bitmap_preview, cp_preview_width, cp_preview_height, cp_pen_preview
  1409.  
  1410.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1411.     if (!hdc)
  1412.         return
  1413.  
  1414.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", cp_bitmap_preview, "Ptr")
  1415.     graphics := CP_GdipCreateGraphics(hdc)
  1416.     if (graphics) {
  1417.         DllCall("gdiplus\GdipDrawRectangle", "Ptr", graphics, "Ptr", cp_pen_preview, "Float", 0.5, "Float", 0.5, "Float", cp_preview_width - 1.0, "Float", cp_preview_height - 1.0, "UInt")
  1418.         DllCall("gdiplus\GdipDeleteGraphics", "Ptr", graphics, "UInt")
  1419.     }
  1420.  
  1421.     if (oldBitmap)
  1422.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  1423.     DllCall("DeleteDC", "Ptr", hdc)
  1424. }
  1425.  
  1426. CP_DrawCircleMarker(cx, cy) {
  1427.     global cp_bitmap_sv, cp_gdip_ready, cp_pen_outer, cp_pen_inner
  1428.  
  1429.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1430.     if (!hdc)
  1431.         return
  1432.  
  1433.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", cp_bitmap_sv, "Ptr")
  1434.  
  1435.     if (cp_gdip_ready) {
  1436.         graphics := CP_GdipCreateGraphics(hdc)
  1437.         if (graphics) {
  1438.             DllCall("gdiplus\GdipDrawEllipse", "Ptr", graphics, "Ptr", cp_pen_outer, "Float", cx - 7.5, "Float", cy - 7.5, "Float", 15.0, "Float", 15.0, "UInt")
  1439.             DllCall("gdiplus\GdipDrawEllipse", "Ptr", graphics, "Ptr", cp_pen_inner, "Float", cx - 6.5, "Float", cy - 6.5, "Float", 13.0, "Float", 13.0, "UInt")
  1440.             DllCall("gdiplus\GdipDeleteGraphics", "Ptr", graphics, "UInt")
  1441.         }
  1442.     } else {
  1443.         nullBrush := DllCall("GetStockObject", "Int", 5, "Ptr")
  1444.         oldBrush := DllCall("SelectObject", "Ptr", hdc, "Ptr", nullBrush, "Ptr")
  1445.         blackPen := DllCall("CreatePen", "Int", 0, "Int", 3, "UInt", 0x000000, "Ptr")
  1446.         whitePen := DllCall("CreatePen", "Int", 0, "Int", 1, "UInt", 0xFFFFFF, "Ptr")
  1447.         oldPen := DllCall("SelectObject", "Ptr", hdc, "Ptr", blackPen, "Ptr")
  1448.         DllCall("Ellipse", "Ptr", hdc, "Int", cx - 7, "Int", cy - 7, "Int", cx + 8, "Int", cy + 8)
  1449.         DllCall("SelectObject", "Ptr", hdc, "Ptr", whitePen)
  1450.         DllCall("Ellipse", "Ptr", hdc, "Int", cx - 6, "Int", cy - 6, "Int", cx + 7, "Int", cy + 7)
  1451.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldPen)
  1452.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBrush)
  1453.         DllCall("DeleteObject", "Ptr", blackPen)
  1454.         DllCall("DeleteObject", "Ptr", whitePen)
  1455.     }
  1456.  
  1457.     if (oldBitmap)
  1458.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  1459.     DllCall("DeleteDC", "Ptr", hdc)
  1460. }
  1461.  
  1462. CP_DrawHorizontalMarker(y) {
  1463.     global cp_bitmap_hue, cp_hue_width, cp_gdip_ready
  1464.     global cp_pen_outer, cp_pen_inner
  1465.  
  1466.     hdc := DllCall("CreateCompatibleDC", "Ptr", 0, "Ptr")
  1467.     if (!hdc)
  1468.         return
  1469.  
  1470.     oldBitmap := DllCall("SelectObject", "Ptr", hdc, "Ptr", cp_bitmap_hue, "Ptr")
  1471.  
  1472.     if (cp_gdip_ready) {
  1473.         graphics := CP_GdipCreateGraphics(hdc)
  1474.         if (graphics) {
  1475.             DllCall("gdiplus\GdipDrawLine", "Ptr", graphics, "Ptr", cp_pen_outer, "Float", 0.5, "Float", y, "Float", cp_hue_width - 0.5, "Float", y, "UInt")
  1476.             DllCall("gdiplus\GdipDrawLine", "Ptr", graphics, "Ptr", cp_pen_inner, "Float", 0.5, "Float", y, "Float", cp_hue_width - 0.5, "Float", y, "UInt")
  1477.             DllCall("gdiplus\GdipDeleteGraphics", "Ptr", graphics, "UInt")
  1478.         }
  1479.     } else {
  1480.         blackPen := DllCall("CreatePen", "Int", 0, "Int", 3, "UInt", 0x000000, "Ptr")
  1481.         whitePen := DllCall("CreatePen", "Int", 0, "Int", 1, "UInt", 0xFFFFFF, "Ptr")
  1482.         oldPen := DllCall("SelectObject", "Ptr", hdc, "Ptr", blackPen, "Ptr")
  1483.         DllCall("MoveToEx", "Ptr", hdc, "Int", 0, "Int", y, "Ptr", 0)
  1484.         DllCall("LineTo", "Ptr", hdc, "Int", cp_hue_width, "Int", y)
  1485.         DllCall("SelectObject", "Ptr", hdc, "Ptr", whitePen)
  1486.         DllCall("MoveToEx", "Ptr", hdc, "Int", 0, "Int", y, "Ptr", 0)
  1487.         DllCall("LineTo", "Ptr", hdc, "Int", cp_hue_width, "Int", y)
  1488.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldPen)
  1489.         DllCall("DeleteObject", "Ptr", blackPen)
  1490.         DllCall("DeleteObject", "Ptr", whitePen)
  1491.     }
  1492.  
  1493.     if (oldBitmap)
  1494.         DllCall("SelectObject", "Ptr", hdc, "Ptr", oldBitmap, "Ptr")
  1495.     DllCall("DeleteDC", "Ptr", hdc)
  1496. }
  1497.  
  1498. ; -------------------------- Embedded GDI+ rendering --------------------------
  1499.  
  1500. CP_GdipInitialize() {
  1501.     global cp_gdip_token, cp_gdip_module, cp_gdip_ready
  1502.     global cp_pen_outer, cp_pen_inner, cp_pen_preview
  1503.  
  1504.     cp_gdip_module := DllCall("LoadLibrary", "Str", "gdiplus", "Ptr")
  1505.     if (!cp_gdip_module)
  1506.         return false
  1507.  
  1508.     VarSetCapacity(startupInput, A_PtrSize = 8 ? 24 : 16, 0)
  1509.     NumPut(1, startupInput, 0, "UInt")
  1510.     status := DllCall("gdiplus\GdiplusStartup", "UPtr*", cp_gdip_token, "Ptr", &startupInput, "Ptr", 0, "UInt")
  1511.  
  1512.     if (status != 0 || !cp_gdip_token) {
  1513.         DllCall("FreeLibrary", "Ptr", cp_gdip_module)
  1514.         cp_gdip_module := 0
  1515.         cp_gdip_token := 0
  1516.         return false
  1517.     }
  1518.  
  1519.     cp_pen_outer := CP_GdipCreatePen(0xFF05070A, 3.0)
  1520.     cp_pen_inner := CP_GdipCreatePen(0xFFF5F7FA, 1.25)
  1521.     cp_pen_preview := CP_GdipCreatePen(0xFFB7C0CC, 1.0)
  1522.     cp_gdip_ready := !!(cp_pen_outer && cp_pen_inner && cp_pen_preview)
  1523.     return cp_gdip_ready
  1524. }
  1525.  
  1526. CP_GdipCreateGraphics(hdc) {
  1527.     graphics := 0
  1528.     status := DllCall("gdiplus\GdipCreateFromHDC", "Ptr", hdc, "Ptr*", graphics, "UInt")
  1529.     if (status != 0 || !graphics)
  1530.         return 0
  1531.     DllCall("gdiplus\GdipSetSmoothingMode", "Ptr", graphics, "Int", 4, "UInt")
  1532.     return graphics
  1533. }
  1534.  
  1535. CP_GdipCreatePen(argb, width) {
  1536.     pen := 0
  1537.     status := DllCall("gdiplus\GdipCreatePen1", "UInt", argb, "Float", width, "Int", 2, "Ptr*", pen, "UInt")
  1538.     if (status != 0)
  1539.         return 0
  1540.     return pen
  1541. }
  1542.  
  1543. CP_GdipCleanup() {
  1544.     global cp_gdip_token, cp_gdip_module, cp_gdip_ready
  1545.     global cp_pen_outer, cp_pen_inner, cp_pen_preview
  1546.  
  1547.     if (cp_pen_outer)
  1548.         DllCall("gdiplus\GdipDeletePen", "Ptr", cp_pen_outer, "UInt")
  1549.     if (cp_pen_inner)
  1550.         DllCall("gdiplus\GdipDeletePen", "Ptr", cp_pen_inner, "UInt")
  1551.     if (cp_pen_preview)
  1552.         DllCall("gdiplus\GdipDeletePen", "Ptr", cp_pen_preview, "UInt")
  1553.  
  1554.     cp_pen_outer := 0
  1555.     cp_pen_inner := 0
  1556.     cp_pen_preview := 0
  1557.     cp_gdip_ready := false
  1558.  
  1559.     if (cp_gdip_token)
  1560.         DllCall("gdiplus\GdiplusShutdown", "Ptr", cp_gdip_token)
  1561.     cp_gdip_token := 0
  1562.  
  1563.     if (cp_gdip_module)
  1564.         DllCall("FreeLibrary", "Ptr", cp_gdip_module)
  1565.     cp_gdip_module := 0
  1566. }
  1567.  
  1568. ; -------------------------- Color conversion and input validation ------------
  1569.  
  1570. CP_HSVToRGB(h, s, v, ByRef red, ByRef green, ByRef blue) {
  1571.     h += 0
  1572.     s := s / 100
  1573.     v := v / 100
  1574.  
  1575.     if (h < 0)
  1576.         h := 0
  1577.     if (h >= 360)
  1578.         h := Mod(h, 360)
  1579.  
  1580.     chroma := v * s
  1581.     sector := h / 60
  1582.     x := chroma * (1 - Abs(Mod(sector, 2) - 1))
  1583.     match := v - chroma
  1584.  
  1585.     if (sector < 1)
  1586.         r1 := chroma, g1 := x, b1 := 0
  1587.     else if (sector < 2)
  1588.         r1 := x, g1 := chroma, b1 := 0
  1589.     else if (sector < 3)
  1590.         r1 := 0, g1 := chroma, b1 := x
  1591.     else if (sector < 4)
  1592.         r1 := 0, g1 := x, b1 := chroma
  1593.     else if (sector < 5)
  1594.         r1 := x, g1 := 0, b1 := chroma
  1595.     else
  1596.         r1 := chroma, g1 := 0, b1 := x
  1597.  
  1598.     red := Round((r1 + match) * 255)
  1599.     green := Round((g1 + match) * 255)
  1600.     blue := Round((b1 + match) * 255)
  1601. }
  1602.  
  1603. CP_RGBToHSV(red, green, blue, ByRef h, ByRef s, ByRef v) {
  1604.     redN := red / 255
  1605.     greenN := green / 255
  1606.     blueN := blue / 255
  1607.  
  1608.     maxValue := redN
  1609.     if (greenN > maxValue)
  1610.         maxValue := greenN
  1611.     if (blueN > maxValue)
  1612.         maxValue := blueN
  1613.  
  1614.     minValue := redN
  1615.     if (greenN < minValue)
  1616.         minValue := greenN
  1617.     if (blueN < minValue)
  1618.         minValue := blueN
  1619.  
  1620.     delta := maxValue - minValue
  1621.  
  1622.     ; Hue is undefined for gray and black. Preserve the caller's current hue
  1623.     ; so dragging the square to V=0 (or S=0) does not snap the hue slider to
  1624.     ; red and teleport the SV marker to the bottom-left corner.
  1625.     if (delta != 0) {
  1626.         if (maxValue = redN) {
  1627.             h := 60 * ((greenN - blueN) / delta)
  1628.             if (h < 0)
  1629.                 h += 360
  1630.         } else if (maxValue = greenN)
  1631.             h := 60 * (((blueN - redN) / delta) + 2)
  1632.         else
  1633.             h := 60 * (((redN - greenN) / delta) + 4)
  1634.  
  1635.         h := Round(h)
  1636.         if (h >= 360)
  1637.             h := 0
  1638.     }
  1639.  
  1640.     if (maxValue = 0)
  1641.         s := 0
  1642.     else
  1643.         s := (delta / maxValue) * 100
  1644.  
  1645.     v := maxValue * 100
  1646.     s := Round(s)
  1647.     v := Round(v)
  1648. }
  1649.  
  1650. CP_ParseHex(hexText, ByRef red, ByRef green, ByRef blue) {
  1651.     hexText := Trim(hexText)
  1652.     StringReplace, hexText, hexText, #,, All
  1653.     StringReplace, hexText, hexText, 0x,, All
  1654.  
  1655.     if (StrLen(hexText) = 3 && RegExMatch(hexText, "i)^[0-9a-f]{3}$")) {
  1656.         red := "0x" . SubStr(hexText, 1, 1) . SubStr(hexText, 1, 1)
  1657.         green := "0x" . SubStr(hexText, 2, 1) . SubStr(hexText, 2, 1)
  1658.         blue := "0x" . SubStr(hexText, 3, 1) . SubStr(hexText, 3, 1)
  1659.         red += 0
  1660.         green += 0
  1661.         blue += 0
  1662.         return true
  1663.     }
  1664.  
  1665.     if (StrLen(hexText) != 6 || !RegExMatch(hexText, "i)^[0-9a-f]{6}$"))
  1666.         return false
  1667.  
  1668.     red := "0x" . SubStr(hexText, 1, 2)
  1669.     green := "0x" . SubStr(hexText, 3, 2)
  1670.     blue := "0x" . SubStr(hexText, 5, 2)
  1671.     red += 0
  1672.     green += 0
  1673.     blue += 0
  1674.     return true
  1675. }
  1676.  
  1677. CP_IsIntegerInRange(value, minimum, maximum) {
  1678.     value := Trim(value)
  1679.     if (value = "" || !RegExMatch(value, "^\d+$"))
  1680.         return false
  1681.     number := value + 0
  1682.     return (number >= minimum && number <= maximum)
  1683. }
  1684.  
  1685. CP_UpdateFields(skipHwnd := 0) {
  1686.     global cp_updating, cp_r, cp_g, cp_b, cp_hue, cp_sat, cp_val, cp_hex
  1687.     global cp_hwnd_edit_r, cp_hwnd_edit_g, cp_hwnd_edit_b
  1688.     global cp_hwnd_edit_h, cp_hwnd_edit_s, cp_hwnd_edit_v, cp_hwnd_edit_hex
  1689.  
  1690.     cp_updating := true
  1691.  
  1692.     if (skipHwnd != cp_hwnd_edit_r)
  1693.         GuiControl,, CP_EditR, %cp_r%
  1694.     if (skipHwnd != cp_hwnd_edit_g)
  1695.         GuiControl,, CP_EditG, %cp_g%
  1696.     if (skipHwnd != cp_hwnd_edit_b)
  1697.         GuiControl,, CP_EditB, %cp_b%
  1698.     if (skipHwnd != cp_hwnd_edit_h)
  1699.         GuiControl,, CP_EditH, %cp_hue%
  1700.     if (skipHwnd != cp_hwnd_edit_s)
  1701.         GuiControl,, CP_EditS, %cp_sat%
  1702.     if (skipHwnd != cp_hwnd_edit_v)
  1703.         GuiControl,, CP_EditV, %cp_val%
  1704.  
  1705.     cp_hex := Format("{:02X}{:02X}{:02X}", cp_r, cp_g, cp_b)
  1706.     if (skipHwnd != cp_hwnd_edit_hex)
  1707.         GuiControl,, CP_EditHex, %cp_hex%
  1708.  
  1709.     cp_updating := false
  1710. }
  1711.  
  1712. CP_Cleanup(exitReason := "", exitCode := 0) {
  1713.     global cp_drag_mode, cp_drag_hwnd, cp_drag_timer_pending
  1714.     global cp_bitmap_sv, cp_bitmap_sv_base, cp_bitmap_hue, cp_bitmap_hue_base
  1715.     global cp_bitmap_preview, cp_bitmap_overlay, cp_bitmap_eyedropper
  1716.     global cp_hwnd_sv, cp_hwnd_hue, cp_hwnd_preview, cp_hwnd_eyedropper_icon
  1717.     global cp_screen_pick_active, cp_screen_pick_cursor_saved
  1718.  
  1719.     ; Cancel any in-flight screen pick so its hotkeys and cursor are released.
  1720.     if (cp_screen_pick_active) {
  1721.         cp_screen_pick_active := false
  1722.         SetTimer, CP_ScreenPickTrack, Off
  1723.         try {
  1724.             Hotkey, *LButton, Off
  1725.             Hotkey, *RButton, Off
  1726.             Hotkey, *Escape,  Off
  1727.         }
  1728.         CP_PickPanelHide()
  1729.     }
  1730.  
  1731.     CP_RestoreSystemCursors()
  1732.  
  1733.     SetTimer, CP_DragUpdate, Off
  1734.     cp_drag_mode := ""
  1735.     cp_drag_hwnd := 0
  1736.     cp_drag_timer_pending := false
  1737.  
  1738.     if (cp_hwnd_sv)
  1739.         DllCall("SendMessage", "Ptr", cp_hwnd_sv, "UInt", 0x172, "Ptr", 0, "Ptr", 0, "Ptr")
  1740.     if (cp_hwnd_hue)
  1741.         DllCall("SendMessage", "Ptr", cp_hwnd_hue, "UInt", 0x172, "Ptr", 0, "Ptr", 0, "Ptr")
  1742.     if (cp_hwnd_preview)
  1743.         DllCall("SendMessage", "Ptr", cp_hwnd_preview, "UInt", 0x172, "Ptr", 0, "Ptr", 0, "Ptr")
  1744.     if (cp_hwnd_eyedropper_icon)
  1745.         DllCall("SendMessage", "Ptr", cp_hwnd_eyedropper_icon, "UInt", 0x172, "Ptr", 0, "Ptr", 0, "Ptr")
  1746.  
  1747.     ; The screen-pick overlay and its Gdip resources must be released before
  1748.     ; GdipShutdown runs, since Gdip_DeleteGraphics/Gdip_DeleteBrush/etc still
  1749.     ; need a live GDI+ runtime.
  1750.     CP_PickPanelCleanup()
  1751.  
  1752.     CP_GdipCleanup()
  1753.  
  1754.     if (cp_bitmap_sv)
  1755.         DllCall("DeleteObject", "Ptr", cp_bitmap_sv)
  1756.     if (cp_bitmap_sv_base)
  1757.         DllCall("DeleteObject", "Ptr", cp_bitmap_sv_base)
  1758.     if (cp_bitmap_hue)
  1759.         DllCall("DeleteObject", "Ptr", cp_bitmap_hue)
  1760.     if (cp_bitmap_hue_base)
  1761.         DllCall("DeleteObject", "Ptr", cp_bitmap_hue_base)
  1762.     if (cp_bitmap_preview)
  1763.         DllCall("DeleteObject", "Ptr", cp_bitmap_preview)
  1764.     if (cp_bitmap_overlay)
  1765.         DllCall("DeleteObject", "Ptr", cp_bitmap_overlay)
  1766.     if (cp_bitmap_eyedropper)
  1767.         DllCall("DeleteObject", "Ptr", cp_bitmap_eyedropper)
  1768. }
Advertisement
Add Comment
Please, Sign In to add comment