Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

How to call the standard Choose-Color-dialog?


  • Please log in to reply
12 replies to this topic
AGU
  • Guests
  • Last active:
  • Joined: --
Hi,

I'm looking a for a way to call the standard color picker from Windows.
Here's an article in MSDN:
MSDN - Color DialogBox
And another one. Don't know if this is useful
http://msdn.microsof... ... lorPicker/

I already found this posting about the standard ChooseFont dialog. Chris said there, this method could be used to call the standard color picker, too.
http://www.autohotke.../topic5039.html
But I'm not good at DllCall, Structures and all this things. I would appreciate any help.

During a forum search I also found this 'dirty' color picker. But it just offers three sliders and doesn't show any basic colors for easier orientation like in the standard dialogue. I want a color picker like the standard one that lets you choose a color (by mouse?) and returns the RGB HEX value. (it should work at least in W2k and newer versions)
http://www.autohotke.../topic6693.html

So, anybody out there who is interested in doing some nice DllCall ;)
___________________
Cheers
AGU

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear AGU, :D

Is this request related to your topic CMS for autohotkey.net ? Just curious!

During a forum search I also found this 'dirty' color picker. But it just offers three sliders and doesn't show any basic colors for easier orientation like in the standard dialogue.


That is the script that I have been using extensively. I have modified it to a large extent, into a Function. I have not modified the GUI much, just to keep it simple.. Now after your post, I wonder If I should include the basic Websafe colours into the GUI.

Basic Color Codes: http://www.computerh...om/htmcolor.htm

I will be posting my improved version of ColorPicker in forum's Script section soon. This is for your information.

I wish you all the success in bringing out an another fine utility like BBcode Writer.

Regards, :)
kWo4Lk1.png

Micha
  • Members
  • 539 posts
  • Last active: Dec 31 2011 01:43 PM
  • Joined: 15 Nov 2005
Hi,
I've changed the original script a little bit...and it works.
Very interesting, what can be done with structs (Extract/insertinteger)

Gui, Add, Button, Default gButtonSelectFont, Press this to show font-picker dialog 
Gui, Add, Text, w60 right xm, RGB Color: 
Gui, Add, Edit, x+10 ReadOnly vColor 

; Most of the setup for the font picker dialog needs to be done only once, 
; which is why this section is here rather than in the button's subroutine.
/*
typedef struct tagCHOOSECOLOR {
  DWORD lStructSize;
  HWND hwndOwner;
  HINSTANCE hInstance;
  COLORREF rgbResult;
  COLORREF* lpCustColors;
  DWORD Flags;
  LPARAM lCustData;
  LPCCHOOKPROC lpfnHook;
  LPCTSTR lpTemplateName;
} CHOOSECOLOR, *LPCHOOSECOLOR;
*/ 
SizeOfStructForChooseColor = 0x24 
VarSetCapacity(StructForChooseColor, SizeOfStructForChooseColor, 0) 
VarSetCapacity(StructArrayForChooseColor, 64, 0) 

Gui, +LastFound 
GuiHWND := WinExist()  ; Relies on the line above to get the unique ID of GUI window. 

InsertInteger(SizeOfStructForChooseColor, StructForChooseColor, 0)  ; DWORD lStructSize 
InsertInteger(GuiHWND, StructForChooseColor, 4)  ; HWND hwndOwner (makes dialog "modal"). 
InsertInteger(0x0 ,    StructForChooseColor, 8)  ; HINSTANCE hInstance 
InsertInteger(0x0 ,    StructForChooseColor, 12)  ; clr.rgbResult =  0;
InsertInteger(&StructArrayForChooseColor , StructForChooseColor, 16)  ; COLORREF *lpCustColors
InsertInteger(0x00000100 , StructForChooseColor, 20)  ; Flag: Anycolor
InsertInteger(0x0 ,    StructForChooseColor, 24)  ; LPARAM lCustData
InsertInteger(0x0 ,    StructForChooseColor, 28)  ; LPCCHOOKPROC lpfnHook
InsertInteger(0x0 ,    StructForChooseColor, 32)  ; LPCTSTR lpTemplateName


Gui, Show 
return 

ButtonSelectFont: 
nRC := DllCall("comdlg32\ChooseColorA", str, StructForChooseColor)  ; Display the dialog.
if (errorlevel <> 0) || (nRC = 0)
{
	MsgBox error while calling ChooseColor Errorlevel: %errorlevel% - RC: %nRC%
	return
}

; Otherwise, the user pressed OK in the dialog, so determine what was selected. 
;GuiControl,, Color, % BGRtoRGB(ExtractInteger(StructForChooseColor, 12))
SetFormat, integer, hex  ; Show RGB color extracted below in hex format.
GuiControl,, Color, % BGRtoRGB(ExtractInteger(StructForChooseColor, 12)) 
SetFormat, integer, d 
return 

#h::reload

GuiClose: 
ExitApp 

ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4) 
; See DllCall documentation for details. 
{ 
   SourceAddress := &pSource + pOffset  ; Get address and apply the caller's offset. 
   result := 0  ; Init prior to accumulation in the loop. 
   Loop %pSize%  ; For each byte in the integer: 
   { 
      result := result | (*SourceAddress << 8 * (A_Index - 1))  ; Build the integer from its bytes. 
      SourceAddress += 1  ; Move on to the next byte. 
   } 
   if (!pIsSigned OR pSize > 4 OR result < 0x80000000) 
      return result  ; Signed vs. unsigned doesn't matter in these cases. 
   ; Otherwise, convert the value (now known to be 32-bit) to its signed counterpart: 
   return -(0xFFFFFFFF - result + 1) 
} 

InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4) 
; To preserve any existing contents in pDest, only pSize number of bytes starting at 
; pOffset are altered in it. The caller must ensure that pDest has sufficient capacity. 
{ 
   mask := 0xFF  ; This serves to isolate each byte, one by one. 
   Loop %pSize%  ; Copy each byte in the integer into the structure as raw binary data. 
   { 
      DllCall("RtlFillMemory", UInt, &pDest + pOffset + A_Index - 1, UInt, 1  ; Write one byte. 
         , UChar, (pInteger & mask) >> 8 * (A_Index - 1))  ; This line is auto-merged with above at load-time. 
      mask := mask << 8  ; Set it up for isolation of the next byte. 
   } 
} 

BGRtoRGB(oldValue)
{
  Value := (oldValue & 0x00ff00)
  Value += ((oldValue & 0xff0000) >> 16)
  Value += ((oldValue & 0x0000ff) << 16)  
  return Value
}
return

Ciao
Micha

AGermanUser
  • Members
  • 82 posts
  • Last active: Apr 07 2010 06:06 PM
  • Joined: 12 Feb 2005

Is this request related to your topic CMS for autohotkey.net ? Just curious!

Nope, sorry ;). It's for another release (6.1) of BBCodeWriter. I just found out, that phpbb supports hex values for colors. Therefore I want to integrate some color picker into the font color GUI that lets the user pick a custom color if he likes

I will be posting my improved version of ColorPicker in forum's Script section soon. This is for your information.

Thx. I'm looking forward to it. :D

I wish you all the success in bringing out an another fine utility like BBcode Writer.

Concerning CMS for autohotkey.net. As for my person this project is still in first stage. Gathering ideas. Next step would be to create a first version of the website template. I will look into that after release of version 6.1 of BBCodeWriter in case no one else is interested in this project. Have to fix a little displaying bug and integrate that color picker.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear AGU, :)

I have posted my AHK ColorPicker @ http://www.autohotke...p?p=57968#57968
The above post was edited/created using BBCodeWrtiter 6.1 . The system Color Picker has been nicely integrated.

Keep the good work going.

Thanks a lot.

Regards, :)
kWo4Lk1.png

ds
  • Guests
  • Last active:
  • Joined: --
Hello,

I use the script posted by Micha (see above) to integrate the standard
Windows ColorPicker in my GUI and it works fine for me.
The color output of this script is Hex RGB but I need a “R G B” output
(for example: 255 255 255 for color white). After long time of testing
and reading help files I gave up.
Could someone help me by getting a R G B output?!

Best regards

ds

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

The color output of this script is Hex RGB but I need a “R G B” output


MsgBox, % ChooseColor()  

ChooseColor( Color=0x0, hWnd=0x0, Flags=0x2 )  { ; CC_FULLOPEN := 0x2
 VarSetCapacity(CC,36+64,0), NumPut(36,CC), NumPut(hWnd,CC,4), NumPut(Color,CC,12)
 NumPut(&CC+36,CC,16), NumPut(Flags,CC,20), DllCall( "comdlg32\ChooseColorA", Str,CC ) 
 Return NumGet(CC,12,"UChar") "." NumGet(CC,13,"UChar") "." NumGet(CC,14,"UChar")    
}

:)

Edit: In case someone needs it in hex

MsgBox, % ChooseColorH()  

ChooseColorH( Color=0x0, hWnd=0x0, Flags=0x2 )  { ; CC_FULLOPEN := 0x2
 VarSetCapacity(CC,36+64,0), NumPut(36,CC), NumPut(hWnd,CC,4), NumPut(Color,CC,12)
 NumPut(&CC+36,CC,16), NumPut(Flags,CC,20), DllCall( "comdlg32\ChooseColorA", Str,CC ) 
 Hex:="123456789ABCDEF0",   RGB:=&CC+11 
 Loop 3  
  HexColorCode .=  SubStr(Hex, (*++RGB >> 4), 1) . SubStr(Hex, (*RGB & 15), 1) 
Return HexColorCode  
}

kWo4Lk1.png

ds
  • Guests
  • Last active:
  • Joined: --
SKAN - you are my hero!
you saved my day! Thanks a lot!!!

smikkelsen
  • Members
  • 126 posts
  • Last active: Jan 02 2014 06:38 AM
  • Joined: 24 Jun 2008
SKAN, thanks a lot for sharing your color function, it is very useful. Do you know of a way to change the window position when it opens? Obviously i could use something like winmove, but i would probably have to use a timer and stuff. Is there a good way to change any of the settings of the window that comes up?

Thanks again.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Do you know of a way to change the window position when it opens?


:arrow: If you specify a parent window, then the color dialog dialog would be shown @ coordinates 0,0 of the parent window.

Here is an example to show the dialog in the middle of user's screen.

Gui, 99:+LastFound
Gui, 99:Show, w453 h324 [color=red]Hide[/color]
hwnd := WinExist()
MsgBox, % ChooseColorH( 0x0, hwnd )

ChooseColorH( Color=0x0, hWnd=0x0, Flags=0x2 )  { ; CC_FULLOPEN := 0x2
 VarSetCapacity(CC,36+64,0), NumPut(36,CC), NumPut(hWnd,CC,4), NumPut(Color,CC,12)
 NumPut(&CC+36,CC,16), NumPut(Flags,CC,20), DllCall( "comdlg32\ChooseColorA", Str,CC )
 Hex:="123456789ABCDEF0",   RGB:=&CC+11
 Loop 3 
  HexColorCode .=  SubStr(Hex, (*++RGB >> 4), 1) . SubStr(Hex, (*RGB & 15), 1)
Return HexColorCode 
}

kWo4Lk1.png

daorc
  • Members
  • 177 posts
  • Last active:
  • Joined: 18 Oct 2006
That's a great function - thanks! Any idea how to store custom colours for future use using this?

Cheers.

daorc
  • Members
  • 177 posts
  • Last active:
  • Joined: 18 Oct 2006
Also, is there any way to tell the difference between someone selecting black and pressing cancel ...?

Thank you!

Elgin
  • Members
  • 79 posts
  • Last active: Dec 18 2015 08:07 PM
  • Joined: 29 Jul 2011

Also, is there any way to tell the difference between someone selecting black and pressing cancel ...?

That's a great function - thanks! Any idea how to store custom colours for future use using this?


As I just ran into the same problems, here's an adapted version that stores the custom colors and returns the exit code of the dialog:

; Example:
; Start with some random colors (optional)
Col:=0xFF0000
CColors:=Object()
Loop 16
  CColors.Insert(A_Index,A_Index*1036335)

If ChooseColor(Col,CColors)=1
{
  SetFormat, Integer, HEX
  b:=Col
  a:="Chosen: " b "`r`nCustom:`r`n"
  Loop 16
  a .= A_Index ": " CColors[A_Index] "`r`n"
  MsgBox, %a%
}
else Msgbox, Cancel!

; Function:

ChooseColor( ByRef Color, ByRef CustColors, hWnd=0x0, Flags=0x103 )
{
;  CC_FULLOPEN := 0x2  CC_RGBINIT := 0x1
  VarSetCapacity(CC,36+64,0)
  NumPut(36,CC)
  NumPut(hWnd,CC,4)
  NumPut(Color,CC,12)
  Loop 16
    NumPut(CustColors[A_Index],CC,32+A_Index*4)
  NumPut(&CC+36,CC,16)
  NumPut(Flags,CC,20)
  RVal:=DllCall( "comdlg32\ChooseColorW", Str,CC )
  Color:=NumGet(CC,12,"UInt")
  CustColors:=
  CustColors:=Object()
  Loop 16
  {
    CustColors.Insert(A_Index,Numget(CC,32+A_Index*4,"UInt"))
  }
  Return RVal ; 1 if OK; 0 if Cancel or Closed
}

Just in case anyone ever looks for this kind of thing again...