udfAskColor
str udfAskColor (str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)
intFormatIn = 0
intError = 1
Switch @TRUE
Case StrSub (strAC_DefaultColor, 1, 1) == ""  ; Empty string opens dialog with status "128|128|128" and returns hex formatted value.
Case StrSub (strAC_DefaultColor, 1, 1) == "#" ; e. g. "#FF8040", further error checking in AskColor function.
   intError = 0
   Break
Case @TRUE
   strDelim = StrClean (strAC_DefaultColor, "0123456789", "", @TRUE, 1)
   strDelim1 = StrSub (strDelim, 1, 1)
   Continue
Case StrClean (strDelim, strDelim1, "", @TRUE, 1) != ""
   Break
   Continue
Case StrClean (strDelim1, ",|", "", @TRUE, 1) != ""
   Break
   Continue
Case ItemCount (strAC_DefaultColor, strDelim1) != 3
   Break
   Continue
Case ItemCount (strAC_DefaultColor, ",") == 3 ; e. g. "255,255,64"
   intFormatIn = 2
   Continue
Case ItemCount (strAC_DefaultColor, "|") == 3 ; e. g. "255|255|64"
   intFormatIn = 1
   Continue
Case @TRUE
   strAC_DefaultColor = StrReplace (strAC_DefaultColor, ",", "|") ; e. g. from "255,255,64" to "255|255|64"
   strAny = StrReplace (strAC_DefaultColor, "|", "")              ; e. g. "25525564"
   Continue
Case StrLen (strAny) < 3     ; e. g. "000"        is ok.
   Break                     ; e. g. "00"         is error.
Case StrLen (strAny) > 9     ; e. g. "000000000"  is ok.
   Break                     ; e. g. "0000000000" is error.
Case !IsNumber (strAny)
   Break                     ; Not a number is error.
Case @TRUE
   intNumber = Int (strAny)  ; Make integer for sure.
   Continue
Case intNumber < 0
   Break                     ; Less than minimum is error.
Case intNumber > 255255255
   Break                     ; Greater than maximum is error.
Case @TRUE                   ; Defaultcolor RGB string list is accepted.
   hdlBB = BinaryAlloc (3)   ; Split RGB string list and convert to three bytes.
   BinaryPoke (hdlBB, 0, ItemExtract (1, strAC_DefaultColor, "|")) ; R
   BinaryPoke (hdlBB, 1, ItemExtract (2, strAC_DefaultColor, "|")) ; G
   BinaryPoke (hdlBB, 2, ItemExtract (3, strAC_DefaultColor, "|")) ; B
   strAC_DefaultColor = "#" : BinaryPeekHex (hdlBB, 0, 3); Format hex string for use with AskColor.
   hdlBB = BinaryFree (hdlBB)
   intError = 0
   Break
EndSwitch

If intError == 1 Then ErrorEvent (-1, 7694, "AskColor: Invalid default color when using RGB format.") ; Throw minor error.

Switch @TRUE
Case intAC_Format == 3
   intAC_Format = intFormatIn
   Continue
Case intAC_Format == 2
   strColor = AskColor (strAC_DefaultColor, strAC_Reg, 1)
   strColor = StrReplace (strColor, "|", ",")
   Break
Case @TRUE
   strColor = AskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)
   Break
EndSwitch
Return strColor

:CANCEL
Return strAC_DefaultColor
;..........................................................................................................................................
; This UDF extends the WinBatch function "AskColor (default-color, reg-key, format)" to accept also a
; RGB value as the default color value. The RGB value can be formatted as a pipe delimited string list "R|G|B"
; or a comma delimited string list "R,G,B".
;
; In addition the format parameter accepts two new integer values for modifying the output format:
; - Value 2 forces the function to return a RGB comma delimited string list "R,G,B".
; - Value 3 let the function return the choosen color value in the same format as the default color value
; format was given when invoked.
;
; Note: The original function AskColor allows to specify the name of a user-defined registry key by the
; second parameter. This functionality can be used unchanged by this UDF.
;
; Example:
; Using second parameter strAC_Reg as "MyUserDefinedColors" then there will be created a registry key
; named as "[HKEY_CURRENT_USER\Software\Wilson WindowWare\MyUserDefinedColors\AskColor]".
; This registry entry holds a binary value named "CustomColors" which is a table of 16 elements of 4-byte values,
; each value assigned to a color field  in the dialog area "User Defined Colors" within the AskColor dialog.
;..........................................................................................................................................
; Syntax
;
; udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)
;
; strAC_DefaultColor
;    String of format "#RRGGBB" or "R|G|B" or "R,G,B".
;
; strAC_Reg
;    String, name of a registry sub key under "HKEY_CURRENT_USER\Software\Wilson WindowWare" where persistent
;    state information will be stored. The sub key will be created if it does not already exist.
;    If an empty string is specified, then persistent state information will not be stored.
;    The sub key stores the table of 16 user defined colors from the AskColor dialog.
;
; intAC_Format
;    0: Return RGB color value as hex string with format "#RRGGBB",
;       where "R", "G", and "B" are the respective red, green, and blue components, in hexadecimal values.
;    1: Return RGB color value as pipe delimited list with format "R|G|B",
;       where "R", "G", and "B" are the respective red, green, and blue components, in decimal values.
;    2: Return RGB color value as comma delimited list with format "R,G,B",
;       where "R", "G", and "B" are the respective red, green, and blue components, in decimal values.
;    3: Return same format as default color format.
;..........................................................................................................................................
; Detlev Dalitz.20090418.20110126.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test-1
strAC_Reg = ""
intAC_Format = 0

strAC_DefaultColor = ""            ; No error, opens dialog with value "128|128|128", returns hex formatted value when intAC_Format != 1.
;strColorValue11 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)

strAC_DefaultColor = "255|128"     ; Throws minor error 7694: AskColor: Invalid default color when using RGB format.
;strColorValue12 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)

strAC_DefaultColor = "255|255|256" ; Throws minor error 7694: AskColor: Invalid default color when using RGB format.
;strColorValue13 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)

strAC_DefaultColor = "0|-1|0"      ; Throws minor error 7694: AskColor: Invalid default color when using RGB format.
;strColorValue14 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)

strAC_DefaultColor = "0||0"        ; Throws minor error 7694: AskColor: Invalid default color when using RGB format.
;strColorValue15 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)

strAC_DefaultColor = "-1|0|0"      ; Throws minor error 7694: AskColor: Invalid default color when using RGB format.
;strColorValue16 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)

strAC_DefaultColor = "21:22:23" ; Throws minor error 7694: AskColor: Invalid default color when using RGB format.
;strColorValue17 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format)


; Test-2
strAC_DefaultColor = "255|128|64"

intAC_Format = 0
strColorValue21 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "#FF8040"

intAC_Format = 1
strColorValue22 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255|128|64"

intAC_Format = 2
strColorValue23 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255,128,64"

intAC_Format = 3
strColorValue24 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255|128|64"


; Test-3
strAC_DefaultColor = "255,128,64"

intAC_Format = 0
strColorValue31 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "#FF8040"

intAC_Format = 1
strColorValue32 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255|128|64"

intAC_Format = 2
strColorValue33 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255,128,64"

intAC_Format = 3
strColorValue34 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255,128,64"


; Test-4
strAC_DefaultColor = "#FF8040"

intAC_Format = 0
strColorValue41 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "#FF8040"

intAC_Format = 1
strColorValue42 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255|128|64"

intAC_Format = 2
strColorValue43 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "255,128,64"

intAC_Format = 3
strColorValue44 = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; "#FF8040"


; Test-5
strAC_Reg = "MyUserDefinedColors" ; Registry key to hold my user defined colors.
intAC_Format = 3 ; The return value should be the same as default color format.

strAC_DefaultColor = "255,128,64"
strColorValue = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; Returned value has format "R,G,B".

strAC_DefaultColor = strColorValue
strColorValue = udfAskColor (strAC_DefaultColor, strAC_Reg, intAC_Format) ; Returned value has format "R,G,B".

Exit