Page 1 of 1

colorChanger : optimize Code ?

Posted: 31 Jul 2009 06:59
by dragovian
can anyone help me optimize this code, i don't know how to in dos, in other programming languages i'd know but not here :/

what i'm trying to achieve is that the foregroundChange and backgroundChange use the same function, instead of having 2 ( practically the same functions ).

is there a way to do so ?

Code: Select all

:colorChanger
echo.
echo to get a quick list of the available colors
echo type colorHelp
echo.
set input=
set /p input=Background Color:
if not defined input (echo.
echo color not found
echo.
goto a)

goto kleurenBackground

:kleurenBackground
if "%INPUT%"=="black" (set Background=0
goto kleurenForeground)
if "%INPUT%"=="blue" (set Background=1
goto kleurenForeground)
if "%INPUT%"=="green" (set Background=2
goto kleurenForeground)
if "%INPUT%"=="aqua" (set Background=3
goto kleurenForeground)
if "%INPUT%"=="red" (set Background=4
goto kleurenForeground)
if "%INPUT%"=="purple" (set Background=5
goto kleurenForeground)
if "%INPUT%"=="yellow" (set Background=6
goto kleurenForeground)
if "%INPUT%"=="white" (set Background=7
goto kleurenForeground)
if "%INPUT%"=="gray" (set Background=8
goto kleurenForeground)
if "%INPUT%"=="light blue" (set Background=9
goto kleurenForeground)
if "%INPUT%"=="light green" (set Background=A
goto kleurenForeground)
if "%INPUT%"=="light aqua" (set Background=B
goto kleurenForeground)
if "%INPUT%"=="light red" (set Background=C
goto kleurenForeground)
if "%INPUT%"=="light purple" (set Background=D
goto kleurenForeground)
if "%INPUT%"=="light yellow" (set Background=E
goto kleurenForeground)
if "%INPUT%"=="bright white" (set Background=F
goto kleurenForeground)
if "%INPUT%"=="colorHelp" (goto colorHelp)
if "%INPUT%"=="default" (color 0A
goto a)
echo.
echo Color Not Found
echo.
goto a

:kleurenForeground
set input=
set /p input=Foreground Color:
if not defined input (echo.
echo color not found
echo.
goto a)
if "%INPUT%"=="black" (set Foreground=0
goto kleurenSetter)
if "%INPUT%"=="blue" (set Foreground=1
goto kleurenSetter)
if "%INPUT%"=="green" (set Foreground=2
goto kleurenSetter)
if "%INPUT%"=="aqua" (set Foreground=3
goto kleurenSetter)
if "%INPUT%"=="red" (set Foreground=4
goto kleurenSetter)
if "%INPUT%"=="purple" (set Foreground=5
goto kleurenSetter)
if "%INPUT%"=="yellow" (set Foreground=6
goto kleurenSetter)
if "%INPUT%"=="white" (set Foreground=7
goto kleurenSetter)
if "%INPUT%"=="gray" (set Foreground=8
goto kleurenSetter)
if "%INPUT%"=="light blue" (set Foreground=9
goto kleurenSetter)
if "%INPUT%"=="light green" (set Foreground=A
goto kleurenSetter)
if "%INPUT%"=="light aqua" (set Foreground=B
goto kleurenSetter)
if "%INPUT%"=="light red" (set Foreground=C
goto kleurenSetter)
if "%INPUT%"=="light purple" (set Foreground=D
goto kleurenSetter)
if "%INPUT%"=="light yellow" (set Foreground=E
goto kleurenSetter)
if "%INPUT%"=="bright white" (set Foreground=F
goto kleurenSetter)
if "%INPUT%"=="colorHelp" (goto colorHelp)
if "%INPUT%"=="default" (color 0A
goto a)
echo.
echo Color Not Found
echo.
goto a

:kleurenSetter
color %Background%%Foreground%
echo.
echo. do you wish to keep these changes ?
echo.
echo 1 - yes
echo 2 - no
echo.
choice /c:12
if errorlevel 2 (color 0A
goto a)
if errorlevel 1 goto a


:colorHelp
echo -----------------------------------------
echo ----------- Available Colors ------------
echo -----------------------------------------
echo.
echo ^>^>^>^>^> reminder : don't use cases
echo.
echo - black
echo - blue
echo - green
echo - aqua
echo - red
echo - purple
echo - yellow
echo - white
echo - gray
echo - light blue
echo - light green
echo - light aqua
echo - light red
echo - light purple
echo - light yellow
echo - bright white
echo - default
echo -
echo -----------------------------------------
timeout -1
goto colorChanger

Posted: 31 Jul 2009 11:27
by avery_larry
Something like this:

*UNTESTED*

Code: Select all

:colorChanger 
echo.
echo to get a quick list of the available colors
echo type colorHelp
echo.
set input=
for %%a in (Background Foreground) do (
   set /p input=%%a Color:
   if not defined input (
      echo.
      echo color not found
      echo.
      goto a
   )
   call :setcolor %%a
)
color %Background%%Foreground%
echo.
echo. do you wish to keep these changes ?
echo.
echo 1 - yes
echo 2 - no
echo.
choice /c:12
if errorlevel 2 (color 0A
goto a)
if errorlevel 1 goto a

goto :eof

:setcolor
if "%INPUT%"=="black" set %1=0&&goto :eof
if "%INPUT%"=="blue" set %1=1&&goto :eof
if "%INPUT%"=="green" set %1=2&&goto :eof

.
.
.

Posted: 31 Jul 2009 12:17
by dragovian
yup that works ^^

but question

the "goto :eof"

is that sorta like the "End If" in vb.net, or the "break/continue" command in select case in java ?

like "end of for/end of file" ?

god i really wish someone would write a decent tutorial on for loops in cmd :/

Posted: 07 Aug 2009 09:19
by avery_larry
dragovian wrote:yup that works ^^

but question

the "goto :eof"

is that sorta like the "End If" in vb.net, or the "break/continue" command in select case in java ?

like "end of for/end of file" ?
It specifically stands for "End Of File". It effectively exits the batch script. It's useful because you can call subroutines and use the goto :eof to simply end the subroutine. Calling a label is the same as recursively calling the current batch file with a "goto :label" as the effective first line of the batch file.
god i really wish someone would write a decent tutorial on for loops in cmd :/
http://ss64.com/nt/for.html