                                 
                                 GET COLORS
                                Version 1.0 
                              January 10, 1994

FREEWARE for evaluation and use by anyone who may be interested.  Please
see comments below.

UPLOAD AUTHOR: Richard Simpson (CompuServe #73353,2204).

PURPOSE: To save a user's (unknown) screen colors at the beginning of
a batch file, so that they can be reset at the end of the batch file.

FILE LIST:
     DEMO.BAT        Run and examine for example.
     GETCOLOR.BAT    Called from DEMO.BAT and contains IF statements.
     GET-FORE.COM    Detects foreground color and returns errorlevel.
     GET-BACK.COM    Detects background color and returns errorlevel.
     GET-BOLD.COM    Detects bold attribute and returns errorlevel.
     README          This file.

INSTRUCTIONS:  Anyone familiar with DOS batch files and setting colors
in batch files will easily see how this utility works simply by looking at
the two batch files included.  Start with DEMO.BAT, which is a substitution
for your real batch file.  DEMO.BAT calls GETCOLOR.BAT near the beginning.
By having your batch file call GETCOLOR.BAT, you won't have to retype the
contents into your batch file.  GETCOLOR.BAT runs the three COM files
which detect the screen attributes and return the attributes as errorlevels.
The attributes are saved as DOS environment variables.  At the end of
DEMO.BAT, the attributes are reset using ANSI escape sequences with the
previously set environment variables.  (Of course, ANSI.SYS must be loaded.)

COMMENTS:  This is my first software upload.  I'm just beginning to try
to teach myself programming.  My first attempt at writing this utility
consisted of one COM file to detect all the screen attributes.  One
problem with it was that GETCOLOR.BAT then required 128 lines of IF
ERRORLEVEL statements.  I posted a message on the IBMPRO forum, Assembler
section, asking for ideas to improve this utility (and acknowledging that
I'm just a beginner).  Peter Below (CompuServe #100113,1101) was one of
the members who answered, and he pointed out that I could reduce the number
of IF ERRORLEVEL statements by having separate programs to detect the
foreground and background.  He gave me the source code set out below for
detecting the foreground and background.  From there, I was able to write
the program to detect the bold attribute.

     Peter also suggested avoiding the need for the IF ERRORLEVEL statements
in the batch file by having one program write the attribute values to a file
and having another program read the file, build the ANSI sequences, and write
to the DOS standard output.  I haven't learned enough yet to know how to do
that.  If I keep learning, I'll try that approach.

     Anyone who has any comments is encouraged to send them to me directly
(CompuServe #73353,2204) or post them in the IBMPRO forum.  My original
request for help appeared in a message thread called Small Pgramming Problem
(message #105639).  The following may be of interest to other beginners:

                             Attribute Byte
   BackgroundĿ     ForegroundĿ
 Ŀ Ŀ Ŀ Ŀ Ŀ Ŀ Ŀ Ŀ
   7      6      5      4      3      2      1      0   
 Blink   Red   Green   Blue  Bright  Red   Green   Blue 
        

           Binary Bits    Decimal   ANSI Foreground   ANSI Background
 Black        0 0 0          0           30                40
 Blue         0 0 1          1           34                44
 Green        0 1 0          2           32                42
 Cyan         0 1 1          3           36                46
 Red          1 0 0          4           31                41
 Magenta      1 0 1          5           35                45
 Yellow       1 1 0          6           33                43
 White        1 1 1          7           37                47


     GET-FORE.COM (File size is F hex or 15 decimal)

MOV AH,8       ;BIOS int 10/8 returns attribute at cursor in AH
XOR BH,BH      ;Screen page 0
INT 10
AND AH,7       ;Keep only lower 3 bits
XCHG AL,AH     ;Prepare AL for errorlevel
MOV AH,4C
INT 21

     
     GET-BACK.COM  (File size is 13 hex or 19 decimal)

MOV AH,8       ;BIOS int 10/8 returns attribute at cursor in AH
XOR BH,BH      ;Screen page 0
INT 10
MOV CL,4       ;Shift color 4 bits to right to get
SHR AH,CL      ;background color in low nibble
AND AH,7       ;Keep only lower 3 bits
XCHG AL,AH     ;Prepare AL for errorlevel
MOV AH,4C
INT 21         

     
     GET-BOLD.COM  (File size is 13 hex or 19 decimal)

MOV AH,8       ;BIOS int 10/8 returns attribute at cursor in AH
XOR BH,BH      ;Screen page 0
INT 10
MOV CL,4       ;Shift color 3 bits to right to get
SHR AH,CL      ;4th bit (bit 3) in 1st bit (bit 0) position
AND AH,7       ;Keep only 1st bit (bit 0)
XCHG AL,AH     ;Prepare AL for errorlevel
MOV AH,4C
INT 21         


The following shows two examples of the effect on the registers as 
GET-BOLD.COM is stepped through, with colors set as specified:

Example #1:  Yellow on White (no bold used)

               Backgrnd  Foregrnd
  Binary       0 1 1 1    0 1 1 0
  Hex             7          6
  Decimal         7          6

INSTRUCTION                   REGISTERS AFTER EXECUTION

MOV     AH,08                 AX=0800  BX=0000  CX=0013  

XOR     BH,BH                 AX=0800  BX=0000  CX=0013  

INT     10                    AX=7620  BX=0000  CX=0013

MOV     CL,03                 AX=7620  BX=0000  CX=0003  

SHR     AH,CL                 AX=0E20  BX=0000  CX=0003  

     After shifting 3 bits to right, binary changes from
     0111-0110 to 0000-1110, and hex changes from 76 to 0E.

AND     AH,01                 AX=0020  BX=0000  CX=0003  

     After keeping only first digit, binary changes from
     0000-1110 to 0000-0000 and hex changes from 0E to 00.

XCHG    AL,AH                 AX=2000  BX=0000  CX=0003  

MOV     AH,4C                 AX=4C00  BX=0000  CX=0003  

INT     21                    Program terminated normally                                                     

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Example #2:  Bright Yellow on Blue

               Backgrnd  Foregrnd
  Binary       0 0 0 1    1 1 1 0
  Hex             1          E
  Decimal         1         14

INSTRUCTION                   REGISTERS AFTER EXECUTION

MOV     AH,08                 AX=0800  BX=0000  CX=0013  

XOR     BH,BH                 AX=0800  BX=0000  CX=0013  

INT     10                    AX=1E20  BX=0000  CX=0013  

MOV     CL,03                 AX=1E20  BX=0000  CX=0003  

SHR     AH,CL                 AX=0320  BX=0000  CX=0003  

     After shifting 3 bits to right, binary changes from
     0001-1110 to 0000-0011, and hex changes from 1E to 03.

AND     AH,01                 AX=0120  BX=0000  CX=0003  

     After keeping only first digit, binary changes from
     0000-0011 to 0000-0001 and hex changes from 03 to 01.

XCHG    AL,AH                 AX=2001  BX=0000  CX=0003  

MOV     AH,4C                 AX=4C01  BX=0000  CX=0003  

INT     21                    Program terminated normally                                                     
