Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Windows Color Picker Plus


  • Please log in to reply
22 replies to this topic
rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
In the forums there are several functions to display the standard Windows color picker dialog. However, this version has IMO some significant advantages over those previously posted:
  • The values passed to and returned from the function are in RGB format. This is the format that most of the AHK commands expect for color information.
  • The 16 custom colors in the dialog can be preset before the dialog is displayed.
  • The custom colors are remembered between function calls.
  • Any changes to the custom colors can be saved in an array when the function exits. This allows the custom colors to stored in a file and reloaded later.
  • The X, Y location of the dialog can be set before it is displayed.
  • The dialog can be assigned an owner window. The dialog will always be in front of its owner, if it has one.
  • Tested on 32/64-bit, ANSI/Unicode
3-11-2013: Initial release
3-12-2013: Added a new parameter, hOwner. The Window ID of the dialog's owner, if it has one. Defaults to 0, i.e. No Owner. Thanks jballi grin.png
6-3-2013: BUGFIX Dialog wasn't displaying on 64-bit systems. Fixed thanks to Rseding91 and maestrith.
7-2-2013: BUGFIX Custom colors were not displaying correctly on 64-bit systems. Fixed thanks to Pulover.
 
Here is a very short test script:
#NoEnv
#SingleInstance force
SetBatchLines -1
ListLines Off
SendMode Input
SetWorkingDir %A_ScriptDir%

#Include ChooseColor.ahk

; AutoExecute
    SetFormat IntegerFast, H
   
    Gui +hwndGuiHwnd
    Gui Color, 0x888888, ControlColor
    Gui Show, w500 h500
    Colors := [0x00FF00, 0xFF0000, 0xFF00FF]
    MyColor := ChooseColor(0x80FF, GuiHwnd, , , Colors*)
    Gui Color, %MyColor%
    MsgBox % ErrorLevel
ExitApp
The Functions:
/*!
    Function: ChooseColor([pRGB, hOwner, DlgX, DlgY, Palette])
        Displays a standard Windows dialog for choosing colors.

    Parameters:
        pRGB - The initial color to display in the dialog in RGB format.
               The default setting is Black.
        hOwner - The Window ID of the dialog's owner, if it has one. Defaults to
                0, i.e. no owner. If specified DlgX and DlgY are ignored.
        DlgX, DlgY - The X and Y coordinates of the upper left corner of the 
                     dialog. Both default to 0.
        Palette - An array of up to 16 RGB color values. These become the 
                  initial custom colors in the dialog.

    Remarks:
        The custom colors in the dialog are remembered between calls.
        
        If the user selects OK, the Palette array (if it exists) will be loaded 
        with the custom colors from the dialog. 

    Returns:
        If the user selects OK, the selected color is returned in RGB format 
        and ErrorLevel is set to 0. Otherwise, the original pRGB value is 
        returned and ErrorLevel is set to 1.
*/
ChooseColor(pRGB := 0, hOwner := 0, DlgX := 0, DlgY := 0, Palette*)
{
    static CustColors    ; Custom colors are remembered between calls
    static SizeOfCustColors := VarSetCapacity(CustColors, 64, 0)
    static StructSize := VarSetCapacity(ChooseColor, 9 * A_PtrSize, 0)
    
    CustData := (DlgX << 16) | DlgY    ; Store X in high word, Y in the low word

;___Load user's custom colors
    for Index, Value in Palette
        NumPut(BGR2RGB(Value), CustColors, (Index - 1) * 4, "UInt")

;___Set up a ChooseColor structure as described in the MSDN
    NumPut(StructSize, ChooseColor, 0, "UInt")
    NumPut(hOwner, ChooseColor, A_PtrSize, "UPtr")
    NumPut(BGR2RGB(pRGB), ChooseColor, 3 * A_PtrSize, "UInt")
    NumPut(&CustColors, ChooseColor, 4 * A_PtrSize, "UPtr")
    NumPut(0x113, ChooseColor, 5 * A_PtrSize, "UInt")
    NumPut(CustData, ChooseColor, 6 * A_PtrSize, "UInt")
    NumPut(RegisterCallback("ColorWindowProc"), ChooseColor, 7 * A_PtrSize, "UPtr")

;___Call the function
    ErrorLevel := ! DllCall("comdlg32\ChooseColor", "UPtr", &ChooseColor, "UInt")

;___Save the changes made to the custom colors
    if not ErrorLevel
        Loop 16
            Palette[A_Index] := BGR2RGB(NumGet(CustColors, (A_Index - 1) * 4, "UInt"))
        
    return BGR2RGB(NumGet(ChooseColor, 3 * A_PtrSize, "UINT"))
}

/*!
    Function: ColorWindowProc(hwnd, msg, wParam, lParam)
        Callback function used to modify the Color dialog before it is displayed

    Parameters:
        hwnd - Handle to the Color dialog window.
        msg - The message sent to the window.
        wParam - The handle to the control that has the keyboard focus.
        lParam - A pointer to the ChooseColor structure associated with the 
                 Color dialog.

    Remarks:
        This is intended to be a private function, called only by ChooseColor. 
        In response to a WM_INITDIALOG message, this function can be used to 
        modify the Color dialog before it is displayed. Currently it just moves 
        the window to a new X, Y location.

    Returns:
        If the hook procedure returns zero, the default dialog box procedure 
        also processes the message. Otherwise, the default dialog box procedure 
        ignores the message.
*/
ColorWindowProc(hwnd, msg, wParam, lParam)
{
    static WM_INITDIALOG := 0x0110
    
    if (msg <> WM_INITDIALOG)
        return 0
    
    hOwner := NumGet(lParam+0, A_PtrSize, "UPtr")
    if (hOwner)
        return 0

    DetectSetting := A_DetectHiddenWindows
    DetectHiddenWindows On
    CustData := NumGet(lParam+0, 6 * A_PtrSize, "UInt")
    DlgX := CustData >> 16, DlgY := CustData & 0xFFFF
    WinMove ahk_id %hwnd%, , %DlgX%, %DlgY%
    
    DetectHiddenWindows %DetectSetting%
    return 0
}

/*!
    Function: BGR2RGB(Color)
        Converts a BGR color value to a RGB one or vice versa.

    Parameters:
        Color - The BGR or RGB value to convert

    Returns:
        The converted value.
*/
BGR2RGB(Color)
{
    return  (Color & 0xFF000000) 
         | ((Color & 0xFF0000) >> 16) 
         |  (Color & 0x00FF00) 
         | ((Color & 0x0000FF) << 16)
}

Edited by rbrtryn, 02 July 2013 - 05:39 PM.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Very interesting. Saving the custom colors is a nice feature. I've never found a use for it but it is a nice feature.

One comment. You probably should include an optional "Owner" parameter in the ChooseColor function. If specified, the dialog become modal and for the most part, reduces the need/value of the x and y parameters.

Looks good. Thanks for sharing.

A v i
  • Members
  • 1323 posts
  • Last active: Nov 14 2015 06:56 PM
  • Joined: 30 Jan 2013

Looks Great!!

But,

I copied your code and ran it and Scite showed--

S:\Portables\AutoHotkey\My Scripts\Comuntiy Packages\Choosecolor.ahk (30) : ==> This line does not contain a recognized action.
Specifically: static CustColors

 

Any Help??


Now a CS Undergrad. | My WebsiteAutohotkey Scripts | Softwares

Telegram me : @aviaryan


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

Very interesting. Saving the custom colors is a nice feature. I've never found a use for it but it is a nice feature.

 
One use is to save the custom colors to a file, so that the next time the program is run they could be reloaded.
 

One comment. You probably should include an optional "Owner" parameter in the ChooseColor function. If specified, the dialog become modal and for the most part, reduces the need/value of the x and y parameters.

 
That's a great idea! Do you think the Owner parameter should come before or after the X, Y coordinates in the parameter list?
 

I copied your code and ran it and Scite showed--

S:\Portables\AutoHotkey\My Scripts\Comuntiy Packages\Choosecolor.ahk (30) : ==> This line does not contain a recognized action.
Specifically: static CustColors


 
You need at least v1.1.09 of AHK in order to use this as is. What version are you running? If you want to run this with a previous version you need to change this line
ChooseColor(pRGB := 0, DlgX := 0, DlgY := 0, Palette := 0)
to
ChooseColor(pRGB = 0, DlgX = 0, DlgY = 0, Palette = 0)

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


robertcollier4
  • Members
  • 141 posts
  • Last active: Jun 28 2019 01:05 AM
  • Joined: 11 Nov 2012

I was also not able to get it to run since am using AutoHotkey Classic.

Found this alternative.



jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

That's a great idea! Do you think the Owner parameter should come before or after the X, Y coordinates in the parameter list?
ChooseColor(pRGB = 0, DlgX = 0, DlgY = 0, Palette = 0)

It doesn't matter (OK, put it before (or first)). After you get it working, you'll find that the X/Y parameters aren't really needed if the Owner is specified.

 

I was also not able to get it to run since am using AutoHotkey Classic.
Found this alternative.

The Dlg_Color function within the Dlg library from Forms Framework 0.8 published a few years back by majkinetor still works with AutoHotkey Basic. It is not as sophisticated as the function published here but it will do the trick without using an outside program. You can find it here:

http://www.autohotke...s-framework-08/



rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Posted a new version which includes jballi's excellent suggestion of a dialog owner.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

I had a heck of a time getting the forum software to load the new version properly angry.png . All should be good now.


My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Sanctus
  • Members
  • 283 posts
  • Last active: Dec 06 2014 04:05 PM
  • Joined: 30 Nov 2012
#NoEnv
#SingleInstance force
SetBatchLines -1
ListLines Off
SendMode Input
SetWorkingDir %A_ScriptDir%

#Include ChooseColor.ahk

; AutoExecute
    SetFormat IntegerFast, H
   
    Gui +hwndGuiHwnd
    Gui Show, w500 h500
    Colors := [0x00FF00, 0xFF0000, 0xFF00FF]
    MyColor := ChooseColor(0xFFFF00, , 200, 200, Colors)
    MyColor := ChooseColor(0x80FF, GuiHwnd, , , Colors)
    gui, Color, %MyColor%
    MsgBox % MyColor
ExitApp

 

I added gui, Color, %MyColor% just for a visual confirmation that it picked the same colour as me. (MsgBox stops the program from closing)


Check out ALL My Scripts  ;)


A v i
  • Members
  • 1323 posts
  • Last active: Nov 14 2015 06:56 PM
  • Joined: 30 Jan 2013

@rbrtryn

 

I have tried with both ahk_basic and ahk_l latest version (Version v1.1.09.02).I also tried it both with unicode and ansi.

Confused??confused.png 

 

Now a CS Undergrad. | My WebsiteAutohotkey Scripts | Softwares

Telegram me : @aviaryan


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

I have tried with both ahk_basic and ahk_l latest version (Version v1.1.09.02).I also tried it both with unicode and ansi.
Confused??confused.png 

I don't understand. Are you saying that the function doesn't work for you? Are you getting an error message? If so, what is it?

Don't even try this with Basic, it won't work.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


A v i
  • Members
  • 1323 posts
  • Last active: Nov 14 2015 06:56 PM
  • Joined: 30 Jan 2013

I don't understand. Are you saying that the function doesn't work for you? Are you getting an error message? If so, what is it?

Yes, the fuction doesnt work..

Shows error  at  static CustColors -- See Below

S:\Portables\AutoHotkey\My Scripts\Comuntiy Packages\Choosecolor.ahk (30) : ==> This line does not contain a recognized action.
Specifically: static CustColors

I have tried everything , using #include <func. script> or without #include..                   [Using ahk_l latest]

Just not working.confused.png

 

and, so i'm stuck to my old color picker.


Now a CS Undergrad. | My WebsiteAutohotkey Scripts | Softwares

Telegram me : @aviaryan


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

I have tried everything , using #include <func. script> or without #include..

 

If you put it in one of the standard libraries then don't add the .ahk extension. This works on my system:

#NoEnv
#SingleInstance force
SetBatchLines -1
ListLines Off
SendMode Input
SetWorkingDir %A_ScriptDir%

#Include <ChooseColor>

; AutoExecute
    SetFormat IntegerFast, H
   
    Gui +hwndGuiHwnd
    Gui Show, w500 h500
    Colors := [0x00FF00, 0xFF0000, 0xFF00FF]
    MyColor := ChooseColor(0x80FF, GuiHwnd, , , Colors)
    Gui Color, %MyColor%
    MsgBox % MyColor
ExitApp

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


A v i
  • Members
  • 1323 posts
  • Last active: Nov 14 2015 06:56 PM
  • Joined: 30 Jan 2013

Dear rbrtryn,

 

Sorry for troubling youconfused.png , but i seriously cant get the thing to work..I followed every instruction of yours, still the same error..

I'm Surprised.

 

Error in  static CustColors.

 

oQa1F02.jpg


Now a CS Undergrad. | My WebsiteAutohotkey Scripts | Softwares

Telegram me : @aviaryan


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

BUGFIX: Dialog wasn't displaying on 64-bit systems. Fixed thanks to Rseding91 and maestrith


My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!