'===========================================================================
' Subject: RESTORE INITIAL SCREEN COLOR       Date: 03-14-98 (10:14)       
'  Author: Brian McLaughlin                   Code: PB                     
'  Origin: alt.lang.powerbasic              Packet: PB.ABC
'===========================================================================
$IF 0
Under DOS, in text mode, you can use ANSI.SYS to force screen colors
other than B&W. I find very few text-mode programs restore the colors 
I am using when they exit. Instead, they revert the screen to B&W.
The following code illustrates how to save the colors being used under 
DOS, and restore them. I'm releasing this code into the public domain.
It requires PB3C or higher.
$ENDIF

$LIB ALL OFF
DECLARE SUB InitialScreenColors (Flag%)

%SAVECOLORS    = 0
%RESTORECOLORS = 1

InitialScreenColors %SAVECOLORS
CLS
PRINT "Initial colors have been saved."
DELAY 1
InitialScreenColors %RESTORECOLORS

'=============================================================================
 SUB InitialScreenColors (Flag%)   PUBLIC
'=============================================================================
' If you want to save the original screen colors, call this
' SUB before you print ANYTHING to the screen, or do a CLS.
'
' Flag% indicates whether to save or restore the screen colors:
'   %SAVECOLORS = 0
'   %RESTORECOLORS = 1
 
 DIM InitColorAttr AS STATIC BYTE
 
 IF Flag% = 0 THEN       'save the current screen colors.
   CursorRow% = CSRLIN
   CursorCol% = POS
   IF CursorCol% > 1 THEN DECR CursorCol%
   IF CursorRow% > 1 THEN DECR CursorRow%
   Attribute% = SCREEN(CursorRow%, CursorCol%, 1)
   InitColorAttr = CBYT(Attribute%)

 ELSEIF Flag% = 1 THEN   'restore the current screen colors.
   InitAttr? = InitColorAttr
   ASM  Les DI, pbvScrnBuff
   ASM  Inc DI             ; point at attribute byte, not char byte
   ASM  Mov AL, InitAttr?
   ASM  Mov CX, 2000
 VidLoop:
   ASM  Stosb              ; increments DI automatically
   ASM  Inc DI             ; but we need to increment DI once more
   ASM  Loop VidLoop
 END IF

END SUB
