TweetFollow Us on Twitter

Color Basics
Volume9
Number11
Column TabGetting Started

Related Info: Color QuickDraw Control Panel Gestalt Manager

The Basics of Color Quickdraw

Using a color grafport and multiple monitors

By Dave Mark, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Last month’s column introduced some of the basics of C++ and object programming. This month, we’ll turn our attention back to C and the Macintosh Toolbox. This month’s column introduces one of the most enjoyable parts of the Mac Toolbox, Color QuickDraw. Along the way, we’ll learn how to use Gestalt(), the Toolbox function that knows everything there is to know about the current state of your Macintosh.

So far, about the only real experience we’ve had with color has been with a program that displayed a color PICT in a window. Since any possible color work was handled by the routine DrawPicture(), we won’t even count that as color experience. This month’s program is called ColorMondrian, a colorized version of an early Mac Primer program called Mondrian.

Mondrian created a window and drew a never-ending series of randomly generated QuickDraw shapes in the window. All this was done in the plain old black-and-white of QuickDraw version 1, the version of QuickDraw that shipped with the original 128K Macintosh.

Ever since the introduction of the Macintosh II, all Macs have shipped with a new version of QuickDraw known as Color QuickDraw, also known as 8-bit Color QuickDraw. As the technology marched on, 24-bit and then 32-bit Color QuickDraw made their way into the Toolbox. For now, we’ll concentrate on the features common to all flavors of Color QuickDraw.

ColorMondrian starts by checking to see if Color QuickDraw is available on the current Macintosh. If so, it looks at each of the displays connected to the Mac (remember, your Mac can have multiple monitors hooked up at the same time!) to determine which display is the deepest, that is, can display the largest number of colors. For example, if you have an 8-bit monitor (256 simultaneous colors) and a 1-bit monitor (black and white only), ColorMondrian will identify the 8-bit monitor as the deepest.

Next, ColorMondrian will open a color window on the specified monitor and start drawing randomly sized QuickDraw shapes in randomly selected colors. If your deepest monitor is gray-scale, don’t worry. Shades of gray count as colors too!

Let’s get started. As has been the case of late, we’ll get the program running this month, then walk through the code next month.

Creating the ColorMondrian Resources

Start by creating a folder labeled ColorMondrian in your Development folder. Next, launch ResEdit and create a new file called ColorMondrian.Π.rsrc in the ColorMondrian folder.

Create a new MBAR resource with a resource id of 128. Enter the MENU ids 128, 129, 130, and 131 as shown in Figure 1.

Figure 1. The MBAR resource.

Next, create four MENU resources. The first, having a resource id of 128, will match the menu shown in Figure 2. It’s an • menu with a single item, About ColorMondrian....

Figure 2. The • menu.

The second menu should match the one shown in Figure 3. The title is File with a single item, Be sure to add the command-key equivalent, Q.

Figure 3. The File menu.

The third menu should match the one shown in Figure 4. It’s a standard Edit menu with a single separator line and all the standard command-key equivalents.

Figure 4. The Edit menu.

The fourth menu consists of a title and no items. The title is Devices, as shown in Figure 5.

Figure 5. The Devices menu.

Next you’ll create two ALRT resources along with their respective DITLs, one for displaying error messages, and one to bring up when About ColorMondrian... is selected from the • menu. First the error ALRT. Create an ALRT resource with a resource id of 128, using the sizing info in Figure 6.

Figure 6. Specifications for the error ALRT.

Double-click on the alert window to bring up a DITL editor. Create two items, an OK button (Figure 7) and a static text item (Figure 8).

Figure 7. Specifications for the OK button.

Figure 8. Specification for the static text item.

Now create a second ALRT with a resource id of 129. using the sizing info in Figure 9.

Figure 9. Specifications for the about ALRT.

Double-click on the alert window to bring up a DITL editor. Create two items, an OK button (Figure 10) and a static text item (Figure 11).

Figure 10. Specifications for the OK button.

Figure 11. Specification for the static text item.

Creating the ColorMondrian Project

Quit ResEdit, being sure to Save your changes. Now launch THINK C and create a new project named ColorMondrian.Π in the ColorMondrian folder. Add the MacTraps and ANSI libraries to the project (In THINK 6, ANSI is inthe Standard Libraries folder - add it in its own segment), then create a new source code window. Save the window as ColorMondrian.c and Add it to the project as well.

Type in this source code, being sure to save periodically:

/* 1 */

#include <stdio.h>
#include <GestaltEqu.h>


#define kMBARResID 128
#define kErrorAlertID128
#define kAboutALRTid 129

#define kSleep   0L

#define kAutoStorage NULL
#define kVisible true
#define kWindowTitle "\pColor Mondrian"
#define kMoveToFront (WindowPtr)-1
#define kNoGoAwayfalse
#define kNULLRefCon60L

#define mApple   128
#define iAbout   1

#define mFile    129
#define iQuit    1

#define mDevice  131

#define kWindowMargin5

#define kRandomUpperLimit 32768

#define kEmptyString "\p"
#define kNULLFilterProc   NULL

/*************/
/*  Globals  */
/*************/

Boolean gDone;

/***************/
/*  Functions  */
/***************/

void    ToolboxInit( void );
void    MenuBarInit( void );
void    CreateWindow( GDHandle device );
void    EventLoop( void );
void    DoEvent( EventRecord *eventPtr );
void    HandleMouseDown( EventRecord *eventPtr );
void    HandleMenuChoice( long menuChoice );
void    HandleAppleChoice( short item );
void    HandleFileChoice( short item );
void    HandleDeviceChoice( short item );
Boolean HasColorQD( void );
GDHandleGetDeepestDevice( void );
short   GetDeviceDepth( GDHandle device );
void    DrawRandomRect( void );
void    RandomColor( RGBColor *colorPtr );
void    RandomRect( Rect *rectPtr );
short Randomize( short range );
void    DoError( Str255 errorString );


/****************** main ***************************/

void main( void )
{
 ToolboxInit();
 
 if ( ! HasColorQD() )
 DoError( 
 "\pThis machine doesn't support Color Quickdraw!" );
 
 MenuBarInit();
 
 CreateWindow( GetDeepestDevice() );
 
 EventLoop();
}


/****************** ToolboxInit *********************/

void ToolboxInit( void )
{
 InitGraf( &thePort );
 InitFonts();
 InitWindows();
 InitMenus();
 TEInit();
 InitDialogs( nil );
 InitCursor();
}


/****************** MenuBarInit ***********************/

void MenuBarInit( void )
{
 Handle menuBar;
 MenuHandle menu;
 GDHandle device, deepestDevice;
 Str255 itemStr;
 short  curDeviceNumber = 1;
 
 menuBar = GetNewMBar( kMBARResID );
 SetMenuBar( menuBar );

 menu = GetMHandle( mApple );
 AddResMenu( menu, 'DRVR' );
 
 menu = GetMHandle( mDevice );
 
 deepestDevice = GetDeepestDevice();
 
 device = GetDeviceList();
 
 while ( device != NULL )
 {
 itemStr[0] = 10;
 sprintf( (char *)(&(itemStr[1])), "0x%08lX", 
 (unsigned long)device );
 AppendMenu( menu, itemStr );
 
 if ( device == deepestDevice )
 CheckItem( menu, curDeviceNumber, true );
 
 device = GetNextDevice( device );
 curDeviceNumber++;
 }
 
 DrawMenuBar();
}


/****************** CreateWindow ***********************/

void CreateWindow( GDHandle device )
{
 WindowPtrwindow;
 Rect   wBounds;
 
 wBounds = (**device).gdRect;
 
 if ( device == GetMainDevice() )
 wBounds.top += GetMBarHeight();
 
 InsetRect( &wBounds, kWindowMargin, kWindowMargin );
 
 window = NewCWindow( kAutoStorage, &wBounds, kWindowTitle, 
 kVisible, altDBoxProc, kMoveToFront, 
 kNoGoAway, kNULLRefCon );
 
 if ( window == nil )
 {
 DoError( "\pCouldn't create window!" );
 }
 else
 {
 ShowWindow( window );
 SetPort( window );
 }
}


/******************************** EventLoop *********/

void EventLoop( void )
{
 EventRecordevent;
 
 GetDateTime( (unsigned long *)(&randSeed) );
 
 gDone = false;
 while ( gDone == false )
 {
 if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
 DoEvent( &event );
 
 DrawRandomRect();
 }
}


/************************************* DoEvent *********/

void DoEvent( EventRecord *eventPtr )
{
 char   theChar;
 
 switch ( eventPtr->what )
 {
 case mouseDown: 
 HandleMouseDown( eventPtr );
 break;
 case keyDown:
 case autoKey:
 theChar = eventPtr->message & charCodeMask;
 if ( (eventPtr->modifiers & cmdKey) != 0 ) 
 HandleMenuChoice( MenuKey( theChar ) );
 break;
 }
}


/****************************** HandleMouseDown *********/

void HandleMouseDown( EventRecord *eventPtr )
{
 WindowPtrwindow;
 short  thePart;
 long   menuChoice;
 
 thePart = FindWindow( eventPtr->where, &window );

 switch ( thePart )
 {
 case inMenuBar:
 menuChoice = MenuSelect( eventPtr->where );
 HandleMenuChoice( menuChoice );
 break;
 case inSysWindow : 
 SystemClick( eventPtr, window );
 break;
 }
}


/****************** HandleMenuChoice ***********************/

void HandleMenuChoice( long menuChoice )
{
 short  menu;
 short  item;
 
 if ( menuChoice != 0 )
 {
 menu = HiWord( menuChoice );
 item = LoWord( menuChoice );
 switch ( menu )
 {
 case mApple:
 HandleAppleChoice( item );
 break;
 case mFile:
 HandleFileChoice( item );
 break;
 case mDevice:
 HandleDeviceChoice( item );
 break;
 }
 HiliteMenu( 0 );
 }
}


/****************** HandleAppleChoice ***********************/

void HandleAppleChoice( short item )
{
 MenuHandle appleMenu;
 Str255 accName;
 short  accNumber;
 
 switch ( item )
 {
 case iAbout:
 NoteAlert( kAboutALRTid, kNULLFilterProc );
 break;
 default:
 appleMenu = GetMHandle( mApple );
 GetItem( appleMenu, item, accName );
 accNumber = OpenDeskAcc( accName );
 break;
 }
}


/****************** HandleFileChoice ***********************/

void HandleFileChoice( short item )
{
 switch ( item )
 {
 case iQuit :
 gDone = true;
 break;
 }
}


/****************** HandleDeviceChoice **********************/

void HandleDeviceChoice( short item )
{
/* Try this: 
 Modify the program so that when a device is selected
 from the Device menu, the current window gets closed and a
 new window is opened on the selected device. Be careful when
 you translate the menu item back into an address. Debug your
 program thoroughly before you try to use the address as an
 address. You don't want to accidentally reformat your hard 
 drive, right?
 
 Also, don't forget to update the check mark!
*/
}


/****************** HasColorQD *****************/

Boolean HasColorQD( void )
{
 unsigned char   version[ 4 ];
 OSErr  err;
 
 err = Gestalt( gestaltQuickdrawVersion, (long *)version );
 
 if ( err != noErr )
 {
 SysBeep( 10 );  /*  Error calling Gestalt!!!  */
 ExitToShell();
 }
 
 if ( version[ 2 ] > 0 )
 return( true );
 else
 return( false );
}


/****************** GetDeepestDevice *****************/

GDHandle GetDeepestDevice( void )
{
 GDHandle curDevice, maxDevice = NULL;
 short  curDepth, maxDepth = 0;
 
 curDevice = GetDeviceList();
 
 while ( curDevice != NULL )
 {
 curDepth = GetDeviceDepth( curDevice );
 
 if ( curDepth > maxDepth )
 {
 maxDepth = curDepth;
 maxDevice = curDevice;
 }

 curDevice = GetNextDevice( curDevice );
 }
 
 return( maxDevice );
}


/****************** GetDeviceDepth *****************/

short GetDeviceDepth( GDHandle device )
{
 PixMapHandle  screenPixMapH;
 
 screenPixMapH = (**device).gdPMap;
 
 return( (**screenPixMapH).pixelSize );
}


/****************** DrawRandomRect *****************/

void DrawRandomRect( void )
{
 Rect   randomRect;
 RGBColor color;
 
 RandomRect( &randomRect );
 RandomColor( &color );
 RGBForeColor( &color );
 PaintOval( &randomRect );
}


/****************** RandomColor *********************/

void RandomColor( RGBColor *colorPtr )
{
 colorPtr->red = Random() + 32767;
 colorPtr->blue = Random() + 32767;
 colorPtr->green = Random() + 32767;
}


/****************** RandomRect *********************/

void RandomRect( Rect *rectPtr )
{
 WindowPtrwindow;

 window = FrontWindow();
 
 rectPtr->left = Randomize( window->portRect.right
 - window->portRect.left );
 rectPtr->right = Randomize( window->portRect.right
 - window->portRect.left );
 rectPtr->top = Randomize( window->portRect.bottom
 - window->portRect.top );
 rectPtr->bottom = Randomize( window->portRect.bottom
 - window->portRect.top );
}


/****************** Randomize **********************/

short Randomize( short range )
{
 long   randomNumber;
 
 randomNumber = Random();
 
 if ( randomNumber < 0 )
 randomNumber *= -1;
 
 return( (randomNumber * range) / kRandomUpperLimit );
}


/***************** DoError ********************/

void DoError( Str255 errorString )
{
 ParamText( errorString, kEmptyString, 
 kEmptyString, kEmptyString );
 
 StopAlert( kErrorAlertID, kNULLFilterProc );
}

Running ColorMondrian

Save your changes, then select Run from the Project menu. If ColorQuickDraw is not available on your machine (unlikely), an error message will appear. Otherwise, a window, similar to the one shown in Figure 12, will appear on the monitor with the deepest pixel settings.

Figure 12. ColorMondrian in action.

Bring the Finder to the front by selecting Finder from the applications menu at the right end of the menu bar. Notice that ColorMondrian stops dead in its tracks. Go back to ColorMondrian and select Quit from the File menu. Back in THINK C, select Set Project Type... from the Project menu. Set the value in the SIZE Flags field to 5880 (you don’t have to fiddle with the popup menu - you can just type the number in the field).

You’ve just set ColorMondrian up to continue processing events even when it is running in the background. To prove this, select Run from the Project menu, then bring the Finder to the front again. This time, ColorMondrian will continue running, even in the background.

Next, click on the Devices menu. A hex address will appear for every monitor attached to your Macintosh. A check-mark will appear next to the address representing the monitor with the deepest settings. You’ll find out what the address or addresses are for in next month’s column. Figure 13 shows my Devices menu. As you can see, I’ve got two monitors attached to my Mac.

Figure 13. My devices menu.

If you’ve got more than one monitor on your machine, try using the Monitors control panel to set one monitor to 1-bit and the other to a deeper setting. Run ColorMondrian. The window should appear on the deeper monitor. Now switch the settings so that the second monitor has the deeper settings. Run ColorMondrian again. Now the window should appear on the other monitor.

Till Next Month...

Next, month we’ll get into all the theory behind ColorMondrian. Till then, take a look through the code, then read up on Color QuickDraw in the pages of Inside Macintosh or on your Mac, courtesy of THINK Reference.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.