Jump to content

Son wants to start learning Autoit. Look over this script please.


Recommended Posts

Let me start off by saying if this is the wrong section I'm sorry please move and thanks.

Like the title says my son wants to start learning Autoit and asked me to write a script that can create color palettes for a program he uses and would take that script and go over it as a learning point. He wants to then write his own script that will use functions from this current script and present it to me. I am not the best in Autoit but can do some stuff and have come up with a script for him. I tried to explain the script a little as I coded it for his understanding. 

He learns very quickly and always surprises me with how fast he catches on to any new project he takes on.

So my question is will you check over this code and offer up any improvements that I may implement? Open to any ideas that will improve the script because I want to be able to give him the cleanest, most functional script I can for his learning benefit. 

Thanks in advance for any help provided and again, I am sorry if I posted this in the wrong place I have never posted on this forum before.

 

#Region ; Created by
#cs  ###################################################
    #   Palette Builder with Random Color Generator     #
    #       Created by Pr0t0c0l Effects Design          #
    #           First Release 11/01/2022                #
    #                 Version 1.0                       #
    #   includes function by Yashied on line 407
#ce  ###################################################
#EndRegion ; Coder
#Region ; Run before initial calls to startup Global Variables, Includes, Hotkeysets
#include <MsgBoxConstants.au3>
AutoItSetOption("WinTitleMatchMode", 2)
HotKeySet("{ESC}", "TERM")
HotKeySet("{PAUSE}", "TogglePause")
Global $g_bPaused = False
Global $cLine = 1
Global $cCount = 1
Global $Cn = 1
Global $Cn1 = 1
Global $Swatch = 1
Global $1st = 000
Global $2nd = 000
Global $3rd = 000
Global $1r
Global $2g
Global $3b
Global $cnum = 000
Global $cval = 000
Global $Column = 16
Global $rValue2
Global $gValue2
Global $bValue2
Global $rValue3
Global $gValue3
Global $bValue3
Global $rValue4
Global $gValue4
Global $bValue4
Global $Min
Global $Max
Global $Variance
Global $Mean
Global $Times
Global $R2
Global $G2
Global $B2
Global $Gen1
Global $Gen2
Global $Gen3
Global $Loop = 0
Global $Cnt = 0
Global $Editor = InputBox("  Text Editor Name", "  Open the Text editor program you want to use and then enter the name of the editor." & @CRLF & "  Example: Notepad, Notepad++ Word and so on. Case sensitive", "", "", 525, 150)
Global $eName = $Editor
Global $wHndl = WinGetHandle($Editor, "") ; gets handle
Global $pName = InputBox("  Palette Name", "  This creates a Pallete with 16 columns, rows depends on amount of colors and gradients of each color." & @CRLF & "  What do you want to call this Palette?", "", "", 575, 150)
Global $Who = InputBox("  Created by?", "  Made by who?" & @CRLF & "  Example: This palette is Made By YOUR NAME.", "", "", 325, 150)
Global $Choose = InputBox(" Define or Generator?", "  Do you want to define your colors or use the random generator?" & @CRLF & "   Enter 1 to define or 2 for Generator.", "", "", 400, 150)
If $Choose > 2 Or $Choose <= 0 Then
    While $Choose > 2 Or $Choose <= 0
        MsgBox($MB_SYSTEMMODAL, "Error", " Incorrect amount entered please enter a number between 1 and 10 for the amount")
        $Choose = InputBox("  Number of Colors", "  How many colors to you want to create? You may enter up to 10 Colors. Valid Entery 1 - 10", "1", "", 250, 150)
    WEnd
EndIf
Select
    Case $Choose = 2
        $Tm = InputBox(" # of Colors ", "  How many colors do you want to randomly create?", "", "", 400, 150)
        $Div = $Tm / 2
        $Times = Round($Div)
        Call("Startup2")
    Case $Choose = 1
EndSelect
Global $Amount = InputBox("  Number of Colors", "  How many colors to you want to create? You may enter up to 10 Colors. Valid Entery 1 - 10", "1", "", 250, 150)
If $Amount > 10 Or $Amount <= 0 Then
    While $Amount > 10 Or $Amount <= 0
        MsgBox($MB_SYSTEMMODAL, "Error", " Incorrect amount entered please enter a number between 1 and 10 for the amount")
        $Amount = InputBox("  Number of Colors", "  How many colors to you want to create? You may enter up to 10 Colors. Valid Entery 1 - 10", "1", "", 250, 150)
    WEnd
EndIf
If $Amount > 1 Then
    Call("More_Colors")
Else
    Global $cName1 = InputBox("  Setting Color Params", "  Name of first color? ", "", "", 150, 150)
    Global $rValue1 = InputBox("  Setting Color Params", "  Example increments for Red from black to full Red you would set first " & @CRLF & "  value increment to 010 this will create swatches in increments of 10 till" & _
            @CRLF & "  it reaches 255." & @CRLF & "  Example Swatch values entered would look like 010 000 000, 020 000 000 and so on." & @CRLF & "  You can set increments for each field as need for the color you want to use." & @CRLF & _
            "  All Base Colors Start with RGB 000 000 000 and will increase" & @CRLF & "  to max of 255 once increment amount is set per field." & @CRLF & "  Set first field Red increment amount. If none leave unchanged and click OK ", "000", "", 525, 225)
    Global $gValue1 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
    Global $bValue1 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
    Call("Startup")
EndIf
#EndRegion ; Run before initial startup Global Variables, Includes, Hotkeysets
Func Main()                                         ; start of Main() function
    While 1
        WinActivate($wHndl, "")     ; activates editor
        If $Loop = $Amount Then     ; compares and decides when to end color enteries
            Exit
        EndIf
        Select                          ; sets current color variable value if more than one color is defined
            Case $Cnt = 0
                $1r = $rValue1
                $2g = $gValue1
                $3b = $bValue1
                $cName = $cName1
            Case $Cnt = 1
                $1r = $rValue2
                $2g = $gValue2
                $3b = $bValue2
                $cName = $cName2
            Case $Cnt = 2
                $1r = $rValue3
                $2g = $gValue3
                $3b = $bValue3
                $cName = $cName3
            Case $Cnt = 3
                $1r = $rValue4
                $2g = $gValue4
                $3b = $bValue4
                $cName = $cName4
            Case $Cnt = 4
                $1r = $rValue5
                $2g = $gValue5
                $3b = $bValue5
                $cName = $cName5
            Case $Cnt = 5
                $1r = $rValue6
                $2g = $gValue6
                $3b = $bValue6
                $cName = $cName6
            Case $Cnt = 6
                $1r = $rValue7
                $2g = $gValue7
                $3b = $bValue7
                $cName = $cName7
            Case $Cnt = 7
                $1r = $rValue8
                $2g = $gValue8
                $3b = $bValue8
                $cName = $cName8
            Case $Cnt = 8
                $1r = $rValue9
                $2g = $gValue9
                $3b = $bValue9
                $cName = $cName9
            Case $Cnt = 9
                $1r = $rValue10
                $2g = $gValue10
                $3b = $bValue10
                $cName = $cName10
        EndSelect
        If $1r > 000 Then               ; sets variable used for stopping point of current color
            $cnum = $1st
            $cval = $1r
        ElseIf $2g > 000 Then           ; sets variable used for stopping point of current color
            $cnum = $2nd
            $cval = $2g
        ElseIf $3b > 000 Then           ; sets variable used for stopping point of current color
            $cnum = $3rd
            $cval = $3b
        Else                            ; sets variable used for stopping point of current color
        EndIf
        While 1                         ; loop for entering current color data
            $lMath = $cLine / $Column   ; math use for setting tooltip message
            $line = Round($lMath, 1)    ; math use for setting tooltip message
            Select          ; changes tooltip message based off value of $cCount number
                Case $cCount = 17
                    $cCount = 1 ; resets count for tooltip message display
                    $Swatch = 1 ; resets swatch number for ending data entery of current color
                Case $cCount <= 5
                    ToolTip("Building Dimensions " & @CRLF & "Current Palette Row is: " & $line & @CRLF & "Current Swatch in is " & $Swatch, 0, 0, "BUILDING PALETTE .")
                Case $cCount >= 5 And $cCount <= 10
                    ToolTip("Building Dimensions " & @CRLF & "Current Palette Row is: " & $line & @CRLF & "Current Swatch in is " & $Swatch, 0, 0, "BUILDING PALETTE . .")
                Case $cCount >= 10 And $cCount <= 15
                    ToolTip("Building Dimensions " & @CRLF & "Current Palette Row is: " & $line & @CRLF & "Current Swatch in is " & $Swatch, 0, 0, "BUILDING PALETTE . . .")
            EndSelect
            $cLine += 1     ; increments for tooltip message display and current swatch number that helps close out data entry of current color
            $cCount += 1    ; increments for tooltip message display and current swatch number that helps close out data entry of current color
            $Swatch += 1    ; increments for tooltip message display and current swatch number that helps close out data entry of current color
            Sleep(50)       ; very brief pause
            If $cnum >= 255 And $Swatch >= 17 Then  ; compares  and decides when to end current color data entery
                Sleep(100)
                ControlSend($wHndl, "", "", "0" & "{SPACE}" & "0" & "{SPACE}" & "0" & "{TAB}" & "{TAB}" & "Black - " & $Cn1)    ; enters last black swatch in currenr palette row
                Sleep(100)
                Send("{ENTER}")                     ; moves to next line
                Sleep(50)
                $cnum = 0                           ; resets variables for next color entery
                $cval = 0
                $1st = 000
                $2nd = 000
                $3rd = 000
                $Cn = 1                             ; end of reset
                $Cn1 += 1                           ; increment for current black swatch number
                $Cnt += 1                           ; increment for changing variables for next color entery
                $Loop += 1                          ; increment for loop till it equals amount - amount is number of colors that were defined
                ExitLoop                            ; exits loop to start next color if defined
            ElseIf $cnum >= 255 Then                ; compares and decides when to switch swatches to black to finish current palette row
                Sleep(150)
                ControlSend($wHndl, "", "", "0" & "{SPACE}" & "0" & "{SPACE}" & "0" & "{TAB}" & "{TAB}" & "Black - " & $Cn1) ; continues entering black in swatches till above compare is true
                Sleep(100)
                Send("{ENTER}")                     ; moves to next line
                Sleep(150)
                $Cn1 += 1                           ; increment for current black swatch number
            Else                                    ; continues data entery of current color till one of the two compares above become true
                $1st += $1r
                $2nd += $2g
                $3rd += $3b
                $cnum += $cval
                Sleep(150)
                ControlSend($wHndl, "", "", $1st & "{SPACE}" & $2nd & "{SPACE}" & $3rd & "{TAB}" & "{TAB}" & $cName & " - " & $Cn)
                Sleep(100)
                Send("{ENTER}")
                $Cn += 1
                $Cn1 += 1
            EndIf
        WEnd                                        ; ending of loop 1
    WEnd                                            ; ending of loop 2
EndFunc                                             ; ending of Main() function
Func More_Colors()                                  ; start of More_Colors function called if more than one color is defined
    Global $cName1 = InputBox("  Setting Color Params", "  Name of first color? ", "", "", 150, 150)         ; starts section of setting extra color variables based of number of colors
    Global $rValue1 = InputBox("  Setting Color Params", "  Example increments for Red from black to full Red you would set first " & @CRLF & "  value increment to 010 this will create swatches in increments of 10 till" & _
            @CRLF & "  it reaches 255." & @CRLF & "  Example Swatch values entered would look like 010 000 000, 020 000 000 and so on." & @CRLF & "  You can set increments for each field as need for the color you want to use." & @CRLF & _
            "  All Base Colors Start with RGB 000 000 000 and will increase" & @CRLF & "  to max of 255 once increment amount is set per field." & @CRLF & "  Set first field Red increment amount. If none leave unchanged and click OK ", "000", "", 525, 225)
    Global $gValue1 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
    Global $bValue1 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
    For $Cnt2 = 1 To $Amount            ; runs a loop to set vairiables of each color till it equals the amount that was entered
        Select
            Case $Cnt2 = 1
                $cName2 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue2 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue2 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue2 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 2
                $cName3 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue3 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue3 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue3 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 3
                $cName4 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue4 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue4 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue4 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 4
                $cName5 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue5 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue5 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue5 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 5
                $cName6 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue6 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue6 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue6 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 6
                $cName7 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue7 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue7 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue7 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 7
                $cName8 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue8 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue8 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue8 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 8
                $cName9 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue9 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue9 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue9 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
            Case $Cnt2 = 9
                $cName10 = InputBox("  Setting Color Params", "  Name of next color? ", "", "", 150, 150)
                $rValue10 = InputBox("  Setting Color Params", "  Set First field Red increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $gValue10 = InputBox("  Setting Color Params", "  Set Second field Green increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
                $bValue10 = InputBox("  Setting Color Params", "  Set Third field Blue increment amount. If none leave unchanged and click OK ", "000", "", 450, 150)
        EndSelect
    Next
    Call("Startup")                 ; calls Startup() function once the above For loop is completed
EndFunc                                             ; ending of  More_Colors function
Func Startup()                                      ; start of Startup() function used to verify editor is running already and enters palette name and so on then calls Main() function
    WinActivate($wHndl, "")                         ; activates notepad++
    $Proc = WinExists($wHndl, "")                   ; checks editor is running
    If $Proc = 0 Then                               ; if editor is not running will loop till editor name is correct and editor is running
        While $Proc = 0
            MsgBox($MB_SYSTEMMODAL, "Error", "The Editor you entered is not open or you typed the name incorrect." & @CRLF & " Please open the editor and enter the name again")
            $Editor = InputBox("  Text Editor Name", "  Open the Text editor program you want to use and then enter the name of the editor." & @CRLF & "  Example: Notepad, Notepad++ Word and so on. Case sensitive", "", "", 525, 150)
            $eName = $Editor
            $wHndl = WinGetHandle($Editor, "")      ; gets handle of entered editor
            $Proc = WinExists($wHndl, "")           ; checks editor is running
        WEnd
    Else
    EndIf                                           ; end of compare and loop
    WinActivate($wHndl, "")                         ; activates editor
    WinWaitActive($wHndl, "")                       ; waits for editor to be focus
    ControlSend($wHndl, $eName, "", "GIMP Palette "); enters palette information
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "Name: " & $pName)
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "Columns: " & $Column)
    Sleep(2)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} " & $Who)
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(250)
    Send("{ENTER}")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    Call("Main")
EndFunc                                             ; ending of Startup() function
Func Startup2() ; start of Startup2() function same as above function but ran if random color generator is chosen instead of definning colors to call Generator()
    WinActivate($wHndl, "")
    $Proc = WinExists($wHndl, "")
    If $Proc = 0 Then
        While $Proc = 0
            MsgBox($MB_SYSTEMMODAL, "Error", "The Editor you entered is not open or you typed the name incorrect." & @CRLF & " Please open the editor and enter the name again")
            $Editor = InputBox("  Text Editor Name", "  Open the Text editor program you want to use and then enter the name of the editor." & @CRLF & "  Example: Notepad, Notepad++ Word and so on. Case sensitive", "", "", 525, 150)
            $eName = $Editor
            $wHndl = WinGetHandle($Editor, "") ; gets handle
            $Proc = WinExists($wHndl, "")
        WEnd
    Else
    EndIf
    WinActivate($wHndl, "")        ; activates notepad++
    WinWaitActive($wHndl, "")   ; waits for notepad++ to be focus
    ControlSend($wHndl, $eName, "", "GIMP Palette ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "Name: " & $pName)
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "Columns: " & $Column)
    Sleep(2)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} " & $Who)
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    ControlSend($wHndl, $eName, "", "{#} ")
    Sleep(250)
    Send("{ENTER}")
    Sleep(250)
    Send("{ENTER}")
    Sleep(250)
    Send("{ENTER}")
    Sleep(150)
    Call("Generator")
EndFunc   ;==>Startup2
Func Generator()                                    ; start of random color entery
    WinActivate($wHndl, "")   ; activates editor
    WinWaitActive($wHndl, "") ; waits for editor to be focus
    For $i = 1 To $Times      ; starts For loop till $i equals number of colors that are wanted to be generated
        $Gen1 = _GRandom(0, 256, 125, 100)  ; runs _GRandom() function to generate random number
        $R2 = Round($Gen1)                  ; sets value of variable used to enter random number value by rounding the number to nearest whole number trimming off decimal values
        $Gen2 = _GRandom(0, 256, 125, 100)
        $G2 = Round($Gen2)
        $Gen3 = _GRandom(0, 256, 125, 100)
        $B2 = Round($Gen3)                  ; ends section
        Sleep(100)
        ControlSend($wHndl, "", "", $R2 & "{SPACE}" & $G2 & "{SPACE}" & $B2 & "{TAB}" & "{TAB}" & "Random Color - " & $Cn) ; enters data
        Sleep(100)
        Send("{ENTER}")
        $R2 = $B2    ; switched variable values to enter data second time without having to generate numbers again
        $G2 = $R2
        $B2 = $G2
        Sleep(100)
        ControlSend($wHndl, "", "", $R2 & "{SPACE}" & $G2 & "{SPACE}" & $B2 & "{TAB}" & "{TAB}" & "Random Color - " & $Cn)
        Sleep(100)
        Send("{ENTER}")
        $R2 = ""        ; resets variable values
        $G2 = ""
        $B2 = ""
    Next                ; ends for loop
    Exit                ; exits script
EndFunc                                                     ; ending of Generator() function
Func _GRandom($Min = 0, $Max = 1, $Variance = Default, $Mean = Default) ; Function Written by Yashied _GRandom() function used to generate random number
    Local $D, $X1, $X2, $Result
    If $Min > $Max Then
        Return SetError(1, 0, 0)
    EndIf
    If $Min = $Max Then
        Return $Min
    EndIf
    If $Mean = Default Then
        $Mean = ($Min + $Max) / 2
    EndIf
    If $Variance = Default Then
        $Variance = ($Max - $Min) / 2
    EndIf
    If ($Mean < $Min) Or ($Mean > $Max) Then
        Return SetError(1, 0, 0)
    EndIf
    If $Variance <= 0 Then
        Return SetError(1, 0, 0)
    EndIf
    Do
        Do
            $X1 = Random(-1, 1)
            $X2 = Random(-1, 1)
            $D = $X1 ^ 2 + $X2 ^ 2
        Until $D <= 1
        $Result = $Mean + $X1 * $Variance * Sqrt(-2 * Log($D) / $D)
    Until ($Result >= $Min) And (($Result <= $Max))
    Return $Result
EndFunc   ;==>_GRandom
Func TERM()  ; called by hot key
    ToolTip("")
    Exit
EndFunc   ;==>TERM
Func TogglePause()  ; called by hot key
    $g_bPaused = Not $g_bPaused
    While $g_bPaused
        Sleep(100)
        ToolTip($Choose, 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>TogglePause
   #Region ; Function and Variables Reference
#cs
     == User functions =================================================================================================
Function name             Row     Referenced at Row(s)
========================= ====== ==================================================================================
Generator                 00377  00375
Main                      00088  00322
More_Colors               00213  00078
Startup                   00271  00086 00269
Startup2                  00324  00067
TERM                      00434
TogglePause               00438
_GRandom                  00404  00381 00383 00385
== Variables ======================================================================================================
Variable name             Dim   Used in Row(s)
========================= ===== ===================================================================================
$1r                       ----- 00022 00096 00101 00106 00111 00116 00121 00126 00131 00136 00141 00146 00148 00199
$1st                      ----- 00019 00147 00183 00199 00204
$2g                       ----- 00023 00097 00102 00107 00112 00117 00122 00127 00132 00137 00142 00149 00151 00200
$2nd                      ----- 00020 00150 00184 00200 00204
$3b                       ----- 00024 00098 00103 00108 00113 00118 00123 00128 00133 00138 00143 00152 00154 00201
$3rd                      ----- 00021 00153 00185 00201 00204
$Amount                   ----- 00070 00071 00072 00074 00077 00091 00220
$B2                       ----- 00044 00386 00388 00391 00393 00395 00400
$Choose                   ----- 00055 00056 00057 00059 00063 00068 00442
$Cn                       ----- 00016 00186 00204 00207 00388 00395
$Cn1                      ----- 00017 00177 00187 00193 00197 00208
$Cnt                      ----- 00049 00095 00100 00105 00110 00115 00120 00125 00130 00135 00140 00188
$Cnt2                     ----- 00220 00222 00227 00232 00237 00242 00247 00252 00257 00262
$Column                   ----- 00027 00158 00294 00347
$D                        ----- 00405 00428 00429 00430
$Div                      ----- 00065 00066
$Editor                   ----- 00050 00051 00052 00277 00278 00279 00330 00331 00332
$G2                       ----- 00043 00384 00388 00392 00393 00395 00399
$Gen1                     ----- 00045 00381 00382
$Gen2                     ----- 00046 00383 00384
$Gen3                     ----- 00047 00385 00386
$Loop                     ----- 00048 00091 00189
$MB_SYSTEMMODAL           ----- 00058 00073 00276 00329
$Max                      ----- 00038 00404 00406 00409 00413 00416 00418 00431
$Mean                     ----- 00040 00404 00412 00413 00418 00430
$Min                      ----- 00037 00404 00406 00409 00410 00413 00416 00418 00431
$Proc                     ----- 00273 00274 00275 00280 00326 00327 00328 00333
$R2                       ----- 00042 00382 00388 00391 00392 00395 00398
$Result                   ----- 00405 00430 00431 00432
$Swatch                   ----- 00018 00163 00165 00167 00169 00173 00175
$Times                    ----- 00041 00066 00380
$Tm                       ----- 00064 00065
$Variance                 ----- 00039 00404 00415 00416 00421 00430
$Who                      ----- 00054 00306 00359
$X1                       ----- 00405 00426 00428 00430
$X2                       ----- 00405 00427 00428
$bValue1                  ----- 00085 00098 00219
$bValue10                 ----- 00143 00266
$bValue2                  ----- 00030 00103 00226
$bValue3                  ----- 00033 00108 00231
$bValue4                  ----- 00036 00113 00236
$bValue5                  ----- 00118 00241
$bValue6                  ----- 00123 00246
$bValue7                  ----- 00128 00251
$bValue8                  ----- 00133 00256
$bValue9                  ----- 00138 00261
$cCount                   ----- 00015 00161 00162 00164 00166 00168 00172
$cLine                    ----- 00014 00158 00171
$cName                    ----- 00099 00104 00109 00114 00119 00124 00129 00134 00139 00144 00204
$cName1                   ----- 00080 00099 00214
$cName10                  ----- 00144 00263
$cName2                   ----- 00104 00223
$cName3                   ----- 00109 00228
$cName4                   ----- 00114 00233
$cName5                   ----- 00119 00238
$cName6                   ----- 00124 00243
$cName7                   ----- 00129 00248
$cName8                   ----- 00134 00253
$cName9                   ----- 00139 00258
$cnum                     ----- 00025 00147 00150 00153 00175 00181 00191 00202
$cval                     ----- 00026 00148 00151 00154 00182 00202
$eName                    ----- 00051 00278 00286 00290 00294 00298 00302 00306 00310 00314 00331 00339 00343 00347 00351 00355 00359 00363 00367
$gValue1                  ----- 00084 00097 00218
$gValue10                 ----- 00142 00265
$gValue2                  ----- 00029 00102 00225
$gValue3                  ----- 00032 00107 00230
$gValue4                  ----- 00035 00112 00235
$gValue5                  ----- 00117 00240
$gValue6                  ----- 00122 00245
$gValue7                  ----- 00127 00250
$gValue8                  ----- 00132 00255
$gValue9                  ----- 00137 00260
$g_bPaused                ----- 00013 00439 00440
$i                        ----- 00380
$lMath                    ----- 00158 00159
$line                     ----- 00159 00165 00167 00169
$pName                    ----- 00053 00290 00343
$rValue1                  ----- 00081 00096 00215
$rValue10                 ----- 00141 00264
$rValue2                  ----- 00028 00101 00224
$rValue3                  ----- 00031 00106 00229
$rValue4                  ----- 00034 00111 00234
$rValue5                  ----- 00116 00239
$rValue6                  ----- 00121 00244
$rValue7                  ----- 00126 00249
$rValue8                  ----- 00131 00254
$rValue9                  ----- 00136 00259
$wHndl                    ----- 00052 00090 00177 00193 00204 00272 00273 00279 00280 00284 00285 00286 00290 00294
                                00298 00302 00306 00310 00314 00325 00326 00332 00333 00337 00338 00339 00343 00347
                                00351 00355 00359 00363 00367 00378 00379 00388 00395
@CRLF                     ----- 00050 00053 00054 00055 00081 00165 00167 00169 00215 00276 00277 00329 00330
#ce
   #EndRegion

 

Pallet builder plus Random color generator.au3

Link to comment
Share on other sites

Here my take on your script :

1- Make it more modular (into functions)

2- Remove all unnecessary global variables (within functions define them as Local).  It is not a best practice to use lots of Global variables.

3- Create a GUI instead of asking multiple InputBox (that will make it more user friendly and you can see all inputs required).  You can go back easily if you make an error while your script you need to restart.

4- Instead of using Select/Case (with 0, 1, 2...) use array assignment.

By the way, I did not run your script.

FWIW, before starting programming, your kid should read some books (or take some courses) on how to create good algorithms, how to modularize a program, how to use advance programming techniques (like recursion).  Before writing a book, you should master grammar and syntax. :)

Link to comment
Share on other sites

I made two scripts some time ago. They are way more basic.

 

- The first one, it's just a function. It generates a color based on the 'Perceived brightness'. For example, If you need a random dark color, call the function with a 0-50 value, if you need a random bright color call the function with a 50-100 value. If you need a total random color, call the function with a random number from 0-100, if you need a palette, call the function in a loop.

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#AutoIt3Wrapper_Run_Tidy=y

; ---------------------------------------------------------------------------------------
ConsoleWrite("hex color: #" & gen_color(50) & @CRLF)


; ---------------------------------------------------------------------------------------
; generate color
; ---------------------------------------------------------------------------------------
Func gen_color($iTarget)
    Local $sHexColor = "", $fBright = 0.0
    Local $fTarget = $iTarget / 100
    Local $iR = 0, $iG = 0, $iB = 0

    Do
        $iR = Random(0, 1)
        $iG = Random(0, 1)
        $iB = Random(0, 1)

        $fBright = (Sqrt(0.299 * ($iR ^ 2) + 0.587 * ($iG ^ 2) + 0.114 * ($iB ^ 2)))
    Until $fBright < $fTarget + 0.10 And $fBright > $fTarget - 0.10

    ConsoleWrite("Target Perceived brightness: " & $iTarget & "%     ± 0.10" & @CRLF)
    ConsoleWrite("Actual Perceived brightness: " & Floor($fBright * 100) & "%" & @CRLF)

    $sHexColor = _
            Hex(Int(Round($iR * 255, 0)), 2) & _
            Hex(Int(Round($iG * 255, 0)), 2) & _
            Hex(Int(Round($iB * 255, 0)), 2)

    Return $sHexColor

EndFunc   ;==>gen_color

 

 

- The second one It's the same but it does have a GUI whith a slider to adjust the color.

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#AutoIt3Wrapper_Run_Tidy=y
#include <Array.au3>


; variables -----------------------------------------------------------------------------
Const $WS_OVERLAPPEDWINDOW = 0x00CF0000
Const $GUI_EVENT_CLOSE = -3
Const $SS_CENTER = 0x00000001
Const $GUI_FOCUS = 0x00000100
Const $WS_EX_TOPMOST = 0x00000008

Local $iR = 0, $iG = 0, $iB = 0, $bInvert = False, $fLuminosity = 0.0
Local $sHexColor = ""

; window --------------------------------------------------------------------------------
Local $iGUIWidth = 300, $iGUIHeight = 0
Local $hGUI = GUICreate(@ScriptName, _
        $iGUIWidth, $iGUIHeight, -1, -1, $WS_OVERLAPPEDWINDOW, $WS_EX_TOPMOST)
GUISetFont(12.5, 500, 0, "Segoe UI", $hGUI, 5)
;GUISetBkColor(0xFF00FF, $hGUI)

; controls ------------------------------------------------------------------------------
Local $idSampleBox = 0, $idSampleText = 0, $idColor = 0, $idInfo = 0, $idSlide = 0
Local $idRand = 0

sample_box(50)
sample_text(160)
color_inp(30)
info_inp(380)
$iGUIHeight += 15
slider(40)
rand(30)



; adjustments ---------------------------------------------------------------------------
WinMove($hGUI, "", Default, 1, Default, $iGUIHeight + 38)
GUISetState()


; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; main loop
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit 0
        Case $idRand
            gen_color()

        Case $idSlide
            gen_color()

        Case $idSampleBox, $idSampleText
            If $bInvert Then
                $bInvert = False
            Else
                $bInvert = True
            EndIf
            invert()

    EndSwitch
WEnd


; ---------------------------------------------------------------------------------------
; sample box
; ---------------------------------------------------------------------------------------
Func sample_box($iHeight)

    $idSampleBox = GUICtrlCreateLabel("Header Text", _
            1, $iGUIHeight + 1, $iGUIWidth - 2, $iHeight - 2, $SS_CENTER)
    GUICtrlSetBkColor($idSampleBox, "0xFFFFFF")
    GUICtrlSetColor($idSampleBox, "0x000000")
    GUICtrlSetFont($idSampleBox, 28, 500, Default, "Impact", 5)

    $iGUIHeight += $iHeight
EndFunc   ;==>sample_box


; ---------------------------------------------------------------------------------------
; sample text label
; ---------------------------------------------------------------------------------------
Func sample_text($iHeight)

    $idSampleText = GUICtrlCreateLabel( _
            "Waltz, bad nymph, for quick jigs vex. " & _
            "Glib jocks quiz nymph to vex dwarf. " & _
            "Sphinx of black quartz, judge my vow. " & _
            "How quickly daft jumping zebras vex! " & _
            "The five boxing wizards jump quickly. " & _
            "Jackdaws love my big sphinx of quartz. " & _
            "Pack my box with five dozen liquor jugs. ", _
            1, $iGUIHeight + 1, $iGUIWidth - 2, $iHeight - 2)
    GUICtrlSetFont($idSampleText, 12.5, 400, Default, "Verdana", 5)

    GUICtrlSetBkColor($idSampleText, "0xFFFFFF")

    $iGUIHeight += $iHeight
EndFunc   ;==>sample_text


; ---------------------------------------------------------------------------------------
; color input
; ---------------------------------------------------------------------------------------
Func color_inp($iHeight)

    $idColor = GUICtrlCreateInput("", _
            1, $iGUIHeight + 1, $iGUIWidth - 2, $iHeight - 2, $SS_CENTER)

    $iGUIHeight += $iHeight
EndFunc   ;==>color_inp

; ---------------------------------------------------------------------------------------
; info input
; ---------------------------------------------------------------------------------------
Func info_inp($iHeight)

    $idInfo = GUICtrlCreateEdit("", _
            1, $iGUIHeight + 1, $iGUIWidth - 2, $iHeight - 2)
    GUICtrlSetFont($idInfo, 10, 400, Default, "Consolas", 5)

    $iGUIHeight += $iHeight
EndFunc   ;==>info_inp


; ---------------------------------------------------------------------------------------
; slider
; ---------------------------------------------------------------------------------------
Func slider($iHeight)
    $idSlide = GUICtrlCreateSlider(1, $iGUIHeight + 1, $iGUIWidth - 2, $iHeight - 2)
    GUICtrlSetData($idSlide, 35)
    gen_color()

    $iGUIHeight += $iHeight
EndFunc   ;==>slider


; ---------------------------------------------------------------------------------------
; random light control
; ---------------------------------------------------------------------------------------
Func rand($iHeight)

    $idRand = GUICtrlCreateButton(" Random Color ", _
            1, $iGUIHeight + 1, $iGUIWidth - 2, $iHeight - 2)

    $iGUIHeight += $iHeight
EndFunc   ;==>rand


; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
; generate color
;
; HSP Color Model — Alternative to HSV (HSB) and HSL:
; https://alienryderflex.com/hsp.html
; sqrt( 0.299*R^2 + 0.587*G^2 + 0.114*B^2 )
;
; Method 2, Relative luminance:
; https://en.wikipedia.org/wiki/Relative_luminance
; (0.2126*R + 0.7152*G + 0.0722*B)
;
; Method 3, Color visibility:
; https://www.w3.org/TR/AERT/#color-contrast
; (0.299*R + 0.587*G + 0.114*B)
; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Func gen_color()
    Local $fTarget = GUICtrlRead($idSlide) / 100



    Do
        $iR = Random(0, 1)
        $iG = Random(0, 1)
        $iB = Random(0, 1)

        $fLuminosity = (Sqrt(0.299 * ($iR ^ 2) + 0.587 * ($iG ^ 2) + 0.114 * ($iB ^ 2)))
    Until $fLuminosity < $fTarget + 0.10 And $fLuminosity > $fTarget - 0.10

    ;-------------------
    Local $aRGB[3] = [$iR, $iG, $iB]
    Local $iMax = 0, $iMin = 0, $iHue = 0, $iLum = 0, $iSat = 0

    $iMax = _ArrayMaxIndex($aRGB)
    $iMin = _ArrayMinIndex($aRGB)

    ; hue -
    Local $iDivisor = ($aRGB[$iMax] - $aRGB[$iMin])

    If $iMax = 0 Then $iHue = 60 * (($iG - $iB) / $iDivisor)
    If $iMax = 1 Then $iHue = 60 * (2.0 + ($aRGB[2] - $aRGB[0]) / $iDivisor)
    If $iMax = 2 Then $iHue = 60 * (4.0 + ($aRGB[0] - $aRGB[1]) / $iDivisor)

    $iHue = Floor($iHue)
    If $iHue < 0 Then $iHue += 360
    If $aRGB[$iMax] = 0 Then $iHue = 0

    ; lum -
    $iLum = ($aRGB[$iMax] + $aRGB[$iMin]) / 2
    $iLum = Floor($iLum * 100)

    ; sat -
    If $iLum <= 0.5 Then _
            $iSat = ($aRGB[$iMax] - $aRGB[$iMin]) / ($aRGB[$iMax] + $aRGB[$iMin])
    If $iLum > 0.5 Then _
            $iSat = ($aRGB[$iMax] - $aRGB[$iMin]) / (2 - $aRGB[$iMax] + $aRGB[$iMin])

    If $aRGB[$iMax] = $aRGB[$iMin] Then $iSat = 0
    $iSat = Floor($iSat * 100)
    ;-------------------



    $iR = Int(Round($iR * 255, 0))
    $iG = Int(Round($iG * 255, 0))
    $iB = Int(Round($iB * 255, 0))

    $sHexColor = Hex($iR, 2) & Hex($iG, 2) & Hex($iB, 2)


    GUICtrlSetBkColor($idSampleBox, "0x" & $sHexColor)
    GUICtrlSetColor($idSampleText, "0x" & $sHexColor)
    GUICtrlSetData($idColor, "#" & $sHexColor)

    GUICtrlSetData($idInfo, _
            "Target luminosity: " & $fTarget & "  ± 0.10" & @CRLF & @CRLF & _
            "Current Luminosity: " & Round($fLuminosity, 2) & @CRLF & @CRLF & _
            "scale 0-1: " & @CRLF & _
            "R: " & Round($aRGB[0], 3) & @CRLF & _
            "G: " & Round($aRGB[1], 3) & @CRLF & _
            "B: " & Round($aRGB[2], 3) & @CRLF & @CRLF & _
            "R: " & Round($iR, 2) & @CRLF & _
            "G: " & Round($iG, 2) & @CRLF & _
            "B: " & Round($iB, 2) & @CRLF & @CRLF & _
            "Hex: 0x" & $sHexColor & @CRLF & @CRLF & _
            "-------------------------" & @CRLF & _
            "HSL Method:" & @CRLF & @CRLF & _
            "Hue: " & $iHue & @CRLF & _
            "Sat: " & $iSat & @CRLF & _
            "Lum: " & $iLum & @CRLF & _
            "-------------------------")
    GUICtrlSetState($idColor, $GUI_FOCUS)

    If $fLuminosity < 0.5 Then
        GUICtrlSetColor($idSampleBox, "0xFFFFFF")
        GUICtrlSetBkColor($idSampleText, "0xFFFFFF")
    Else
        GUICtrlSetColor($idSampleBox, "0x000000")
        GUICtrlSetBkColor($idSampleText, "0x000000")
    EndIf
    $bInvert = False

EndFunc   ;==>gen_color


; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
; invert
; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Func invert()

    If $bInvert Then
        GUICtrlSetColor($idSampleBox, "0x" & $sHexColor)
        GUICtrlSetBkColor($idSampleText, "0x" & $sHexColor)

        If $fLuminosity < 0.5 Then
            GUICtrlSetBkColor($idSampleBox, "0xFFFFFF")
            GUICtrlSetColor($idSampleText, "0xFFFFFF")
        Else
            GUICtrlSetBkColor($idSampleBox, "0x000000")
            GUICtrlSetColor($idSampleText, "0x000000")
        EndIf
    Else
        GUICtrlSetBkColor($idSampleBox, "0x" & $sHexColor)
        GUICtrlSetColor($idSampleText, "0x" & $sHexColor)

        If $fLuminosity < 0.5 Then
            GUICtrlSetColor($idSampleBox, "0xFFFFFF")
            GUICtrlSetBkColor($idSampleText, "0xFFFFFF")
        Else
            GUICtrlSetColor($idSampleBox, "0x000000")
            GUICtrlSetBkColor($idSampleText, "0x000000")
        EndIf
    EndIf

EndFunc   ;==>invert

 

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...