TweetFollow Us on Twitter

Colorizing
Volume Number:7
Issue Number:2
Column Tag:Developer's Notes

Related Info: Color Manager Palette Manager Color Quickdraw
Device Manager

Colorizing the Mac

By Hugh Fisher, Page, Australia

Colorizing the Macintosh

In August I began converting a black and white Macintosh game, Fire-Brigade, to run in color on the Mac II. One of the requirements for the new version was for a map to be displayed in a specific set of colors, even on 16 color cards; and for the colors on this map to change to show the different seasons. “No problem,” I said to El Presidente, “Apple set up the Palette Manager for that kind of thing - I read it in Inside Mac Five. I’ll design the code over the weekend and put it on the machine Monday.”

After a statement like that it went as you would expect: six weeks of struggle, panic, and despair before finally getting the thing to work. Along the way I learned many interesting things about the Color and Palette Managers.

CLUTs and CopyBits

A quick recap on color graphics: the average color graphics card stores 2, 4, or 8 bit pixel values in memory rather than a 24 or 48 bit RGB color. When the screen is drawn, these pixel values are used as indexes into a color lookup table (CLUT) which stores actual RGB entries. Usually you change the color of something by keeping the table the same and redrawing with a new pixel value. Color cycling, or color table animation, instead stores a new RGB color into an entry in the table, immediately changing the color of every pixel with that value. This is often thought of as just an arcade game technique, but is also essential for manipulating digitized images of all sorts. Sorry if this is all elementary stuff to you.

Each monitor attached to a Mac II has a device CLUT, of type CTabHandle. This is a handle to a table of ColorSpecs, each of which is an RGB color and a 16 bit pixel value. The device CLUT is stored as the pmTable field for the PixMap representing the device video RAM. The pixel values in a device table are used by the Color Manager for various purposes, and it is most likely just a shadow copy of the real hardware table anyway, so it should not be altered directly.

Offscreen pixmaps also have a pixmap CLUT, usually copied from a device. These pixmaps never change depth and have no hardware to worry about, so every pixel value in the table is equal to its index and every entry is valid. New RGB colors can be assigned directly to table entries - a pixel value means whatever you want it to.

Color Quickdraw often needs to check if two color tables correspond. Instead of comparing the two entry by entry it just compares the ctSeed fields and assumes that if the seeds match, so does everything else. Device seeds are maintained and updated by the Color Manager. Pixmap tables copied from a device start with the same seed value. If you are going to change the RGB colors in an offscreen table it needs a unique seed of its own, so call GetCTSeed when it is first created. The seed does not need to change every time the RGB colors change, just be unique.

When CopyBits is called with PixMap parameters, it checks the seed values of the source and destination. If they are the same, the pixel values can be just blitted across directly. If the seeds are different, CopyBits translates each source pixel value into the best RGB match available in the destination table. The translation overhead is not detectable, but despite this I saw a letter in MacTutor recommending setting the source and destination seeds equal before a CopyBits. Don’t do it! If the seeds are already the same, you gain nothing. If they are different, your source image will be randomly recolored in the destination.

Down and Dirty Color Cycling

Color cycling an offscreen image is easy - you just store the new RGB color in the table. Onscreen color cycling can be done through the Color Manager routines Color2Index and SetEntries (code example below.) This method of color cycling is frowned upon by the User Interface Thought Police because it is device dependent.

{1}

procedure colorCycle (oldRGB, newRGB : RGBColor);
 var
 newColor : ColorSpec;
 colorPtr : ^CSpecArray;
 devIndex : Integer;
 begin
 devIndex := Color2Index (oldRGB);
 newColor.rgb := newRGB;
 colorPtr := @newColor;
 SetEntries (devIndex, 0, colorPtr^);
 end;

The Palette Manager

The primary function of the Palette Manager is arbitrating between competing demands when the number of colors is limited. It does this well, and every color application should use it. The second function is device independent color table animation, which has problems and is described in the next section.

The types of Palette Manager color reflect this division. Courteous and Tolerant colors are for applications working in RGB space, where you specify a color and let the hardware handle the details. Animated and Explicit colors are for applications that work with pixel values and CLUTs.

A palette is a set of colors assigned to a window. When there are not enough colors in the device CLUT to satisfy all the visible windows, the Palette Manager gives priority to the frontmost window and then works back. Every time a new window, including dialogs and alerts, is brought forward the Palette Manager reshuffles the priorities and if necessary alters the device CLUT, causing those distracting changes in the background.

Even a window without a palette can cause a change in the device CLUT on a 16 color system, which is quite irritating to watch. As per Tech Note #211 you can avoid this by setting up an application default palette regardless of whether you use color or not.

 data ‘pltt’ (-1) {
 $”0002 0000 0000 0000 0000 0000 0000 0000"
 $”FFFF FFFF FFFF 0002 0000 0000 0000 0000"
 $”0000 0000 0000 0002 0000 0000 0000 0000"
 };

The Palette Manager doesn’t know about ‘floating’ windows such as tearoff menus, so you have to do some palette managing of your own. When a document is brought to the ‘front’, make its palette the application default or share it with the true front window.

{2}

procedure mySelectWindow (w : WindowPtr);
 var
 p : PaletteHandle;
 begin
 p := GetPalette (w);
 ...
 SetPalette (WindowPtr(-1), p, true);

OR

{3}

 SetPalette ( the top floater, p, true);
 ActivatePalette (the top floater );

For some bizarre reason the Palette Manager is present on all Macs, not just the Mac II, so you don’t have to worry about compatibility.

The Palette Manager chapter says that PmForeColor and PmBackColor should be used in place of the regular RGBForeColor and RGBBackColor. For applications working in RGB space (Courteous and Tolerant) this is not necessary. The Palette Manager will not set up duplicate entries for a single color, so the final pixel value will always be the same no matter which you call. My opinion is that it is better to be consistent and always use the RGB calls.

Palettes are assigned to windows but not GrafPorts, which is awkward if you want to draw offscreen using a different set of colors. One solution is to temporarily reassign the front window palette, but this can cause the screen to change for no apparent reason. You could set up your own GDevice, but this is excessive. The best way is to use a custom search proc which returns the pixel values you want. More on this later.

The Dark Side of the Palette Manager

The Palette Manager can, according to Inside Macintosh V, be used for device independent color table animation. True, but it turns out that the Palette Manager has two serious limitations:

• You cannot animate a PICT

• You cannot maintain an offscreen copy

When you activate a palette of animated colors, the Palette Manager reserves entries in the device CLUT for your exclusive use. Reserved entry indexes are never returned by Color2Index, RGBForeColor, etc; so the color cannot be used by another application (or even another window unless it shares the palette.) This would be fine, except that you cannot use those routines either! The only way to draw with those pixel values is through PmForeColor and PmBackColor, period.

PICTs and Animated Colors

This is why you cannot animate a PICT. The PICT works in RGB space and therefore calls RGBForeColor. Your animated colors are protected against this, so are ignored. If there are free colors elsewhere in the device table, these will be used instead. The PICT will be drawn in color, but the colors do not correspond to the entries in your palette so animating the palette has no effect. If there are not any free colors, quite likely on a 4 or 16 color card, the PICT comes out in black and white. Either way, you are up the creek.

Can you draw the PICT with Tolerant colors and then change them to Animated? Sorry, no. The Palette Manager does some kind of least recently used analysis to select device CLUT entries for animation, so tries as hard as possible not to reserve the already existing entry. On a 4 or 16 color card, it will probably have to reserve one of your Tolerant colors, but not necessarily the right one.

Can you force Quickdraw to draw with your Animated colors? Yes, with a custom search proc, but it is messy. More on this later.

Pixmaps and Animated Colors

Now for offscreen pixmaps. Here the Palette Manager works fine in isolation, but can’t cope with the real world. Suppose you activate a palette of animated colors, draw your image using PmForeColor and PmBackColor, then create an offscreen pixmap and CopyBits the image to it. What happens when you CopyBits back?

It works for a while. The offscreen pixmap has a copy of the device CLUT with the same seed, so CopyBits just transfers the pixel values directly. AnimateEntry and AnimatePalette have been especially written by Apple to leave the device seed unchanged, because it is the pixel values that should match between the source and destination, not the RGB colors. Even if the onscreen image has been animated since the offscreen copy was created, it will be drawn correctly with the current palette RGB colors.

Unfortunately you can upset this happy state of affairs in many ways: changing the screen depth, moving the window to another screen, switching to another application under MultiFinder, choosing a new highlight color from the Control Panel. All of these may change the device CLUT and seed. (To be fair to Apple, I understand that the Control Panel has been fixed, and there is nothing they could do about the window moving to another monitor anyway.)

As soon as the device CLUT changes, the whole scheme is kaput. As described earlier, if the seeds of the source and destination pixmap CLUTs don’t match, CopyBits translates the pixel values from the source to the destination. The translation uses the same color matching algorithm as RGBForeColor, and likewise it ignores animated colors. Once again, your image is either translated to a different set of colors or becomes black and white.

Can you avoid this by setting the seeds equal? The animating colors are reserved for your application, so in theory those pixel values are unchanged. Again, no. The offscreen pixmap was built using pixel values from one particular device, so if the window has been moved to another they almost certainly will not match. Even on the same device it doesn’t always work. When the device CLUT changes, the Palette Manager may reassign the pixel values for colors in the palette. The pixel values in the offscreen pixmap are no longer valid.

In short, you can only maintain an offscreen copy of Animated colors as long as the device and device CLUT are held constant, and in today’s world of multiscreen, MultiFinder equipped Macintoshes this is hardly practical.

Solutions

Since it is equally impractical to write a bitmapped color graphics application such as Fire-Brigade without using offscreen pixmaps or PICTs, what can a programmer do? For Fire-Brigade, I tried three solutions which did work, as well as countless ones that didn’t.

First, I wrote a custom search proc which forced Quickdraw and CopyBits to recognize animating colors. Unfortunately this ruins the performance of CopyBits for small images, so had to be abandoned.

Next I tried redrawing the offscreen pixmaps whenever the device CLUT changed to keep them up to date. Since there were 200K of PICTs to draw, it took about 20 seconds to resume from a MultiFinder switch and had to be abandoned.

The final solution was to forget about the Palette Manager for animation. Instead Fire-Brigade uses the Palette Manager for what it is good at: making sure that our palette of Tolerant colors is available when we need it. Onscreen color cycling is done directly through the Color Manager as described above.

Locating Devices

To animate colors directly you need to know which device the window is on. Calling GetGDevice doesn’t work, because as far as I can tell it always returns the first device in the list regardless of the current port. The easiest way is to convert the windows portRect into global coordinates and call GetMaxDevice for that rect, on the assumption that very few windows spread over more than one monitor. If you want to be absolutely safe, convert the portRect into global coordinates and then calculate the intersection of this rect with each device^^.gdRect in turn.

(I also found that although GrafPorts are opened on the current device, windows are not. When I changed the device with SetGDevice before creating a window, the title bar and frame appeared on one monitor and the contents on the other! The correct way to put a window on a particular device is to calculate the window bounds rect as usual and then offset it by device^^.gdRect.topLeft.)

Custom Search Procs

Several times I have mentioned custom search procs. A search proc in Color Quickdraw translates an RGB color into an actual CLUT index value for a particular device. Custom search procs, described in the Color Manager chapter of Inside Mac, allow you to override the standard behavior when necessary.

A simple and useful proc is one that matches colors for offscreen drawing. If you want to save a large offscreen image which you know uses only a few colors, setting the offscreen pixmap depth to 2 or 4 saves a considerable amount of memory. Because you want to draw with the pixel values in the offscreen CLUT, not the device CLUT, install this search proc or something similar:

{4}

varoffscreenColors : CTabHandle; { Shared with pixmap }
...
function offscreenPixel (target : RGBColor; var pixel : LongInt):Boolean;
 var index : Integer;
 begin
 offscreenPixel := false; { In case we can’t match }
 with offscreenColors^^ do
 begin
 for index := 0 to ctSize do
 begin
 if (ctTable[index].red = target.red)
 and (ctTable[index].green = target.green)
 and (ctTable[index].blue = target.blue) then
 begin
 pixel := index;
 offscreenPixel := true;
 leave;
 end; { if }
 end; { for }
 end; { with }
 end; { offscreenPixel }

You only need to install this search proc when the image is first drawn, not when copying from it to the screen.

You can also write a custom search proc that recognizes animated colors. The easy way is just to search every entry in the device CLUT and return the index of the best match, regardless of whether it is reserved or not. This can give the wrong result under certain circumstances, because Animated colors, unlike Tolerant or Courteous, may have duplicates elsewhere in the device CLUT.

To be safe you should check if the target color is one in your palette, and if so return the pixel value for that entry. To do this you have to build your own lookup table. The pixel value is encoded in some way in the ciPrivate field of a palette entry record, but since the reason for using the Palette Manager is to avoid compatibility problems you shouldn’t touch it. Instead, open a CGrafPort and call PmForeColor for each palette entry in turn. Calling PmForeColor will set the rgbFgColor field of the port to the RGB color and the fgColor field to the actual pixel value. Store fgColor in your private lookup table and go on to the next.

Custom Quirks

The two things you have to remember with custom search procs are firstly that they are a shared resource, and secondly that they add an overhead to CopyBits.

Custom search procs are assigned to devices, not applications. Under MultiFinder this means that a background application may try to call RGBForeColor, which in turn calls your custom search proc, which bombs because it can’t find your global variables. Even if your search proc is self contained you shouldn’t risk other applications calling it. Make sure the code that calls AddSearch doesn’t call GetNextEvent until after a corresponding DelSearch. Horrible things happen if your application finishes but leaves a custom search proc installed.

The Color Manager chapter in Inside Macintosh V mentions ‘client ids’ , but these are only useful for search procs which are installed on more than one device. The client id can distinguish which device is calling the search proc, but because the id is also a shared resource you can’t arrange for it to be unique to your application.

Installing a custom search proc will add a certain constant overhead to CopyBits which depends on the depth of the source. If CopyBits needs to build a translation table to remap pixel values from the source to a device with a search proc, it has to call that search proc once for each entry in the source CLUT. Copying from a 4 bit pixmap means 16 calls, which is noticeably slower but not too bad; 8 bits means 256 calls which is just awful. A tightly coded search proc is no real improvement over a sloppy one - it is the number of calls that drags performance down, not the individual searches.

Color Diagnosis

A CLUT viewer of some sort is essential for working in color. The March 1988 issue of MacTutor describes a DA called Chroma which displays all the useful device and device color table values. I use a cut down version which just shows the colors in the CLUT and the seed value. Inside Mac V, Palette Manager chapter, Explicit Color section gives an outline of how to write it.

You should test your application with different screen depths. With 256 colors there is lots of room and the Color/Palette Managers are not worked very hard. Sixteen colors is much more stressful and reveals the problems faster. Of course, you still need to test at least briefly under 256 colors to avoid nasty surprises like the increased CopyBits overhead with search procs, and ideally you want to try it under 32 bit Quickdraw as well. (Ack! This is getting as bad as the IBM PC.)

Conclusion

During the weeks I spent trying to get the animation in Fire-Brigade to work, anyone fool enough to ask “How’s it going?” got 30 minutes of me calling down fire and brimstone upon the entire population of Cupertino, California. Since then I’ve mellowed a bit. I’m still irritated that the animation features of the Palette manager are useless for real applications, but I also think it doesn’t matter. By the end of 1990 onscreen color cycling should be obsolete and those parts of the Palette Manager dead, kept only for upward compatibility.

Color cycling is and will remain an important technique for image manipulation, and offscreen color cycling works beautifully on the Mac II. Onscreen color cycling, though, is something of an anachronism. It is only worthwhile when the CPU cannot redraw the screen quickly, and only possible when RAM costs too much for direct color to be used. Neither of these is true for the Mac II, so the successor to Fire-Brigade will do all its color cycling offscreen.

• Work in RGB color space, not with pixel values. It is the only way to survive multiple screens and 32 bit Quickdraw.

• Color Quickdraw tries to give you the best results it can under all circumstances. Resist the temptation to interfere for ‘efficiency’ - you usually make things worse.

• Test with 4 or 16 colors, and do lots of MultiFinder switching with other color applications active.

• The Palette Manager is your friend.

• Every application should have a default palette.

• Use palettes of Courteous/Tolerant colors if you want a particular set of colors onscreen.

• Use a custom search proc if you want a particular set of colors offscreen.

• Don’t use PmForeColor and PmBackColor for normal drawing.

• Avoid Animated colors like the plague!

• If you use a custom search proc, don’t leave it lying around.

• Make sure you test the performance of custom search procs under 256 colors.

• Offscreen CLUTs need a unique ctSeed if they are to be color cycled.

• Don’t force ctSeeds to be equal for CopyBits.

• If you want to cycle colors, do so offscreen in your own pixmaps and let CopyBits translate it to the screen.

Acknowledgements

A great many people helped me with information about Color Quickdraw and the Palette Manager, in particular Brett Adams and the Canberra office of Apple Computer. Thank you all very much.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
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
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.