Better ColorRequester()

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Better ColorRequester()

Post by blueb »

The PB ColorRequester() only opens the basic color screen, and does not remember the users choices.

Hopefully this will be a bit better...

Once compiled, use it in your programs with RunProgram() :)

Code: Select all

;==================================================================
;
; Author:     blueb    
; Date:       May 8, 2017
; Explain:    Create a new ColorRequester that saves User Colors (between calls)
;          
; Saved as:  FullColorRequester.pb 
;=========================================================

EnableExplicit
; --------------------------------------------------------------------------
; Goal - write a 'Unique' preference file in the temporary directory that 
; stays in the temporary directory even when user switches to other programs.
; It will keep the special colors the user last adjusted.
; --------------------------------------------------------------------------
Declare InitPrefs()
Declare ExitPrefs()

Structure COLORREF
  RGB.l[16]
EndStructure 

; Structure CHOOSECOLOR ; see PB toolbox
;      lStructSize.l
;      hwndOwner.i
;      hInstance.i
;      rgbResult.l
;      *lpCustColors
;      Flags.l
;      lCustData.i
;      *lpfnHook
;      *lpTemplateName
; EndStructure

Define CHOOSECOLOR.CHOOSECOLOR

Define COLORREF.COLORREF
Global prefOpen.i, SelectedClipResult.s

; needed for preferences file.
Global.i  COLORREF1, COLORREF2, COLORREF3, COLORREF4, COLORREF5, COLORREF6, COLORREF7, COLORREF8, COLORREF9 
Global.i  COLORREF10, COLORREF11, COLORREF12, COLORREF13, COLORREF14, COLORREF15, COLORREF16

InitPrefs()

; place saved colors inside requester()
COLORREF\RGB[0] = COLORREF1
COLORREF\RGB[1] = COLORREF2
COLORREF\RGB[2] = COLORREF3
COLORREF\RGB[3] = COLORREF4
COLORREF\RGB[4] = COLORREF5
COLORREF\RGB[5] = COLORREF6
COLORREF\RGB[6] = COLORREF7
COLORREF\RGB[7] = COLORREF8
COLORREF\RGB[8] = COLORREF9
COLORREF\RGB[9] = COLORREF10
COLORREF\RGB[10] = COLORREF11
COLORREF\RGB[11] = COLORREF12
COLORREF\RGB[12] = COLORREF13
COLORREF\RGB[13] = COLORREF14
COLORREF\RGB[14] = COLORREF15
COLORREF\RGB[15] = COLORREF16

CHOOSECOLOR\LStructSize = SizeOf(CHOOSECOLOR)
CHOOSECOLOR\hwndOwner = 0;WindowID(#Window)
CHOOSECOLOR\rgbResult = 0 ; nothing selected
CHOOSECOLOR\lpCustColors = COLORREF
CHOOSECOLOR\flags = #CC_ANYCOLOR | #CC_FULLOPEN | #CC_RGBINIT

Procedure InitPrefs()
; ===================================
; Color Preference file - Storage information
;

prefOpen = OpenPreferences(GetTemporaryDirectory() + "GetUserColors.prefs") ; test and create if necessary

If prefOpen = 0 ; Does not exist... so create it.
     
   ; Default values - if "GetUserColors.prefs" is erased
 
          prefOpen = CreatePreferences(GetTemporaryDirectory() + "GetUserColors.prefs")
          
          WritePreferenceInteger("COLORREF1",  COLORREF1)  ; the first color
          WritePreferenceInteger("COLORREF2",  COLORREF2)  
          WritePreferenceInteger("COLORREF3",  COLORREF3)  
          WritePreferenceInteger("COLORREF4",  COLORREF4)  
          WritePreferenceInteger("COLORREF5",  COLORREF5)  
          WritePreferenceInteger("COLORREF6",  COLORREF6)  
          WritePreferenceInteger("COLORREF7",  COLORREF7)  
          WritePreferenceInteger("COLORREF8",  COLORREF8)  
          WritePreferenceInteger("COLORREF9",  COLORREF9)  
          WritePreferenceInteger("COLORREF10",  COLORREF10)  
          WritePreferenceInteger("COLORREF11",  COLORREF11)  
          WritePreferenceInteger("COLORREF12",  COLORREF12)  
          WritePreferenceInteger("COLORREF13",  COLORREF13)  
          WritePreferenceInteger("COLORREF14",  COLORREF14)  
          WritePreferenceInteger("COLORREF15",  COLORREF15)  
          WritePreferenceInteger("COLORREF16",  COLORREF16)  
    ClosePreferences()
   
Else ; it exists
          ; Retrieve the GetUserColors.prefs information
          COLORREF1 = ReadPreferenceInteger("COLORREF1", COLORREF1) 
          COLORREF2 = ReadPreferenceInteger("COLORREF2", COLORREF2) 
          COLORREF3 = ReadPreferenceInteger("COLORREF3", COLORREF3) 
          COLORREF4 = ReadPreferenceInteger("COLORREF4", COLORREF4) 
          COLORREF5 = ReadPreferenceInteger("COLORREF5", COLORREF5) 
          COLORREF6 = ReadPreferenceInteger("COLORREF6", COLORREF6) 
          COLORREF7 = ReadPreferenceInteger("COLORREF7", COLORREF7) 
          COLORREF8 = ReadPreferenceInteger("COLORREF8", COLORREF8) 
          COLORREF9 = ReadPreferenceInteger("COLORREF9", COLORREF9)     
          COLORREF10 = ReadPreferenceInteger("COLORREF10", COLORREF10)
          COLORREF11 = ReadPreferenceInteger("COLORREF11", COLORREF11)
          COLORREF12 = ReadPreferenceInteger("COLORREF12", COLORREF12)
          COLORREF13 = ReadPreferenceInteger("COLORREF13", COLORREF13)
          COLORREF14 = ReadPreferenceInteger("COLORREF14", COLORREF14)
          COLORREF15 = ReadPreferenceInteger("COLORREF15", COLORREF15)
          COLORREF16 = ReadPreferenceInteger("COLORREF16", COLORREF16)
  ClosePreferences()
   Debug COLORREF1 
EndIf
 EndProcedure
 
Procedure ExitPrefs()
       
     PrefOpen = OpenPreferences(GetTemporaryDirectory() + "GetUserColors.prefs")
    
  If prefOpen
          WritePreferenceInteger("COLORREF1",  COLORREF1)  ; the first color
          WritePreferenceInteger("COLORREF2",  COLORREF2)  
          WritePreferenceInteger("COLORREF3",  COLORREF3)  
          WritePreferenceInteger("COLORREF4",  COLORREF4)  
          WritePreferenceInteger("COLORREF5",  COLORREF5)  
          WritePreferenceInteger("COLORREF6",  COLORREF6)  
          WritePreferenceInteger("COLORREF7",  COLORREF7)  
          WritePreferenceInteger("COLORREF8",  COLORREF8)  
          WritePreferenceInteger("COLORREF9",  COLORREF9)  
          WritePreferenceInteger("COLORREF10",  COLORREF10)  
          WritePreferenceInteger("COLORREF11",  COLORREF11)  
          WritePreferenceInteger("COLORREF12",  COLORREF12)  
          WritePreferenceInteger("COLORREF13",  COLORREF13)  
          WritePreferenceInteger("COLORREF14",  COLORREF14)  
          WritePreferenceInteger("COLORREF15",  COLORREF15)  
          WritePreferenceInteger("COLORREF16",  COLORREF16)  
       
   EndIf
   If prefOpen
          ClosePreferences()
   EndIf     
EndProcedure

;==================================================================
;------- Place CHOOSECOLOR Info into requester()
;==================================================================

If ChooseColor_(@CHOOSECOLOR)
     COLORREF1 =COLORREF\RGB[0]
     COLORREF2 =COLORREF\RGB[1]
     COLORREF3 =COLORREF\RGB[2]
     COLORREF4 =COLORREF\RGB[3]
     COLORREF5 =COLORREF\RGB[4]
     COLORREF6 =COLORREF\RGB[5]
     COLORREF7 =COLORREF\RGB[6]
     COLORREF8 =COLORREF\RGB[7]
     COLORREF9 =COLORREF\RGB[8]
     COLORREF10 =COLORREF\RGB[9]
     COLORREF11 =COLORREF\RGB[10]
     COLORREF12 =COLORREF\RGB[11]
     COLORREF13 =COLORREF\RGB[12]
     COLORREF14 =COLORREF\RGB[13]
     COLORREF15 =COLORREF\RGB[14]
     COLORREF16 =COLORREF\RGB[15]
     
  SelectedClipResult.s = Str(CHOOSECOLOR\rgbResult)
  SetClipboardText(SelectedClipResult.s)
  
  Debug "Color Selected: " + CHOOSECOLOR\rgbResult
  
Else
  Debug "No color was selected."
EndIf

ExitPrefs() ; save info

End

- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Better ColorRequester()

Post by IdeasVacuum »

That is a thing of beauty! Thanks for sharing blueb.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Better ColorRequester()

Post by blueb »

IdeasVacuum wrote:That is a thing of beauty! Thanks for sharing blueb.
Glad it can be of use. :D

I got onto the idea when I was playing with SweetyVD and couldn't save colors.

I took a look to see what others had done and simply incorporated some of their ideas.

I think PureBasic should make something like this, instead of the regular requester.
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Better ColorRequester()

Post by RSBasic »

Thanks for sharing. Image
Image
Image
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Better ColorRequester()

Post by walbus »

Nice
Last edited by walbus on Thu May 11, 2017 12:40 pm, edited 2 times in total.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Better ColorRequester()

Post by ChrisR »

Very good and Useful, thanks for sharing :)

Without changing anything and to fully integrate it into SweetyVD, I changed the RunProgram() for a procedure

Code: Select all

;==================================================================
;
; Author:     blueb
; Date:       May 8, 2017
; Explain:    Create a new ColorRequester that saves User Colors (between calls)
; Forum:      http://www.purebasic.fr/english/viewtopic.php?f=12&t=68450
; Saved as:   FullColorRequester.pb
;=========================================================

EnableExplicit
; --------------------------------------------------------------------------
; Goal - write a 'Unique' preference file in the temporary directory that 
; stays in the temporary directory even when user switches to other programs.
; It will keep the special colors the user last adjusted.
; --------------------------------------------------------------------------
Declare InitColorPrefs()
Declare ExitColorPrefs()
Declare FullColorRequester()

Structure COLORREF
  RGB.l[16]
EndStructure 

; Structure CHOOSECOLOR ; see PB toolbox
;      lStructSize.l
;      hwndOwner.i
;      hInstance.i
;      rgbResult.l
;      *lpCustColors
;      Flags.l
;      lCustData.i
;      *lpfnHook
;      *lpTemplateName
; EndStructure

; Needed for preferences file.
Global.i  COLORREF1, COLORREF2, COLORREF3, COLORREF4, COLORREF5, COLORREF6, COLORREF7, COLORREF8, COLORREF9 
Global.i  COLORREF10, COLORREF11, COLORREF12, COLORREF13, COLORREF14, COLORREF15, COLORREF16

Procedure InitColorPrefs()   ;Color Preference file - Storage information
  If OpenPreferences(GetTemporaryDirectory() + "GetUserColors.prefs") = 0 ; Does not exist, "GetUserColors.prefs" is erased. So create it with Default values
    CreatePreferences(GetTemporaryDirectory() + "GetUserColors.prefs")
    WritePreferenceInteger("COLORREF1", COLORREF1)   ;The first color
    WritePreferenceInteger("COLORREF2", COLORREF2)
    WritePreferenceInteger("COLORREF3", COLORREF3)
    WritePreferenceInteger("COLORREF4", COLORREF4)
    WritePreferenceInteger("COLORREF5", COLORREF5)
    WritePreferenceInteger("COLORREF6", COLORREF6)
    WritePreferenceInteger("COLORREF7", COLORREF7)
    WritePreferenceInteger("COLORREF8", COLORREF8)
    WritePreferenceInteger("COLORREF9", COLORREF9)
    WritePreferenceInteger("COLORREF10", COLORREF10)
    WritePreferenceInteger("COLORREF11", COLORREF11)
    WritePreferenceInteger("COLORREF12", COLORREF12)
    WritePreferenceInteger("COLORREF13", COLORREF13)
    WritePreferenceInteger("COLORREF14", COLORREF14)
    WritePreferenceInteger("COLORREF15", COLORREF15)
    WritePreferenceInteger("COLORREF16", COLORREF16)
    ClosePreferences()
  Else   ;It exists, retrieve the GetUserColors.prefs information
    COLORREF1 = ReadPreferenceInteger("COLORREF1", COLORREF1)   ;The first user color
    COLORREF2 = ReadPreferenceInteger("COLORREF2", COLORREF2)
    COLORREF3 = ReadPreferenceInteger("COLORREF3", COLORREF3)
    COLORREF4 = ReadPreferenceInteger("COLORREF4", COLORREF4)
    COLORREF5 = ReadPreferenceInteger("COLORREF5", COLORREF5)
    COLORREF6 = ReadPreferenceInteger("COLORREF6", COLORREF6)
    COLORREF7 = ReadPreferenceInteger("COLORREF7", COLORREF7)
    COLORREF8 = ReadPreferenceInteger("COLORREF8", COLORREF8)
    COLORREF9 = ReadPreferenceInteger("COLORREF9", COLORREF9)
    COLORREF10 = ReadPreferenceInteger("COLORREF10", COLORREF10)
    COLORREF11 = ReadPreferenceInteger("COLORREF11", COLORREF11)
    COLORREF12 = ReadPreferenceInteger("COLORREF12", COLORREF12)
    COLORREF13 = ReadPreferenceInteger("COLORREF13", COLORREF13)
    COLORREF14 = ReadPreferenceInteger("COLORREF14", COLORREF14)
    COLORREF15 = ReadPreferenceInteger("COLORREF15", COLORREF15)
    COLORREF16 = ReadPreferenceInteger("COLORREF16", COLORREF16)
    ClosePreferences()
  EndIf
EndProcedure

Procedure ExitColorPrefs()
  If OpenPreferences(GetTemporaryDirectory() + "GetUserColors.prefs")
    WritePreferenceInteger("COLORREF1", COLORREF1)   ;The first color
    WritePreferenceInteger("COLORREF2", COLORREF2)
    WritePreferenceInteger("COLORREF3", COLORREF3)
    WritePreferenceInteger("COLORREF4", COLORREF4)
    WritePreferenceInteger("COLORREF5", COLORREF5)
    WritePreferenceInteger("COLORREF6", COLORREF6)
    WritePreferenceInteger("COLORREF7", COLORREF7)
    WritePreferenceInteger("COLORREF8", COLORREF8)
    WritePreferenceInteger("COLORREF9", COLORREF9)
    WritePreferenceInteger("COLORREF10", COLORREF10)
    WritePreferenceInteger("COLORREF11", COLORREF11)
    WritePreferenceInteger("COLORREF12", COLORREF12)
    WritePreferenceInteger("COLORREF13", COLORREF13)
    WritePreferenceInteger("COLORREF14", COLORREF14)
    WritePreferenceInteger("COLORREF15", COLORREF15)
    WritePreferenceInteger("COLORREF16", COLORREF16)
    ClosePreferences()
   EndIf
EndProcedure

Procedure FullColorRequester()   ;Place CHOOSECOLOR Info into requester()
  Protected CHOOSECOLOR.CHOOSECOLOR, COLORREF.COLORREF
  
  InitColorPrefs()
  ;Place saved colors inside requester()
  COLORREF\RGB[0] = COLORREF1
  COLORREF\RGB[1] = COLORREF2
  COLORREF\RGB[2] = COLORREF3
  COLORREF\RGB[3] = COLORREF4
  COLORREF\RGB[4] = COLORREF5
  COLORREF\RGB[5] = COLORREF6
  COLORREF\RGB[6] = COLORREF7
  COLORREF\RGB[7] = COLORREF8
  COLORREF\RGB[8] = COLORREF9
  COLORREF\RGB[9] = COLORREF10
  COLORREF\RGB[10] = COLORREF11
  COLORREF\RGB[11] = COLORREF12
  COLORREF\RGB[12] = COLORREF13
  COLORREF\RGB[13] = COLORREF14
  COLORREF\RGB[14] = COLORREF15
  COLORREF\RGB[15] = COLORREF16
  
  CHOOSECOLOR\LStructSize = SizeOf(CHOOSECOLOR)
  CHOOSECOLOR\hwndOwner = 0   ;WindowID(#Window)
  CHOOSECOLOR\rgbResult = 0   ;Nothing selected
  CHOOSECOLOR\lpCustColors = COLORREF
  CHOOSECOLOR\flags = #CC_ANYCOLOR | #CC_FULLOPEN | #CC_RGBINIT
  
  If ChooseColor_(@CHOOSECOLOR)
     COLORREF1 =COLORREF\RGB[0]
     COLORREF2 =COLORREF\RGB[1]
     COLORREF3 =COLORREF\RGB[2]
     COLORREF4 =COLORREF\RGB[3]
     COLORREF5 =COLORREF\RGB[4]
     COLORREF6 =COLORREF\RGB[5]
     COLORREF7 =COLORREF\RGB[6]
     COLORREF8 =COLORREF\RGB[7]
     COLORREF9 =COLORREF\RGB[8]
     COLORREF10 =COLORREF\RGB[9]
     COLORREF11 =COLORREF\RGB[10]
     COLORREF12 =COLORREF\RGB[11]
     COLORREF13 =COLORREF\RGB[12]
     COLORREF14 =COLORREF\RGB[13]
     COLORREF15 =COLORREF\RGB[14]
     COLORREF16 =COLORREF\RGB[15]
    ExitColorPrefs()   ;Save info and Return Color Selected
    ProcedureReturn CHOOSECOLOR\rgbResult
  Else   ;No color was selected
    ExitColorPrefs()   ;Save info
    ProcedureReturn -1
  EndIf
EndProcedure

;Debug "Color Selected: " + FullColorRequester()
And It is inserted in SweetyVD with: If it suits you

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  XIncludeFile "FullColorRequester.pb"
CompilerEndIf

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  SelectedColor = FullColorRequester()
CompilerElse
  SelectedColor = ColorRequester()
CompilerEndIf
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Better ColorRequester()

Post by blueb »

Great.
Thanks Chris. :)

The ability to easily control colors is an important part of a Visual Designer... and SweetyVD is turning out to be among the best.
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Better ColorRequester()

Post by kenmo »

Nice tip, I learned a few things about the Windows color chooser now.

But why would you use 16 global COLORREF variables, instead of a global array sized 16 ?!? :)
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Better ColorRequester()

Post by blueb »

kenmo wrote:Nice tip, I learned a few things about the Windows color chooser now.

But why would you use 16 global COLORREF variables, instead of a global array sized 16 ?!? :)
In hindsight... absolutely. At first I was only going to save one or two parameters, then got carried away with finishing it.

I did this in about an hour, and now I'm open to anyone helping with improvements. :mrgreen:
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Better ColorRequester()

Post by davido »

Very nice.
Thank you for sharing.
DE AA EB
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Better ColorRequester()

Post by ChrisR »

I added the ColorRef array + the choice for the color window position (without a hook).
Can be usefull to get it closer to a color selector button.

Code: Select all

;==================================================================
; Author:     blueb
; Date:       May 8, 2017
; Explain:    Create a new ColorRequester that saves User Colors (between calls)
; Forum:      http://www.purebasic.fr/english/viewtopic.php?f=12&t=68450
; Saved as:   FullColorRequester.pb
; Addition:   ChrisR: COLORREF array + choose color window position
; --------------------------------------------------------------------------
; ChooseColor function:  https://msdn.microsoft.com/en-us/library/windows/desktop/ms646912%28v=vs.85%29.aspx
; CHOOSECOLOR Structure: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646830%28v=vs.85%29.aspx
;=========================================================

EnableExplicit
; --------------------------------------------------------------------------
; Goal - write a 'Unique' preference file in the temporary directory that 
; stays in the temporary directory even when user switches to other programs.
; It will keep the special colors the user last adjusted.
; --------------------------------------------------------------------------
Declare InitColorPrefs()
Declare ExitColorPrefs()
Declare FullColorRequester(Color, X = 0, Y = 0)

Structure COLORREF
  RGB.l[16]
EndStructure 

; Structure CHOOSECOLOR ; see PB toolbox
;      lStructSize.l
;      hwndOwner.i
;      hInstance.i
;      rgbResult.l
;      *lpCustColors
;      Flags.l
;      lCustData.i
;      *lpfnHook
;      *lpTemplateName
; EndStructure

; Needed for preferences file.
Global.i Dim COLORPREF(15)

Procedure InitColorPrefs()   ;Color Preference file - Storage information
  Protected I.l
  If OpenPreferences(GetTemporaryDirectory() + "GetUserColors.prefs")   ;It exists, retrieve the GetUserColors.prefs information 
    For I = 0 To 15
      COLORPREF(I) = ReadPreferenceInteger("COLORREF"+Str(I+1), COLORPREF(I))
    Next
  Else   ; Does not exist, "GetUserColors.prefs" is erased. So create it with Default values
    CreatePreferences(GetTemporaryDirectory() + "GetUserColors.prefs")
    For I = 0 To 15
      WritePreferenceInteger("COLORREF"+Str(I+1), COLORPREF(I))
    Next
    ClosePreferences()
  EndIf
EndProcedure

Procedure ExitColorPrefs()
  Protected I.i
  If OpenPreferences(GetTemporaryDirectory() + "GetUserColors.prefs")
    For I = 0 To 15
      WritePreferenceInteger("COLORREF"+Str(I+1), COLORPREF(I))
    Next
    ClosePreferences()
  EndIf
EndProcedure

Procedure FullColorRequester(Color, X = 0, Y = 0)   ;Place CHOOSECOLOR Info into requester()
  Protected CHOOSECOLOR.CHOOSECOLOR, COLORREF.COLORREF, hwnd.i, I.i
  hwnd = OpenWindow(#PB_Any, X, Y, 0, 0, "", #PB_Window_Invisible)
  InitColorPrefs()
  ;Place saved colors inside requester()
  For I = 0 To 15
    COLORREF\RGB[I] = COLORPREF(I)
  Next
  
  CHOOSECOLOR\LStructSize = SizeOf(CHOOSECOLOR)
  If IsWindow(hwnd)   ;Window Owner
    CHOOSECOLOR\hwndOwner = WindowID(hwnd)
  Else   ;No Owner
    CHOOSECOLOR\hwndOwner = 0
  EndIf
  CHOOSECOLOR\rgbResult = Color   ;Nothing selected
  CHOOSECOLOR\lpCustColors = COLORREF
  ;CHOOSECOLOR\flags = #CC_ANYCOLOR | #CC_RGBINIT
  CHOOSECOLOR\flags = #CC_FULLOPEN |#CC_ANYCOLOR | #CC_RGBINIT   ;#CC_FULLOPEN to display the additional controls
  
  If ChooseColor_(@CHOOSECOLOR)
    For I = 0 To 15
      COLORPREF(I) = COLORREF\RGB[I]
    Next
    ExitColorPrefs()   ;Save info and Return Color Selected
    If IsWindow(hwnd)
      CloseWindow(hwnd)
    EndIf
    ProcedureReturn CHOOSECOLOR\rgbResult
  Else   ;No color was selected
    ExitColorPrefs()   ;Save info
    If IsWindow(hwnd)
      CloseWindow(hwnd)
    EndIf
    ProcedureReturn -1
  EndIf
EndProcedure

CompilerIf (#PB_Compiler_IsMainFile)
  Define SelectedColor = FullColorRequester($804000, 100, 20)
  If SelectedColor = -1
    Debug "Canceled by user"
  Else
    Debug "Color Selected: " + SelectedColor
    Debug "Hex: " + "$" + RSet(Hex(Blue(SelectedColor)), 2, "0") + RSet(Hex(Green(SelectedColor)), 2, "0") + RSet(Hex(Red(SelectedColor)), 2, "0")   ;Hex value in BGR format
    Debug "RGB(" + Str(Red(SelectedColor)) + ", " + Str(Green(SelectedColor)) + ", " + Str(Blue(SelectedColor)) + ")"
  EndIf
CompilerEndIf
Note: #CC_FULLOPEN to display the additional controls but once expanded, we can not go back to display it non-expanded.
Edit: Add a parameter for the selected initial color
Last edited by ChrisR on Tue Dec 06, 2022 12:23 pm, edited 5 times in total.
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Better ColorRequester()

Post by blueb »

Thanks Chris.. that's much cleaner! :)
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Better ColorRequester()

Post by IdeasVacuum »

Change RGB.l[16] to RGB.i[16] for x64

How can the requester window be initially displayed non-expanded?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Better ColorRequester()

Post by ChrisR »

RGB.i[16] is changed in the code above :)

Remove #CC_FULLOPEN flag to display it non-expanded
CHOOSECOLOR\flags = #CC_ANYCOLOR | #CC_FULLOPEN | #CC_RGBINIT

ChooseColor function
CHOOSECOLOR structure
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Better ColorRequester()

Post by Bisonte »

IdeasVacuum wrote:Change RGB.l[16] to RGB.i[16] for x64
Ähem... Why ?

The RGB Value is always Long. 32 Bit. Also on x64 machines...
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
Post Reply