
SUB VSFadeToColor (r%, g%, b%, shades%)
   DIM tempcol%(2)
     
   FOR temp2% = 1 TO shades%
      FOR temp% = 0 TO 255
         OUT &H3C7, temp%
         tempcol%(0) = INP(&H3C9)
         tempcol%(1) = INP(&H3C9)
         tempcol%(2) = INP(&H3C9)
         OUT &H3C8, temp%
         FOR temp3% = 0 TO 2
            IF tempcol%(temp3%) > r% THEN
               tempcol%(temp3%) = tempcol%(temp3%) - 1
            ELSEIF tempcol%(temp3%) < r% THEN
               tempcol%(temp3%) = tempcol%(temp3%) + 1
            END IF
            OUT &H3C9, tempcol%(0)
         NEXT
      NEXT
      IF shades% > 2 THEN
         WAIT &H3DA, 8, 8
         WAIT &H3DA, 8
      END IF
   NEXT
END SUB

SUB VSFadeToPalette (file$, shades%)
   DIM Palvar AS STRING * 768, tempcol%(2)
   DEF SEG = VARSEG(Palvar)
   IF file$ <> "" THEN BLOAD file$ + ".pal", VARPTR(Palvar)

   FOR temp2% = 1 TO shades%
      FOR temp% = 0 TO 255
         OUT &H3C7, temp%
         tempcol%(0) = INP(&H3C9)
         tempcol%(1) = INP(&H3C9)
         tempcol%(2) = INP(&H3C9)
         OUT &H3C8, temp%
         FOR temp3% = 0 TO 2
            tempvar% = PEEK(VARPTR(Palvar) + temp% * 3 + temp3%)
            IF tempcol%(temp3%) < tempvar% THEN
               tempcol%(temp3%) = tempcol%(temp3%) + 1
            ELSEIF tempcol%(temp3%) > tempvar% THEN
               tempcol%(temp3%) = tempcol%(temp3%) - 1
            END IF
            OUT &H3C9, tempcol%(temp3%)
         NEXT
      NEXT
      IF shades% > 2 THEN
         WAIT &H3DA, 8, 8
         WAIT &H3DA, 8
      END IF
   NEXT
   DEF SEG = &HA000
END SUB

SUB vsfont (segment%, offset%, x%, y%, text$, col1%, col2%)
IF x% > 319 OR y% > 199 OR x% + LEN(text$) * 8 < 1 OR y% < -6 THEN EXIT SUB
FOR letter% = 1 TO LEN(text$)
   tmppeek% = 14 + 8 * ASC(MID$(text$, letter%, 1))
   tempx% = x% + letter% * 8 - 8
   FOR row% = 0 TO 7
      DEF SEG = &HFFA6
      temp% = PEEK(tmppeek% + row%)
      col% = (row% * col2% + (7 - row%) * col1%) \ 7
      tempy% = row% + y%
      DEF SEG = segment%
      IF tempy% >= 0 AND tempy% < 200 THEN
         IF tempx% >= 0 AND tempx% <= 319 THEN
            FOR tempx2% = 0 TO 7
               IF temp% AND (256 \ (2 ^ tempx2%)) THEN POKE tempx% + tempx2% + 320& * tempy% + offset%, col%
            NEXT
         END IF
      END IF
   NEXT
NEXT
END SUB

SUB vsgrayscale
   FOR col% = 0 TO 255
      OUT &H3C7, col%
      tempr% = INP(&H3C9)
      tempg% = INP(&H3C9)
      tempb% = INP(&H3C9)
      tempgray% = (11 * tempr% + 59 * tempg% + 30 * tempb%) \ 100
      OUT &H3C8, col%
      OUT &H3C9, tempgray%
      OUT &H3C9, tempgray%
      OUT &H3C9, tempgray%
   NEXT
END SUB

SUB vspalette (palfile$)
   OUT &H3C8, 0
   IF palfile$ <> "" THEN
      OPEN palfile$ + ".pal" FOR BINARY AS #1
         FOR temp% = 0 TO 767
            GET #1, 8 + temp%, temp2%
            OUT &H3C9, temp2%
         NEXT
      CLOSE
   ELSE
      FOR temp% = 0 TO 767
         OUT &H3C9, 0
      NEXT
   END IF
END SUB

SUB VSSavePalette (file$)
   DIM Palvar AS STRING * 768
  
   DEF SEG = VARSEG(Palvar)
   OUT &H3C7, 0
   FOR temp% = 0 TO 767
      POKE VARPTR(Palvar) + temp%, INP(&H3C9)
   NEXT
  
   IF file$ <> "" THEN BSAVE file$ + ".pal", VARPTR(Palvar), 767
   DEF SEG = &HA000
END SUB

SUB vsshadowfont (segment%, offset%, x%, y%, text$, col1%, col2%, col3%, col4%)
IF x% > 318 OR y% > 198 OR x% + LEN(text$) * 8 < 1 OR y% < -6 THEN EXIT SUB
FOR letter% = 1 TO LEN(text$)
   tmppeek% = 14 + 8 * ASC(MID$(text$, letter%, 1))
   tempx% = x% + letter% * 8 - 8
   FOR row% = 0 TO 7
      DEF SEG = &HFFA6
      temp% = PEEK(tmppeek% + row%)
      col% = (row% * col2% + (7 - row%) * col1%) \ 7
      shad% = (row% * col4% + (7 - row%) * col3%) \ 7
      tempy% = row% + y%
      DEF SEG = segment%
      IF tempy% >= 0 AND tempy% < 200 THEN
         IF tempx% >= 0 AND tempx% <= 319 THEN
            FOR tempx2% = 0 TO 7
               IF temp% AND (256 \ (2 ^ tempx2%)) THEN
                  POKE tempx% + tempx2% + 320& * tempy% + offset%, col%
                  POKE tempx% + tempx2% + 320& * tempy% + offset% + 321, shad%
               END IF
            NEXT
         END IF
      END IF
   NEXT
NEXT
DEF SEG = &HA000
END SUB

