# Re:fading In Qb 4.5

``` BBS: Inland Empire Archive
Date: 06-02-92 (06:49)             Number: 173
From: RICH GELDREICH               Refer#: NONE
To: MICAH SHERR                   Recvd: NO
Subj: Re:fading In Qb 4.5            Conf: (2) Quik_Bas```
```> Dear Experienced QuickBasic Programmer,
>
>         I am at my wit's end trying to figure out a way to smoothly
> fade a mcga screen to black. I don't wish to just make every color
> black. I want to know how to do a "Sierra" quality fade. I have
> experimented with the palette commands, but to no avail. If ANYONE can
> help, PLEASE respond!
>         By the way, I have tried the formula the way it is displayed
> in QuickBASIC's help screen for the palette statement.  However, it
> doesn't seem to work.  It always produces a number around 4,000,000.

Did you see my VGA fading routines? I posted two of them- one in QB,
and one in assembly. Fading the screen is very easy... here goes!

Lets say we have 4 colors to fade. The colors are:

1. 20,20,30
2. 30,0,0
3. 16,10,0
4. 63,63,63

The object is to go from brightest to darkest as smoothly as
possible. After each fade(or frame), we wait until the next one so that
the fade works at the same speed for each computer.

To do this fade, all we have to do is store the R G B values in 3
arrays, like this:

DIM R(4)
DIM G(4)
DIM B(4)

R(1)=20:G(1)=20:B(1)=30
R(2)=30:G(2)=0:B(2)=0
R(3)=16:G(3)=10:B(3)=0
R(4)=63:G(4)=63:B(4)=63

In order to fade these colors, we must have a quick way of changing
palettes. Since QB's PALETTE command calls BIOS to do it's dirty work,
it's rather slow. Instead, we're going to go directly to the hardware
for speed.

To fade the colors, we go from 1 to 0 with a small step, say 1/64 or
so. There's one problem with that, though. It uses floating point
math(which is slow as hell). The alternate way is to use integer math-
something computers do best. Instead of representing .5 in floating
point form, we would represent .5 in fractional form(e.g. 32/64). This
allows us to manipulate floating point numbers as integers.

The port addresses on the VGA for changing attributes are &h3C7,
&h3c8, and &h3c9(these are hexidecimal, or base 16, numbers). To change
a color on the VGA, quickly, use this:

OUT &h3c7, colornumber:out &h3c8, colornumber          'OUT to BOTH!
Out &h3c9, red:out &h3c9, green:out &h3c9, blue

This is the same thing as:

Palette colornumber, Red+Green*256&+Blue*65536&

To do our fade of the 4 colors, we just do a simple FOR/NEXT loop:

For A=63 to 0 step -1

For B=1 to 4
Rnew=(R(B)*A)\63         '<--notice "\" integer division
Gnew=(G(B)*A)\63
Bnew=(B(B)*A)\63
out &h3c7,B:out &h3c8,B
out &h3c9,Rnew
out &h3c9,Gnew
out &h3c9,Bnew
next

next

That 'ought to do it. The calculation

Rnew = (R(B) * A) \ 63

is really

Rnew = R(B) * (A/63)

If you know your algebra, you'll understand what's going on.

Hope that helps! If you need a practical program(one that gets the
R,G,B values for you) then let me know.

Rich Geldreich

(P.S. All of this was off the top of my head so I hope there's no
stupid bugs in the port numbers or something :-)

--- RBBSMAIL 17.2A
* Origin: Computer Co-Op RBBS HST, 609-784-9404 Voorhees
NJ (RBBS-PC 1:266/29)
```

Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET