Convert R G B values into single 0 - 255 color value?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
IcarusLives
Posts: 117
Joined: 17 Jan 2016 23:55

Re: Convert R G B values into single 0 - 255 color value?

#16 Post by IcarusLives » 16 Apr 2018 10:25

jeb wrote:
16 Apr 2018 09:46
-snip-
Your code is beautiful!!! It works so much better than mine! Excellent work, Jeb! Thank you for sharing!

I replied to your other post as well!

viewtopic.php?p=56404#p56404

penpen
Expert
Posts: 1742
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Convert R G B values into single 0 - 255 color value?

#17 Post by penpen » 16 Apr 2018 15:11

Here is an example of how i meant to use color lookup tables (tested on Windows 10):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
call :init

:: set up colortables
set "colorTable1="0;0;0","0;255;0","255;255;255","128;196;0","196;196;196""
set "colorTable2="0;0;0","255;0;0","255;255;255","255;140;64","196;196;196""
set "colorTable3="0;0;0","0;0;255","255;255;255","0;128;196","196;196;196""

set "loadColors(table)=set "i=0" & for %%a in (!table!) do (set "c!i!=%%~a" & set /a "i+=1")"

setlocal enableDelayedExpansion
mode con cols=100 lines=52
color 17
cls

:: render sprite with different colortables
%loadColors(table):table=colorTable1%
<nul set /p "=%setCursor(x;y):x;y=5;9%%sprite%"

%loadColors(table):table=colorTable2%
<nul set /p "=%setCursor(x;y):x;y=15;29%%sprite%"

%loadColors(table):table=colorTable3%
<nul set /p "=%setCursor(x;y):x;y=25;49%%sprite%"

<nul set /p "=%setCursor(x;y):x;y=50;1%"
%statement%

endlocal

pause
goto :eof


:init
::setup escape
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"

::setup escape codes
set "saveCursor()=%ESC%[s"
set "unsaveCursor()=%ESC%[u"
set "restoreCursor()=%ESC%[8"
set "setCursor(x;y)=%ESC%[x;yf"
set "up(x)=%ESC%[xA"
set "down(x)=%ESC%[xB"
set "left(x)=%ESC%[xD"
set "right(x)=%ESC%[xC"
set "setColor(r;g;b)=%ESC%[38;2;r;g;bm"
set "resetAttributes()=%ESC%[0m"


::setup artwork
:: artwork also contains an image mask with " " := "not painted"
:: artwork pixels := " XG.@-"
set /a "artwork.lastRow=21, artwork.lastColumns=43"
::                         1         2         3         4
::               01234567890123456789012345678901234567890123
set  "artwork[0]=                  XXXXXXXX                  "
set  "artwork[1]=              XXXXGGGGGGGGXXXX              "
set  "artwork[2]=          XXXXGGGGGGGGGGGGGGGGXXXX          "
set  "artwork[3]=        XXGGXXGGGGGGGGGG....GGXXGGXX        "
set  "artwork[4]=      XXGGGGGGXXGGGGGGGGGGGGXXGGGGGGXX      "
set  "artwork[5]=      XXGGGGGG@@XXXXXXXXXXXXGGGGGGGGXX      "
set  "artwork[6]=    XXGGGGGGGGXX@@@@@@@@GGGGXXGGGGGGGGXX    "
set  "artwork[7]=    XXGGGGGGXX@@@@@@@@@@GGGGGGXXGGGGGGXX    "
set  "artwork[8]=    XXGGGGXX@@@@@@@@@@@@GGGGGG@@XXGGGGXX    "
set  "artwork[9]=  XXGGGGXX@@GG@@@@@@@@@@GGGGGGGG@@XXGGGGXX  "
set "artwork[10]=  XXGGXXGGXXGGGG@@@@@@GGGGGGGGGGXXGGXXGGXX  "
set "artwork[11]=  XXXX@@@@GGXXGGGGGGGGGGGGGGGGXXGG@@@@XXXX  "
set "artwork[12]=XXXXGGGGGGGGGGXXGGGGGGGGGGGGXXGGGGGGGGGGXXXX"
set "artwork[13]=XX..XXGGGGGGGGGGXXXXXXXXXXXX@@GGGGGGGGXX..XX"
set "artwork[14]=XX..XXGGGGGGGGXXGGGGGGGGGGGGXXGGGGGGGGXX..XX"
set "artwork[15]=XX--..XXXXGGXXGGGGGGGGGGGGGGGGXXGGXXXX..--XX"
set "artwork[16]=  XX......XXXXGGGGGGGGGGGGGGGGXXXX......XX  "
set "artwork[17]=  XX--........XXXXXXXXXXXXXXXX........--XX  "
set "artwork[18]=    XX----........................----XX    "
set "artwork[19]=      XXXX----................----XXXX      "
set "artwork[20]=          XXXX----------------XXXX          "
set "artwork[21]=              XXXXXXXXXXXXXXXX              "


:: compute sprite out of artwork
setlocal enableDelayedExpansion
rem set "ESC=^!ESC^!"
set "sprite=%saveCursor()%"
set /A "space=0"
set "lastChar= "
for /l %%r in (0, 1, %artwork.lastRow%) do (
	for /l %%c in (0, 1, %artwork.lastColumns%) do (
		for %%a in ("!artwork[%%~r]:~%%~c,1!") do (
			if "!lastChar!" == "%%~a" (
				if "%%~a" == " " ( set /a "space+=1"
				) else ( set "sprite=!sprite!Û"
				)
			) else (
				if "!lastChar!" == " " (
					if       !space! LSS 0 ( set "sprite=!sprite!%left(x):x=!space:~1!%"
					) else if !space GTR 0 ( set "sprite=!sprite!%right(x):x=!space!%"
					)
				) else (
					if "%%~a" == " " (
						set "space=1"
						if defined lastChar set "sprite=!sprite!%resetAttributes()%"
					) else (
						set "sprite=!sprite!%resetAttributes()%"
					)
				)

				if        "%%~a" == "X" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c0^!%Û"
				) else if "%%~a" == "G" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c1^!%Û"
				) else if "%%~a" == "." ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c2^!%Û"
				) else if "%%~a" == "@" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c3^!%Û"
				) else if "%%~a" == "-" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c4^!%Û"
				)
			)
			set "lastChar=%%~a"
		)
	)
	if not "!lastChar!" == " " (
		set "sprite=!sprite!%resetAttributes()%"
		set "lastChar= "
		set "space=0"
	)
	if not "%%~r" == "%artwork.lastRow%" (
		set "sprite=!sprite!%down(x):x=1%"
	)
	set /a "space-=(artwork.lastColumns+1)"
)
endlocal & set "sprite=%sprite%%restoreCursor()%%unsaveCursor()%"

goto :eof
penpen

IcarusLives
Posts: 117
Joined: 17 Jan 2016 23:55

Re: Convert R G B values into single 0 - 255 color value?

#18 Post by IcarusLives » 16 Apr 2018 22:31

Oh my... What a concept! I feel embarrassed I hadn't though of this. Thank you so much for this Penpen!

I also find it amazing that you ALSO managed to solve my OTHER issue viewtopic.php?p=56360#p56360

I will read this code thoroughly so I can learn from it an implement a solution into my game.

Jeb has also done something incredible!

I am so glad I joined this forum. I thought for a long time I had hit a plateau, but you all continue to inspire and teach me so much more.

Thank you for your time!

IcarusLives
Posts: 117
Joined: 17 Jan 2016 23:55

Re: Convert R G B values into single 0 - 255 color value?

#19 Post by IcarusLives » 18 Apr 2018 20:44

Penpen, I've been playing with your code, and to be honest this is really fun!

Image

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
call :init

:: set up colortables
set "colorTable1="0;0;0","0;255;0","255;0;0","128;196;0","255;222;173","139;69;19""
set "colorTable2="0;0;0","255;0;0","0;0;255","255;140;64","255;222;173","139;69;19""
set "colorTable3="0;0;0","0;0;255","0;255;0","0;128;196","255;222;173","139;69;19""

set "loadColors(table)=set "i=0" & for %%a in (!table!) do (set "c!i!=%%~a" & set /a "i+=1")"

setlocal enableDelayedExpansion
mode con cols=100 lines=52
color 17
cls
timeout /t 5

:: render sprite with different colortables
set x=19
for /l %%b in (1,1,300) do (
	
	set /a "a+=1", "b=a %% 5"
	if !b! equ 0 set /a "x+=1"
	%loadColors(table):table=colorTable1%
	set "screen=!screen!!ESC![5;19H%sprite%"
	
	%loadColors(table):table=colorTable2%
	set "screen=!screen!!ESC![15;!x!H%sprite%"
	
	<nul set /p "=!esc![2J!screen!" & <nul set /p "=!screen!" & set "screen="
	
	%loadColors(table):table=colorTable3%
	<nul set /p "=%ESC%[25;49f%sprite%"
)

<nul set /p "=%setCursor(x;y):x;y=50;1%"

endlocal

pause
goto :eof


:init
::setup escape
for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"

::setup escape codes
set "saveCursor()=%ESC%[s"
set "unsaveCursor()=%ESC%[u"
set "restoreCursor()=%ESC%[8"
set "setCursor(x;y)=%ESC%[x;yf"
set "up(x)=%ESC%[xA"
set "down(x)=%ESC%[xB"
set "left(x)=%ESC%[xD"
set "right(x)=%ESC%[xC"
set "setColor(r;g;b)=%ESC%[38;2;r;g;bm"
set "resetAttributes()=%ESC%[0m"


::setup artwork
:: artwork also contains an image mask with " " := "not painted"
:: artwork pixels := " XG.@-"
set /a "artwork.lastRow=16, artwork.lastColumns=11"
::                         1         2         3         4
REM ::               01234567890123456789012345678901234567890123
REM set  "artwork[0]=                  XXXXXXXX                  "
REM set  "artwork[1]=              XXXXGGGGGGGGXXXX              "
REM set  "artwork[2]=          XXXXGGGGGGGGGGGGGGGGXXXX          "
REM set  "artwork[3]=        XXGGXXGGGGGGGGGG....GGXXGGXX        "
REM set  "artwork[4]=      XXGGGGGGXXGGGGGGGGGGGGXXGGGGGGXX      "
REM set  "artwork[5]=      XXGGGGGG@@XXXXXXXXXXXXGGGGGGGGXX      "
REM set  "artwork[6]=    XXGGGGGGGGXX@@@@@@@@GGGGXXGGGGGGGGXX    "
REM set  "artwork[7]=    XXGGGGGGXX@@@@@@@@@@GGGGGGXXGGGGGGXX    "
REM set  "artwork[8]=    XXGGGGXX@@@@@@@@@@@@GGGGGG@@XXGGGGXX    "
REM set  "artwork[9]=  XXGGGGXX@@GG@@@@@@@@@@GGGGGGGG@@XXGGGGXX  "
REM set "artwork[10]=  XXGGXXGGXXGGGG@@@@@@GGGGGGGGGGXXGGXXGGXX  "
REM set "artwork[11]=  XXXX@@@@GGXXGGGGGGGGGGGGGGGGXXGG@@@@XXXX  "
REM set "artwork[12]=XXXXGGGGGGGGGGXXGGGGGGGGGGGGXXGGGGGGGGGGXXXX"
REM set "artwork[13]=XX..XXGGGGGGGGGGXXXXXXXXXXXX@@GGGGGGGGXX..XX"
REM set "artwork[14]=XX..XXGGGGGGGGXXGGGGGGGGGGGGXXGGGGGGGGXX..XX"
REM set "artwork[15]=XX--..XXXXGGXXGGGGGGGGGGGGGGGGXXGGXXXX..--XX"
REM set "artwork[16]=  XX......XXXXGGGGGGGGGGGGGGGGXXXX......XX  "
REM set "artwork[17]=  XX--........XXXXXXXXXXXXXXXX........--XX  "
REM set "artwork[18]=    XX----........................----XX    "
REM set "artwork[19]=      XXXX----................----XXXX      "
REM set "artwork[20]=          XXXX----------------XXXX          "
REM set "artwork[21]=              XXXXXXXXXXXXXXXX              "

set  "artwork[1]=   GGGGG    "
set  "artwork[2]=  GGGGGGGGG "
set  "artwork[3]=  EEE--X-   "
set  "artwork[4]= E-E---X--  "
set  "artwork[5]= E-EE---X---"
set  "artwork[6]= EE----XXXX "
set  "artwork[7]=   -------  "
set  "artwork[8]=  GG.GGG    "
set  "artwork[9]= GGG.GG.GGG "
set "artwork[10]=GGGG....GGGG"
set "artwork[11]=--G.@..@.G--"
set "artwork[12]=---......---"
set "artwork[13]=--........--"
set "artwork[14]=  ...  ...  "
set "artwork[15]= EEE    EEE "
set "artwork[16]=EEEE    EEEE"

:: compute sprite out of artwork
setlocal enableDelayedExpansion
rem set "ESC=^!ESC^!"
set "sprite=%saveCursor()%"
set /A "space=0"
set "lastChar= "
for /l %%r in (0, 1, %artwork.lastRow%) do (
	for /l %%c in (0, 1, %artwork.lastColumns%) do (
		for %%a in ("!artwork[%%~r]:~%%~c,1!") do (
			if "!lastChar!" == "%%~a" (
				if "%%~a" == " " ( set /a "space+=1"
				) else ( set "sprite=!sprite!Û"
				)
			) else (
				if "!lastChar!" == " " (
					if        !space! LSS 0 ( set "sprite=!sprite!%left(x):x=!space:~1!%"
					) else if !space! GTR 0 ( set "sprite=!sprite!%right(x):x=!space!%"
					)
				) else (
					if "%%~a" == " " (
						set "space=1"
						if defined lastChar set "sprite=!sprite!%resetAttributes()%"
					) else (
						set "sprite=!sprite!%resetAttributes()%"
					)
				)

				if        "%%~a" == "X" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c0^!%Û"
				) else if "%%~a" == "G" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c1^!%Û"
				) else if "%%~a" == "." ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c2^!%Û"
				) else if "%%~a" == "@" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c3^!%Û"
				) else if "%%~a" == "-" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c4^!%Û"
				) else if "%%~a" == "E" ( set "sprite=!sprite!%setColor(r;g;b):r;g;b=^!c5^!%Û"
				)
			)
			set "lastChar=%%~a"
		)
	)
	if not "!lastChar!" == " " (
		set "sprite=!sprite!%resetAttributes()%"
		set "lastChar= "
		set "space=0"
	)
	if not "%%~r" == "%artwork.lastRow%" (
		set "sprite=!sprite!%down(x):x=1%"
	)
	set /a "space-=(artwork.lastColumns+1)"
)
endlocal & set "sprite=%sprite%%restoreCursor()%%unsaveCursor()%"

goto :eof

Post Reply