Tech Note 04: Using Color Objects and GraphicsNovember 04, 2008© NSB Corporation. All rights reserved. |
In this Tech Note, we will
give a short introduction to System Palettes.
Then, we will define Color Table and UI Color List with
its enum Entries. Finally, we will discuss some Window
Functions and UI Color Functions showing how it is
possible, using API functions and some tricks, to develop
a full color project in NSBasic. Table of Contents:Introduction IntroductionSystem palettes of 1, 2, 4, or 8 bits-per-pixel are supported, as follows:
Color Table
|
||||
|
||||
UI Color List
Do not confuse the UI Color List with the system Color Table. The system color table (or system palette) defines all available colors for the display or draw window, whether they are in use or not. The UI Color List defines the colors used to draw the interface objects. UI Color Table Entries
|
UI Color Functions | |
UIColorGetTableEntryIndex |
IndexedColorType
UIColorGetTableEntryIndex (UIColorTableEntries which) |
UIColorGetTableEntryRGB |
void UIColorGetTableEntryRGB (UIColorTableEntries which,
RGBColorType *rgbP) |
UIColorSetTableEntry |
Err UIColorSetTableEntry (UIColorTableEntries which,
const RGBColorType *rgbP) |
Window Functions | |
WinIndexToRGB |
void WinIndexToRGB (IndexedColorType i,
RGBColorType *rgbP) |
WinRGBToIndex |
IndexedColorType WinRGBToIndex (const RGBColorType *rgbP) |
WinSetForeColor |
IndexedColorType WinSetForeColor (IndexedColorType foreColor) |
WinSetBackColor |
IndexedColorType WinSetBackColor (IndexedColorType backColor) |
WinSetTextColor |
IndexedColorType WinSetTextColor (IndexedColorType textColor) |
WinDrawGrayLine |
void WinDrawGrayLine (Coord x1, Coord y1,
Coord x2, Coord y2) |
WinDrawPixel |
void WinDrawPixel (Coord x, Coord y) |
WinErasePixel |
void WinErasePixel (Coord x, Coord y) |
WinInvertPixel |
void WinInvertPixel (Coord x, Coord y) |
WinGetPixel |
IndexedColorType WinGetPixel (Coord x, Coord y) |
WinEraseLine |
void WinEraseLine (Coord x1, Coord y1,
Coord x2, Coord y2) |
WinInvertLine |
void WinInvertLine (Coord x1, Coord y1,
Coord x2, Coord y2) |
WinScreenLock |
UInt8* WinScreenLock (WinLockInitType initMode) |
WinScreenUnlock |
void WinScreenUnlock (void) |
IndexedColorType
UIColorGetTableEntryIndex (UIColorTableEntries which)
where which is one of the symbolic color constants in UIColorTableEntries
The index value for a UI color for the current system palette
Dim which as Short
Dim IndexedColor as Short
which = 11 'UIFieldText, for example.
which = which * 256
IndexedColor = SysTrapFunc(932,1,which)
void UIColorGetTableEntryRGB (UIColorTableEntries which,
RGBColorType *rgbP)
where which is one of the symbolic color
constants in UIColorTableEntries
and *rgbP is a returned pointer to an RGB color
value corresponding to the current color used for the
symbolic color.
nothing
Dim which as Short
Dim rgbP as String
which = 10 'UIFieldBackground, for example.
which = which * 256
SysTrapSub 933,2,which,rgbP
Err UIColorSetTableEntry (UIColorTableEntries which,
const RGBColorType *rgbP)
where which is one of the symbolic color
constants in UIColorTableEntries
and const *rgbP is the RGB value
of the color that should be used for the specified UI
object defined as which.
0 upon success.
Dim Err as Short
Dim which as Short
Dim constrgbP as String 'You will see why...
which = constant * 256
Here is where you
insert RGB Table values :-)
This was "the tip of the ICEBERG". This was
found while working on Window Functions using Indexed
colors and converting them back to RGB: RGBColor is a 4-byte
value representing:
Index (may be *anything) + Red (byte) + Green (byte) + Blue (byte).
So, an RGB would be, for example, "$" + &h66 + &h33 + &hCC if you are picking color 64 (6633CC)
in RGBColorType
constrgbP = "$"
+ &h66 + &h33 +
&hCC
Err = SysTrapFunc(934,2,which,constrgbP)
But, if you wish color index 96 (00FFCC)?
Answer: the system will consider the first 00 (null char)
as the end of a string and will take 00FFCC00 as its
value. It will become color index 121 :-(
Solution:
Another "tip of the iceberg"... remember:
"the index holds the index of the closest match to the RGB
value". So, color index 96, for example, could be (01FFCC)
:-)
and the system will *convert* to the closest RGB index
color: 00FFCC...
void WinIndexToRGB (IndexedColorType i,
RGBColorType *rgbP)
where i is a color index value and
rgbP is a pointer to an RGB color value corresponding to
the index value i
.
Nothing
Dim i as Short
Dim rgbP as Variant
i = 134 'Indexed color 134
i = i * 256
SysTrapSub 927,2,i,rgbP
IndexedColorType WinRGBToIndex (const RGBColorType *rgbP)
The index of the closest matching color in the Color Table
Dim constrgbP as String
Dim IndexedColor as Short
constrgbP = "$"
+ &h66 + &h33 +
&hCC 'For example
IndexedColor = SysTrapFunc(926,1,constrgbP)
IndexedColorType WinSetForeColor (IndexedColorType foreColor)
The previous foreground color index.
Dim ForeIndexedColor as Short
Dim ForePreviousIndexedColor as Short
ForeIndexedColor = 96 'For example
ForeIndexedColor = ForeIndexedColor * 256
ForePreviousIndexedColor = SysTrapFunc(920,1,ForeIndexedColor)
IndexedColorType WinSetBackColor (IndexedColorType backColor)
The previous background color index.
Dim BackIndexedColor as Short
Dim BackPreviousIndexedColor as Short
BackIndexedColor = 96 'For example
BackIndexedColor = BackIndexedColor * 256
BackPreviousIndexedColor = SysTrapFunc(921,1,BackIndexedColor)
IndexedColorType WinSetTextColor (IndexedColorType textColor)
The previous text color index.
Dim textIndexedColor as Short
Dim textPreviousIndexedColor as Short
textIndexedColor = 96 'For example
textIndexedColor = textIndexedColor * 256
textPreviousIndexedColor = SysTrapFunc(922,1,textIndexedColor)
void WinDrawGrayLine (Coord x1, Coord y1,
Coord x2, Coord y2)
Nothing.
This routine does not draw in the gray color; it draws
with alternating foreground and background pixels
Dim x1 as Short
Dim y1 as Short
Dim x2 as Short
Dim y2 as Short
SysTrapSub 532,4,x1,y1,x2,y2
void WinDrawPixel (Coord x, Coord y)
Nothing.
Draw a pixel in the draw window using the current
foreground color.
Dim x as Short
Dim y as Short
SysTrapSub 899,2,x,y
void WinErasePixel (Coord x, Coord y)
Nothing.
Draw a pixel in the draw window using the current
background color.
Dim x as Short
Dim y as Short
SysTrapSub 900,2,x,y
void WinInvertPixel (Coord x, Coord y)
Nothing.
Invert a pixel in the draw window.
Dim x as Short
Dim y as Short
SysTrapSub 900,2,x,y
IndexedColorType WinGetPixel (Coord x, Coord y)
The indexed color value of the pixel in the current draw window.
Dim x as Short
Dim y as Short
Dim PixelIndexedColor as Short
PixelIndexedColor = SysTrapFunc(897,2,x,y)
void WinEraseLine (Coord x1, Coord y1,
Coord x2, Coord y2)
Nothing.
Draw a line in the draw window using the current
background color.
Dim x1 as Short
Dim y1 as Short
Dim x2 as Short
Dim y2 as Short
SysTrapSub 533,4,x1,y1,x2,y2
void WinInvertLine (Coord x1, Coord y1,
Coord x2, Coord y2)
Nothing.
Invert a line in the draw window.
Dim x1 as Short
Dim y1 as Short
Dim x2 as Short
Dim y2 as Short
SysTrapSub 534,4,x1,y1,x2,y2
UInt8* WinScreenLock (WinLockInitType initMode)
A pointer to the new screen base address.
"Lock" the current screen by switching the UI
concept of the screen base address to an area that is not
reflected on the display. This routine can be used to "freeze" the
display while doing lengthy drawing operations to avoid a
flickering effect. Call WinScreenUnlock to unlock the
display and cause it to be updated with any changes. The
screen must be unlocked as many times as it is locked to
actually update the display.
Dim initMode as Short
Dim screenAddress as Variant
initMode = 0 'Copy old screen to new
screenAddress = SysTrapFunc(928,1,initMode)
void WinScreenUnlock (void)
Nothing.
Unlock the screen and update the display.
The screen must be unlocked as many times as it is locked
to actually update the display.
SysTrapSub 929,0