TweetFollow Us on Twitter

Graphical Truffles - The Palette Manager Way

Graphical Truffles

The Palette Manager Way

Edgar Lee And Forrest Tanaka

No part of the Macintosh graphics environment is more feared, hated, or misunderstood than the Palette Manager. The Developer Support Center gets many questions about it from people who don't have any idea how to get it to do what they want. We've seen many people just give up on the Palette Manager completely and instead use lower-level routines that are much more difficult to use but easier to understand quickly.

The Palette Manager is actually very simple. It has no complicated heuristics that only rocket scientists can understand. In this column, we'll show how the Palette Manager gets its job done, and we'll talk about a couple of issues that you'll have to deal with to make your palettes do what you want them to do. You'll see that the Palette Manager is both easy to understand and a very useful part of the Macintosh Toolbox.

Before you read this column, it would be a good idea to read the Palette Manager chapter (Chapter 20) ofInside MacintoshVolume VI, which lays down the terminology that we'll use here.

The critical job that the Palette Manager does is activate a palette. This happens whenever you call SetPalette or ActivatePalette for the frontmost window and whenever a window that has a palette is activated. When a palette is activated, the Palette Manager loads the palette colors into the screen's color table. How it goes about doing this is determined by the usage mode of each entry in the palette.

You indicate an entry's usage mode by setting a flag in its usage field. There are four usage modes: pmCourteous, pmTolerant, pmAnimated, and pmExplicit. You can choose a separate usage mode or combination of usage modes for each entry in a palette, or you can give all the entries the same usage mode. Let's take a look at what each usage mode is good for and what effect each one has when a palette is activated.

pmCourteous. The pmCourteous usage mode enables you to replace RGBColor records in your code with single integers. Thus, having a palette of courteous colors gives you an alternative way to specify foreground and background colors. This is great for localizers who might need to change the colors in your program to something more meaningful in other countries, and it's great for you if you feel like changing a color without recompiling.

Activating a palette of courteous colors simply tells the Palette Manager to use your window's palette as a sort of lookup table. When your window is the current port and you call PmForeColor or PmBackColor with a palette index, the Palette Manager simply retrieves the color in your window's palette at that index and uses it for any subsequent drawing to that window. Courteous colors never change the screen's color table -- they get mapped to the closest colors already available there.

Here's an example: Without the Palette Manager, you would draw a green oval in a window by setting up an RGBColor record with a red component of 0, a green component of 65,535, and a blue component of 0 and passing this record to RGBForeColor; then you would call FrameOval. To change the oval to blue, you would modify your RGBColor record in your source code and recompile your program. Now, suppose instead you brought in the Palette Manager by setting up a palette resource (resource type 'pltt') that contained one courteous entry -- green -- with an index of 0. In your code, you would call PmForeColor(0) instead of RGBForeColor with green. When you called FrameOval, the oval would be drawn in green. To change the color to blue, you would just use ResEdit to modify the green entry in your 'pltt' resource to blue. So calling PmForeColor for a courteous palette entry is just like calling RGBForeColor, but instead of supplying the RGB components, you simply pass an index into the palette, where the index points to the color's RGB components (see Figure 1).

pmTolerant. The pmTolerant usage mode is used when you want to be sure that a specific set of colors is available to the screens that your window is on. It's a bummer to draw a rainbow in a window on a screen that another application has removed all the greens and yellows from. You need a palette of tolerant colors to assert your application's right to the colors it needs to display its images optimally. With such a palette, you can change the colors in a screen's color table to ones that you want.

Before we look at how a palette of tolerant colors is activated, realize that Color QuickDraw always wants white in the first entry of a screen's color table and black in the last. To enforce this rule, Color QuickDraw protects these two entries from being changed to other colors. That means a palette of tolerant colors can change all the colors of a screen's color table except two.

When a palette of tolerant colors is activated, the Palette Manager checks each entry in the palette and associates it with an entry in the screen's color table. Let's say we have a palette with three entries -- bright green, black, and dark yellow -- attached to a window on a 16-color screen. All three palette entries are tolerant, with a tolerance of 0. The Palette Manager does the following: 1. It checks the first entry in the palette, bright green, and searches the screen's color table for the same bright green. It finds that color near the middle of the color table, and so associates palette entry 0 with this existing bright green entry in the color table. 2. It searches the screen's color table for the second entry in the palette, black. It finds it at the very end, so palette entry 1 corresponds to entry 15 of the color table. 3. It searches the screen's color table for dark yellow. There isn't one, so it chooses a color table entry to change to dark yellow. It can't choose the black or the white entry because they're protected and can't be changed, and it can't change the bright green entry because that entry is already associated with entry 0 of the palette. So it chooses one of the other color table entries and changes it to dark yellow.

[IMAGE Graphics_column_rev1.GIF]

Figure 1 Alternative Ways to Specify a Foreground Color

Because the color table has been changed, the Palette Manager makes sure that other windows are redrawn, just in case they were drawn using the color table entry that was changed to dark yellow. It does this by sending update events to all windows as soon as the palette is activated. If no color table changes were needed, the Palette Manager doesn't bother doing this.

Once our three-entry palette has been activated, we can call PmForeColor, passing it 0, 1, or 2 to draw objects in bright green, black, or dark yellow, respectively. In fact, we could call RGBForeColor, passing it bright green, black, or dark yellow RGBColor records, and they would use the same colors that our palette loaded into the screen's color table. Figure 1 applies to palettes of tolerant colors as well as palettes of courteous colors. If there are more palette entries than will fit in the screen's color table, the Palette Manager associates each palette entry with a color table entry until no more color table entries are available and then interprets the rest of the palette entries as courteous. For example, let's say a 20-entry palette is activated on a 16-color screen, where each palette entry is pmTolerant with a tolerance of 0 and neither black nor white is in the palette. Beginning with the first palette entry, the Palette Manager associates each entry with a color table entry. The 15th palette entry can't be associated with any color table entry because the black and white entries are protected from changes and all 14 other entries have already been associated with palette entries. So the 15th palette entry and all entries beyond it are simply treated as courteous colors.

pmAnimated. On indexed devices, the pmAnimated usage mode is used to do color table animation, which gives you smooth, fast visual effects simply by changing the colors in your screen's color table very quickly. You don't have to redraw anything to see this animation; you just use the Palette Manager to change the interpretation of the colors of your existing image. This is great for games and fast controls for image processing applications. On direct devices, animated entries are treated as courteous entries.

Like pmTolerant entries, each pmAnimated palette entry is associated with an entry in a screen's color table when the palette is activated, and the colors in the palette are put into the screen's color table. But changing color table entries for color animation changes everything on the screen that uses those same color table entries, like the desktop or window frames. That's usually not what we want, so the Palette Manager forces everything outside the window to be redrawn without the colors that are being used for color animation -- those colors are off limits. In fact, the only way to use those colors is to call PmForeColor or PmBackColor for an animated palette entry and then draw some QuickDraw object. Remember, the big difference between tolerant and animated colors is that color table entries that are used for tolerant colors can be used by anyone, but animated color table entries are used only by objects drawn in the palette's window after a call to PmForeColor or PmBackColor.

Let's use our three-entry palette as an example again, but this time assume that each entry is animated. The Palette Manager first takes the bright green entry, picks a color table entry on the screen, and changes it to bright green. It doesn't matter if there's already a bright green in the color table. As usual, the Palette Manager avoids the black and white entries at either end of the color table. It then picks another color table entry and puts black into it, and does the same for the dark yellow entry. If you call PmForeColor(0) and draw an object, it's drawn in bright green. But if you call RGBForeColor for bright green and draw an object, it doesn't use the bright green that's been defined as animated. Instead, it uses the closest color to bright green available, aside from any color table entries that have been defined as animated.

pmExplicit. The pmExplicit usage mode is rarely used alone, and there's not much to it beyond what's described in the Palette Manager chapter ofInside MacintoshVolume VI. We'll discuss in the next section the more interesting case of using pmExplicit along with the other usage modes.

When attaching a palette to a window, the Palette Manager works in a way that affects whether you should store black and white in the palette. We'll outline the way it works in two different categories. The first category applies to palettes containing the same number of entries as the screen's color table, and the second category applies to palettes containing fewer entries than the screen's color table.

Same number of entries in palette and color table. If the palette contains the same number of entries as the screen's color table, black and white should be stored in the palette. If these two entries aren't stored in the palette, the Palette Manager will ignore two entries in the palette when loading the palette colors into the screen's color table, to avoid overwriting the color table's black and white entries. The Palette Manager will decide which palette entries to ignore based on the usage field for each palette entry.

As an example, let's take the case of a palette all of whose entries are defined as pmTolerant + pmExplicit. Because the pmExplicit flag tells the Palette Manager to store each palette entry in its respective index in the screen's color table, the choice of which palette entry to ignore is fairly straightforward. The colors stored at the first and last entry of the palette correspond to the protected entries in the screen's color table, so these entries will be ignored.

In the case of a palette containing entries not defined with the pmExplicit flag set, the decision of which two palette colors to ignore can seem somewhat random. This is because the decision is based on the current distribution of the palette entries in the screen's color table, where the distribution is derived from the tolerance values of the palette entries and the existing colors in the screen's color table before the palette was activated.

For example, suppose we have a 16-entry palette and a 4-bit screen color table as shown in Figure 2 (we've used the default color table). Figure 3 shows how the screen's color table will look in two different cases when the palette of the frontmost window has been activated. In these figures, the explicit entries are distributed sequentially in the screen's color table, whereas the nonexplicit entries are scattered throughout the color table.

[IMAGE Graphics_column_rev2.GIF]

Figure 2 An Example Palette and Color Table

[IMAGE Graphics_column_rev3.GIF]

Figure 3 The Color Table With the Palette of the Frontmost Window Active

For the explicit entries, we see that the first and last entries of the palette are not loaded into the screen's color table, to protect the color table's white and black entries. However, for the nonexplicit entries, the two palette colors ignored aren't necessarily the first and last entries of the palette. When determining where the nonexplicit palette entries should be stored in the color table, the Palette Manager first checks to see which colors in the screen's color table already match those in the palette. If there's a match within the specified tolerance, that palette entry is stored at the index of the matching color in the screen's color table.

And one other thing: When multiple nonexplicit palette entries match (within the specified tolerance) the same color in the screen's color table, all those palette entries are stored at the same index in the color table. This means that only one slot in the color table is needed rather than as many slots as there are palette entries.

Fewer entries in the palette. Now, if the window's palette contains fewer entries than the screen's color table, the palette entries' usage field plays a large part in determining whether black and white should be included in the palette. The reason for this is similar to the previous case for nonexplicit entries.

[IMAGE Graphics_column_rev4.GIF] Figure 4 Another Example Palette and Color Table

[IMAGE Graphics_column_rev5.GIF]

Figure 5 The Color Table With the Palette of the Frontmost Window Active

If all the entries in a palette are defined without the pmExplicit flag set, the presence of black and white in the palette isn't as critical, since the palette entries will likely be scattered throughout the screen's color table while avoiding the protected white and black colors stored in the first and last slots of the screen's color table. Since there are fewer palette entries than color table entries, we needn't worry about palette entries getting ignored. So in this case, creating a palette without a black entry or a white entry is perfectly fine as long as there are enough slots in the screen's color table to hold all the palette entries and the two protected colors.

However, if the palette entries are all defined with the pmExplicit flag set, there's a good chance that one of the palette's entries will be ignored. And the palette entry that does get ignored will usually be the first entry in the palette, because this entry shares the same index as the protected white entry in the screen's color table.

For example, suppose we have a 192-entry palette and an 8-bit screen color table as shown in Figure 4 (again, we've used the default color table). Figure 5 shows how the screen's color table will look in two different cases when the palette of the frontmost window has been activated.

Again, the explicit entries are distributed sequentially in the screen's color table, starting with the first entry in the color table. Because the first entry in the screen's color table is protected from being overwritten, the first entry in the palette is ignored. But in the nonexplicit case, the entries are distributed somewhat differently. Depending on what colors are already in the screen's color table, the nonexplicit entries can be stored anywhere throughout the color table. And in this example, since there are clearly more slots in the screen's color table than needed by the palette entries, all the colors in the palette appear in the color table; none are ignored. So again in this case, including black and white in the palette really isn't necessary.

We've seen how the way the Palette Manager works can affect whether you decide to store black and white in your palette. In all the instances we mentioned, the positions of the black entry and white entry were always the same: white first and black last. However, in certain cases, you may not want to position white first and black last.

In the case where you'd like to create just one palette to handle devices at multiple bit depths, the black and white entries should be stored as the first two colors in the palette. This ensures that the two colors used on a 1-bit device are present. Likewise, to ensure that the optimal colors are used at depths 2, 4, and 8, we do the same thing for each additional depth. We store the preferred colors at the appropriate position in the palette. Figure 6 shows how a typical palette could be configured to handle multiple bit depths.

In our sample palette, the first 16 colors are defined as shades of gray, because we've decided our window would look best when displayed in grayscale on a 1-, 2-, or 4-bit device. For the 1-bit and 2- bit devices, we simply choose the appropriate shades for those depths and store them in the first four slots of our palette. But for an 8-bit device, we include as many colors as we can for the optimal display at that depth. For this example, we added the nongrayscale colors from the standard 8-bit color table to the remaining slots in our palette. Because the Palette Manager only uses the maximum number of colors it can (starting at the first index in the palette) for a specific bit depth, only the colors we want shown will be shown. Also, because the placement of the colors determines which colors are available at a certain depth, all the palette entries must be defined as explicit entries.

[IMAGE Graphics_column_rev6.GIF] Figure 6 A Palette Configured to Handle Multiple Bit Depths

Another way of ensuring that certain palette entries are available at certain depths is to apply the inhibit usage categories to the palette entries. These inhibit constants tell the Palette Manager which entries are available under the current color environment. Depending on which inhibit constant is used, the palette entries can be inhibited from a specific bit depth and from a color or grayscale device. So by combining various inhibit constants to our sample palette, we can inhibit the colors outside the current depth's range from being used. In our example, if entry number 16 were defined with

pmInhibitC2 + pmInhibitC4 + pmInhibitG2 + pmInhibitG4

this entry would be available only on an 8-bit or deeper color or grayscale device.

The Palette Manager works very simply, but it has so many options and effects that it can seem complicated. By understanding how the Palette Manager makes its decisions, you should find it easy to figure out how to make it do precisely what you want. We hope this column has made this clear, so that you can use the Palette Manager and avoid fussing with the alternatives.

EDGAR LEE (AppleLink EDGAR) Before Edgar's dog, Sunny, departed for the East Coast, we asked her if she could tell us a little about him. Here's what she had to say: "Edgar . . . is that his name? Oh yeah, nice human. A little hairless for my taste, but a good guy. He works over there in DTS or something. He used to come home late all the time. At first I thought he was seeing another dog, then I realized he's just a nerd. And how is he to me? Well, let's see, he takes care of me, entertains me. I bark once, he feeds me; I bark twice, we go out for a walk. Not bad for an owner; I've heard worse stories. Does he ever get upset with me? I suppose at times he does. I probably deserve it; carpet cleaning isn't cheap, you know. But hey, I see a clean spot, I go for it."*

FORREST TANAKA (AppleLink TANAKA) has spent the last couple of months learning how to be a domestic kind of guy. Once worried about paying the rent, he's now worried about paying the mortgage. Once worried about his downstairs neighbors, he's now worried about getting the best fertilizer. Now he's even the stepparent of an old dog and a cat with an attitude. As a final blow to his carefree days of youth, he has to mow the lawn! *

Indexed and direct devices are discussed in the Graphics Overview chapter (Chapter 16) of Inside Macintosh Volume VI.*

The inhibit constants are discussed in the Palette Manager chapter (Chapter 20) of Inside Macintosh Volume VI.*

Thanks to Bill Guschwan, Shannon Holland, Guillermo Ortiz, Konstantin Othmer, Brigham Stevens, and John Wang for reviewing this column. Special thanks to Joseph Maurer and Faith Pai. *


Community Search:
MacTech Search:

Software Updates via MacUpdate

jAlbum Pro 12.6.4 - Organize your digita...
jAlbum Pro has all the features you love in jAlbum, but comes with a commercial license. With jAlbum, you can create gorgeous custom photo galleries for the Web without writing a line of code!... Read more
jAlbum 12.6.4 - Create custom photo gall...
With jAlbum, you can create gorgeous custom photo galleries for the Web without writing a line of code! Beginner-friendly, with pro results Simply drag and drop photos into groups, choose a design... Read more
Microsoft Remote Desktop 8.0.16 - Connec...
With Microsoft Remote Desktop, you can connect to a remote PC and your work resources from almost anywhere. Experience the power of Windows with RemoteFX in a Remote Desktop client designed to help... Read more
Spotify - Stream music, create...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
djay Pro 1.1 - Transform your Mac into a...
djay Pro provides a complete toolkit for performing DJs. Its unique modern interface is built around a sophisticated integration with iTunes and Spotify, giving you instant access to millions of... Read more
Vivaldi - Lightweight browser...
Vivaldi browser. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind that users are... Read more
Stacks 2.6.11 - New way to create pages...
Stacks is a new way to create pages in RapidWeaver. It's a plugin designed to combine drag-and-drop simplicity with the power of fluid layout. Features: Fluid Layout: Stacks lets you build pages... Read more
xScope 4.1.3 - Onscreen graphic measurem...
xScope is powerful set of tools that are ideal for measuring, inspecting, and testing on-screen graphics and layouts. Its tools float above your desktop windows and can be accessed via a toolbar,... Read more
Cyberduck 4.7 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Labels & Addresses 1.7 - Powerful la...
Labels & Addresses is a home and office tool for printing all sorts of labels, envelopes, inventory labels, and price tags. Merge-printing capability makes the program a great tool for holiday... Read more

Discover Your Reflexes With Minimalist G...
Discover O, BYOF Studios, may look simple at first with its' color matching premise and swipe controls, but as you speed up the task becomes more daunting. [Read more] | Read more »
Here's Another Roundup of Notable A...
Now that the Apple Watch is publically available (kind of), even more apps and games have been popping up for it. Some of them are updates to existing software, others are brand new. The main thing is that they're all for the Apple Watch, and if you... | Read more »
Use Batting Average and the Apple Watch...
Batting Average, by Pixolini, is designed to help you manage your statistics. Every time you go to bat, you can use your Apple Watch to track  your swings, strikes, and hits. [Read more] | Read more »
Celebrate Studio Pango's 3rd Annive...
It is time to party, Pangoland pals! Studio Pango is celebrating their 3rd birthday and their gift to you is a new update to Pangoland. [Read more] | Read more »
Become the World's Most Important D...
Must Deliver, by cherrypick games, is a top-down endless-runner witha healthy dose of the living dead. [Read more] | Read more »
SoundHound + LiveLyrics is Making its De...
SoundHound Inc. has announced that SoundHound + LiveLyrics, will be one of the first third-party apps to hit the Apple Watch. With  SoundHound you'll be able to tap on your watch and have the app recognize the music you are listening to, then have... | Read more »
Adobe Joins the Apple Watch Lineup With...
A whole tidal wave of apps are headed for the Apple Watch, and Adobe has joined in with 3 new ways to enhance your creativity and collaborate with others. The watch apps pair with iPad/iPhone apps to give you total control over your Adobe projects... | Read more »
Z Steel Soldiers, Sequel to Kavcom'...
Kavcom has released Z Steel Soldiers, which continues the story of the comedic RTS originally created by the Bitmap Brothers. [Read more] | Read more »
Seene Lets You Create 3D Images With You...
Seene, by Obvious Engineering, is a 3D capture app that's meant to allow you to create visually stunning 3D images with a tap of your finger, and then share them as a 3D photo, video or gif. [Read more] | Read more »
Lost Within - Tips, Tricks, and Strategi...
Have you just downloaded Lost Within and are you in need of a guiding hand? While it’s not the toughest of games out there you might still want some helpful tips to get you started. [Read more] | Read more »

Price Scanner via

Zoho Business Apps for Apple Watch Put Select...
Pleasanton, California based Zoho has launched Zoho Business Apps for Apple Watch, a line of apps that extends Zoho business applications to let users perform select functions from an Apple Watch.... Read more
Universal Stylus Initiative Launched to Creat...
OEMs, stylus and touch controller manufacturers have announced the launch of Universal Stylus Initiative (USI), a new organization formed to develop and promote an industry specification for an... Read more
Amazon Shopping App for Apple Watch
With the new Amazon shopping app for Apple Watch, Amazon customers with one of the wearable devices can simply tap the app on the watch to purchase items in seconds, or save an idea for later. The... Read more
Intel Compute Stick: A New Mini-Computing For...
The Intel Compute Stick, a new pocket-sized computer based on a quad-core Intel Atom processor running Windows 8.1 with Bing, is available now through Intel Authorized Dealers across much of the... Read more
Heal to Launch First One-Touch House Call Doc...
Santa Monica, California based Heal, a pioneer in on-demand personal health care services — will offer the first one-touch, on-demand house call doctor app for the Apple Watch. Heal’s Watch app,... Read more
Mac Notebooks: Avoiding MagSafe Power Adapter...
Apple Support says proper usage, care, and maintenance of Your Mac notebook’s MagSafe power adapter can substantially increase the the adapter’s service life. Of course, MagSafe itself is an Apple... Read more
12″ Retina MacBook In Shootout With Air And P...
BareFeats’ rob-ART morgan has posted another comparison of the 12″ MacBook with other Mac laptops, noting that the general goodness of all Mac laptops can make which one to purchase a tough decision... Read more
FileMaker Go for iPad and iPhone: Over 1.5 Mi...
FileMaker has announced that its FileMaker Go for iPad and iPhone app has surpassed 1.5 million downloads from the iTunes App Store. The milestone confirms the continued popularity of the FileMaker... Read more
Sale! 13-inch 2.7GHz Retina MacBook Pro for $...
 Best Buy has the new 2015 13″ 2.7GHz/128GB Retina MacBook Pro on sale for $1099 – $200 off MSRP. Choose free shipping or free local store pickup (if available). Price for online orders only, in-... Read more
Minimalist MacBook Confirms Death of Steve Jo...
ReadWrite’s Adriana Lee has posted a eulogy for the “Digital Hub” concept Steve Jobs first proposed back in 2001, declaring the new 12-inch MacBook with its single, over-subscribed USB-C port to be... Read more

Jobs Board

*Apple* Client Systems Solution Specialist -...
…drive revenue and profit in assigned sales segment and/or region specific to the Apple brand and product sets. This person will work directly with CDW Account Managers Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Support Technician IV - Jack Henry a...
Job Description Jack Henry & Associates is seeking an Apple Support Technician. This position while acting independently, ensures the proper day-to-day control of Read more
*Apple* Client Systems Solution Specialist -...
…drive revenue and profit in assigned sales segment and/or region specific to the Apple brand and product sets. This person will work directly with CDW Account Managers Read more
*Apple* Software Support - Casper (Can work...
…experience . Full knowledge of Mac OS X and prior . Mac OSX / Server . Apple Remote Desktop . Process Documentation . Ability to prioritize multiple tasks in a fast pace Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.