Jump to content

Color Picker for GUI Contols


cybie
 Share

Recommended Posts

I was making a custom web page generator that would basically just stringreplace to customize a web page for individual web-affiliates on the fly. During this process, I thought it would be easiest to match the color on their current website by using a color-picker that most people are used to using. This is in no way comparable to xDrop(josbe) http://www.autoitscript.com/forum/index.ph...c=7027&hl=xdrop :)

I must also thank Larry for the Magnify DLLCall, without which this would not have been as cool. :D

Updated Below...

I'm sure there are some improvements that can be made, but it's a functional start. I used this to populate the fields, then when the submit button is pressed all of the inputs are read using guictrlread and used as the strings that will replace the pre-marked strings in my HTML file. :D

Edited by cybie

Writing damaged code since 1996.

Link to comment
Share on other sites

Nice, a suggestion.

> Make it so that you can manually enter a colour.

> One bug sorta, if you click on a black square the magnify thing comes up, once you hit F9 it disappears but you can still hit F9 and pick a colour without the magnifier

Good job!

Edit: One more bug, If you load the script and press F9 Straight away you get an error.

Edit2: Another suggestion =), Have the background black label preview thing "White" and the link colour preview thing "blue" on startup.

Edit3: Last one I swear :). When the magnifier comes up you should have a preview of the colour so you know what colour is beneath the mouse, perhaps chaning the label that you click on to activiate the magnifier will change as the colour changes.

Edit4: One more, there are some issues when you click on the label to make the magnifier appear and pressing F9, sometimes F9 triggers it, other times the label.

Edited by burrup

qq

Link to comment
Share on other sites

Nice, a suggestion.

> Make it so that you can manually enter a colour.

> One bug sorta, if you click on a black square the magnify thing comes up, once you hit F9 it disappears but you can still hit F9 and pick a colour without the magnifier

Good job!

Edit: One more bug, If you load the script and press F9 Straight away you get an error.

<{POST_SNAPBACK}>

Dually noted. What I wanted to happen was to change the cursor to a crosshair, but as far as i could tell, that could only be accomplished by making a big transparent GUI window and changing the cursor within that. The transparancy doesn't work in all Windows versions right?

Other than that, I wanted to make it capture the color on mouse click, but I couldn't find any way to detect the mouse click... :)

Does anyone think that it would be acceptable to make a big, fully transparent GUI window with the crosshair cursor and a giant button so that anywhere they click , it clicks the button to initiate the color capture, then returns killing the big transparent GUI? :D

Writing damaged code since 1996.

Link to comment
Share on other sites

I added some more "Edits" above lol. I also made a program like this and i experienced the EXACT same herdle, of trying to make the cursor a crosshair and how to detect a mouse click.

To detect a mouse click you can use the UDF _IsPressed() but the click is still performed.

eg, I have the mouse over a button, I click it gets the colour but the button is pressed still :).

In the end I ended up doing what you done and just put it on a Hotkey.

As for the giant window, its quite an idea lol.

qq

Link to comment
Share on other sites

This should get you started on the Giant window lol.

#include <GUIConstants.au3>

$test = GUICreate("test", @DesktopWidth, @DesktopHeight, 0, 0, BitOR($WS_BORDER, $WS_POPUP), 

BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))

GUISetState(@SW_SHOW)

GUISetCursor (1,1,$test)

WinSetTrans ($test, "", 0)

While 1
   If _IsPressed(01) Then; '01' is the left mouse button
      $Pos = MouseGetPos()
      $Dec_Colour = PixelGetColor($Pos[0],$Pos[1])
      $Hex_Colour = Hex ($Dec_Colour,6)
      GUISetState(@SW_HIDE)
      GUISetCursor (2)
      Msgbox(0,"Hex Colour", $Hex_Colour)
   EndIf
Wend

Func _IsPressed($hexKey)
   Local $aR
   $hexKey = '0x' & $hexKey
   $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)
   If Not @error And BitAND($aR[0], 0x8000) = 0x8000 Then Return 1
   Return 0
EndFunc

qq

Link to comment
Share on other sites

OK, I took _ispressed function into consideration, but I think the button method is possibly better. There will bo no click transferred to the application under the cursor because there is a window with a button in the way. :)

Basically, this is the same thing as the last version, but when you click on the color box a new window is created at the size of the screen and at 1% opacity, then the cursor changes to a crosshair with the magnifier on the bottom right. Once you click on the color you like the large screen is hidden (not really visible to begin with) and you are ready to move on with the form or click another color picker box.

Problems:

1. Because the window is in the way at 1% opacity, the colors may be slightly different than the actual colors on the screen.

(UPDATE)

2. Burrup suggested that the color change within the color box as you move. I agree that this would be cool, but id not build it in yet. (FIXED)

3. The color-box does not initially read the contents of the inputbox next to it. I think this could probably be done easily, but you wouldn't always necessarily have a text box beside this...

Updated Code:

#cs
   
   AutoIt Version: 3.1.1 (beta)
   Language:       English
   Platform:       WinXP
   Author:         Cybie
   
   Script Function:
   Color-picker function for use as a GUI Control
   
#ce

#include <GuiConstants.au3>
HotKeySet("{ESC}", "Terminate")
Opt ("GUICoordMode", 0)
Opt ("WinTitleMatchMode", 4)
$Color = 0x000000;First color displayed in all boxes

GuiCreate ("Color Picker Demo", 250, 75)

; ----------------------------------------------------------------------------
;INPUT FIELDS
; ----------------------------------------------------------------------------
GUICtrlCreateLabel ("Background Color:", 5, 7, 100, 20, 0x0002)
$Background = GUICtrlCreateInput ("#FFFFFF", 105, -2, 100, 20)
$ColorBox1 = GuiCtrlCreateLabel ("", 105, 0, 20, 20)
GuiCtrlSetBkColor ($ColorBox1, $Color)

GUICtrlCreateLabel ("Text Color:", -210, 25, 100, 20, 0x0002)
$TxtColor = GUICtrlCreateInput ("#000000", 105, -2, 100, 20)
$ColorBox2 = GuiCtrlCreateLabel ("", 105, 0, 20, 20)
GuiCtrlSetBkColor ($ColorBox2, $Color)

GUICtrlCreateLabel ("Link Color:", -210, 25, 100, 20, 0x0002)
$Links = GUICtrlCreateInput ("#0000FF", 105, -2, 100, 20)
$ColorBox3 = GuiCtrlCreateLabel ("", 105, 0, 20, 20)
GuiCtrlSetBkColor ($ColorBox3, $Color)

GuiSetState (@SW_SHOW)

; ----------------------------------------------------------------------------
;RUN LOOP
; ----------------------------------------------------------------------------
$msg = 0
While $msg <> $GUI_EVENT_CLOSE
   $msg = GUIGetMsg ()
   
   Select
      Case $msg = $ColorBox1
         $CurrentColorBox = $ColorBox1
         $CurrentColorInput = $Background
         ColorPicker()
         
      Case $msg = $ColorBox2
         $CurrentColorBox = $ColorBox2
         $CurrentColorInput = $TxtColor
         ColorPicker()
         
      Case $msg = $ColorBox3
         $CurrentColorBox = $ColorBox3
         $CurrentColorInput = $Links
         ColorPicker()
   EndSelect
Wend

; ----------------------------------------------------------------------------
; FUNCTIONS
; ----------------------------------------------------------------------------

Func ColorPicker()
   GuiCreate ("Color Picker Mask", @DesktopWidth, @DesktopHeight, -1, -1, 0x80000000)
   WinSetTrans ("Color Picker Mask", "", 1);big trans window...
   $Capture = GuiCtrlCreateButton ("", 0, 0, @DesktopWidth, @DesktopHeight)
   GuiSetState (@SW_SHOW)
   GUISetCursor (3, 1)
   Local $SRCCOPY = 0x00CC0020
   Local $MousePos
   ToolTip("AU3MAG", 0, 0)
   $MyhWnd = WinGetHandle("classname=tooltips_class32")
   
   $CrntColor = $Color
   $msg2 = 0
   While $Color = $CrntColor
      $msg2 = GUIGetMsg ()
      
      $MousePos = MouseGetPos()
      $PixColor = PixelGetColor($MousePos[0], $MousePos[1])
      $TempColor = "0x" & Hex($PixColor, 6)
      GuiCtrlSetBkColor ($CurrentColorBox, $TempColor)
      GUICtrlSetData ($CurrentColorInput, StringReplace($TempColor, "0x", "#"))
      
      Select
         Case $msg2 = $Capture
            $MousePos = MouseGetPos()
            $PixColor = PixelGetColor($MousePos[0], $MousePos[1])
            $Color = "0x" & Hex($PixColor, 6)
            ToolTip("", 0, 0)
            GuiCtrlSetBkColor ($CurrentColorBox, $Color)
            GUICtrlSetData ($CurrentColorInput, StringReplace($Color, "0x", "#"))
            GuiSetState (@SW_HIDE)
      EndSelect
      Sleep(25)
      $MyHDC = DLLCall ("user32.dll", "int", "GetDC", "hwnd", $MyhWnd)
      If @error Then Return
      $DeskHDC = DLLCall ("user32.dll", "int", "GetDC", "hwnd", 0)
      If Not @error Then
         $xy = MouseGetPos()
         If Not @error Then
            $l = $xy[0] - 10
            $t = $xy[1] - 10
            DLLCall ("gdi32.dll", "int", "StretchBlt", "int", $MyHDC[0], "int", _
                  0, "int", 0, "int", 100, "int", 100, "int", $DeskHDC[0], "int", _
                  $l, "int", $t, "int", 20, "int", 20, "long", $SRCCOPY)
            WinMove($MyhWnd, "", $xy[0] + 20, $xy[1] + 20, 100, 100)
         EndIf
         DLLCall ("user32.dll", "int", "ReleaseDC", "int", $DeskHDC[0], "hwnd", 0)
      EndIf
      DLLCall ("user32.dll", "int", "ReleaseDC", "int", $MyHDC[0], "hwnd", $MyhWnd)
   Wend
EndFunc  ;==>ColorPicker

Func Terminate()
   Exit 0
EndFunc  ;==>Terminate

I welcome improvements!!! :D

EDIT: I removed the colorcaprture function as that is now integrated. My bad...

Edited by cybie

Writing damaged code since 1996.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...