Older Version Newer Version

JanetTerra JanetTerra Aug 5, 2006

Multi-Colored Text

Add a Little More Pizzaz to Graphics Text

- JanetTerra JanetTerra
You can define the color of your graphics text with the graphics COLOR command. There are the 16 defined colors and as well as Buttonface, the default background color.

Liberty BASIC's 16 Named Colors

The COLOR command works with a Graphics Window or Graphicbox .
 Open "My Graphics" for Graphics as #g 

The COLOR Command Using a Named Color
 Print #g, "Color Blue" 
or
 hue$ = "Blue" 
Print #g, "Color ";hue$

The COLOR Command Using an RGB Color

Beyond the named colors are the RGB colors. RGB colors are composites of Red, Green and Blue. Each of the three colors can hold values of 0 - 255.
 Print #g, "Color 128 0 255" 
or
 redHue = 128 
greenHue = 0
blueHue = 255
Print #g, "Color ";redHue;" ";greenHue;" ";blueHue
or
 hue$ = "128 0 255" 
Print #g, "Color ";hue$

Only one color can be used at a time, but, by layering colors, some very interesting effects can be achieved. The following demo
  1. Gets a bitmap of the entire graphics window (pic1)
  2. Selects a new color for the text
  3. Draws the text in the new color
  4. Gets a bitmap of a portion of the graphics window (pic)
  5. Draws pic1
  6. Overlays pic1 with pic2
  7. Begins again with Step 1

This loop continues for the height of the text. All drawing is DISCARD ed until the final drawing is captured with the GETBMP command, displayed with the DRAWBMP command and FLUSH ed to conserve memory. Memory is further conserved with a combination of SEGMENT and DELSEGMENT commands.
 Nomainwin 
WindowWidth = 757
WindowHeight = 554
UpperLeftX = Int((DisplayWidth - WindowWidth) /2)
UpperLeftY = Int((DisplayHeight - WindowHeight) /2)

Menu #demo, "&Options", "E&xit", MenuExit
Graphicbox #demo.gb1, 0, 0, 750, 500

Open "Rainbow Text" for Window as #demo
#demo, "Trapclose TrapExit"
#demo.gb1, "Down; Fill Lightgray; Flush"
#demo.gb1, "Getbmp pic 0 0 750 500"

Call RainbowText "Rainbow Colors", "Verdana 32 Bold", 80, 100
Call VarigatedText "Varigated Blue", "Courier_New 54 Bold Italic", 70, 200

Wait

Sub TrapExit handle$
Close #handle$
End
End Sub

Sub MenuExit
Call TrapExit "#demo"
End Sub

Sub RainbowText text$, textFont$, textLLX, textLLY
Unloadbmp "pic"
#demo.gb1, "Font ";textFont$
FontHeight = FontHeight(textFont$)
yUpper = textLLY - FontHeight
yLower = textLLY + Int(FontHeight / 2)
For y = yLower to yUpper Step -3
#demo.gb1, "Getbmp pic1 0 0 750 500"
redHue = Int(Rnd(1) * 256)
greenHue = Int(Rnd(1) * 256)
blueHue = Int(Rnd(1) * 256)
#demo.gb1, "Backcolor Lightgray"
#demo.gb1, "Color ";redHue;" ";greenHue;" ";blueHue
#demo.gb1, "Place ";textLLX;" ";textLLY
#demo.gb1, "\";text$
#demo.gb1, "Getbmp pic2 0 0 750 ";y
#demo.gb1, "Drawbmp pic1 0 0"
#demo.gb1, "Drawbmp pic2 0 0"
#demo.gb1, "Discard"
Next y
#demo.gb1, "Getbmp pic 0 0 750 500"
#demo.gb1, "Drawbmp pic 0 0"
#demo.gb1, "Segment segID"
#demo.gb1, "Flush"
#demo.gb1, "Delsegment ";segID - 1
End Sub

Sub VarigatedText text$, textFont$, textLLX, textLLY
Unloadbmp "pic"
FontHeight = FontHeight(textFont$)
#demo.gb1, "Font ";textFont$
yUpper = textLLY - FontHeight - 2
yLower = textLLY + Int(FontHeight / 2)
hueInc = Int(256 / FontHeight) * 2
blueHue = 0
For y = yLower to yUpper Step -2
#demo.gb1, "Getbmp pic1 0 0 750 500"
blueHue = Min(255, blueHue + hueInc)
#demo.gb1, "Backcolor Lightgray"
#demo.gb1, "Color 0 0 ";blueHue
#demo.gb1, "Place ";textLLX;" ";textLLY
#demo.gb1, "\";text$
#demo.gb1, "Getbmp pic2 0 0 750 ";y
#demo.gb1, "Drawbmp pic1 0 0"
#demo.gb1, "Drawbmp pic2 0 0"
#demo.gb1, "Discard"
Next y
#demo.gb1, "Getbmp pic 0 0 750 500"
#demo.gb1, "Drawbmp pic 0 0"
#demo.gb1, "Segment segID"
#demo.gb1, "Flush"
#demo.gb1, "Delsegment ";segID - 1
End Sub

Function FontHeight(textFont$)
n = 1
While Val(Word$(textFont$, n)) = 0
n = n + 1
Wend
FontHeight = Val(Word$(textFont$, n))
End Function

If the graphics window becomes obscured by another window or lies offscreen, DRAWBMP will not capture the full drawing. This can be prevented by using the stylebits WS_EX_TOPMOST prior to opening the window.