TweetFollow Us on Twitter

Palette Manager Animation

Palette Manager Animation

Rich Collyer

In the last several weeks many of you have asked "How do I animate colors with the Color Manager?" I usually answer, "The Color Manager is not a good way to animate colors. Try the Palette Manager instead." I figure that for every person who asks this question there are a hundred others out there trying to figure out the answer by themselves. This article is for all of you independent types who never ask questions but could use the answer just the same.

This article comes with a sample: GiMeDaPalette. The article shows how to do color table animation by using the Palette Manager, and by the end of the article I think you'll be convinced that using the Palette Manager is the only way to fly.

To see the animation effect, you will need to run the sample on a monitor (device) that uses a color look-up table, often called a clutType device; you need a color look-up table to do color table animation.

The sample is designed to run only under System 6.0.5 or greater, or when 32-Bit QuickDraw is installed; the Palette Manager shows its abilities best in these environments. This limitation may discourage those who want their applications to work on all systems and hardware configurations, but remember that if you stick to 32-Bit QuickDraw or 6.0.5, you'll be able to take advantage of the upgrades and improvements that Apple provides. If you don't, your application is likely to stagnate. One possible way to work around this dilemma is to separate your code into a pre-32-Bit QuickDraw version and a 32-Bit QuickDraw version. This will make your application more complex, but it will give you the flexibility to work under most, if not all, color systems.

When you run the sample application, you will find that there is a File menu, which just allows you to quit, and a Palette menu that allows you to pick the color usage of the palette. The sample initializes itself to use a palette with the color usage Courteous.

When you want to animate the palette, you will want to select one of the menus that contains the word animated. (Animated, Tolerant + Animated, Explicit + Animated, Tolerant + Explicit + Animated).

A LOOK AT THE SAMPLE

The three major parts of the sample:
  • Setting up the color environment
  • Picking a color for drawing
  • Animating the colors will be needed in most applications that require color table animation.

SETTING UP THE COLOR ENVIRONMENT
The four main lines of code that I used to set up the environment in GiMeDaPalette are

mycolors = GetCTable(clutID);

(*mycolors)->ctFlags |= 0x4000;

srcPalette = NewPalette(numcolor, mycolors, pmCourteous, 0);

SetInhibited(pmCourteous); 

Since this sample is a more general use of the Palette Manager, I started by building a palette that is Courteous. This means that you get what colors the system can give you with the best matches it can make, but the Palette Manager is not going to change any of the colors in the environment to give you what you want. The palette is built with a color table that is stored as a resource and retrieved with the trap call to GetCTable. The manipulation to the ctFlags is described in a sidebar, "Other Features of the Palette Manager," later in this article; basically the manipulation makes it possible to use the colors in an offscreen world. The palette has 256 colors (numcolor = 256) and the tolerance is set to zero. The tolerance value tells the Palette Manager how close the color match needs to be, but unless you have the usage set to pmTolerant, it is ignored. For more information on these calls, see the Palette Manager chapters in Inside Macintosh (volume V, chapter 7, and volume VI, chapter 20). My function SetInhibited, is described in the sidebar, "Other Features of the Palette Manager." I used it to set the palette usage so that the palette will be good for any pixel depth that the window may end up on.

After setting up the palette, you attach it to the window:

SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);

If you want more control over when the window gets its updates, you would want to use NSetPalette instead of SetPalette. NSetPalette allows you to specify whether you want the updates to happen only when the window is in the background, only when it is in the foreground, always, or never.

The simplest way of using palettes is to store them as resources of type 'pltt'. When GetNewCWindow is called the system looks for a 'pltt' resource with the same ID as the window being opened. If it is found, the palette is loaded and attached to the window. It is also possible to have a palette that is used for all the windows an application may open; in this case when NewCWindow or GetNewCWindow is called (and no 'pltt' with the same ID is found) the 'pltt'with ID = 0 is used.

When you select one of the Animated menus, the code calls SetInhibited and passes the function a usage of pmAnimated. The function then sets up the palette to have the new usage and makes the palette available for animation.

PICKING A COLOR FOR DRAWING
In GiMeDaPalette I don't have to pick a color to draw with. If you want to do any drawing in your application, other than calling CopyBits, you call the trap PmForeColor to select a color to draw with and then just draw.

ANIMATING COLORS
Once you select one of the Animated menus, GiMeDaPalette requires only four lines of code toanimate colors. First you save the first color in the palette:

GetEntryColor(srcPalette, 1, &changecolor);

Then you cycle the colors 2 to 254:

AnimatePalette(myWindow, StoreCTab, 2, 1, numcolor - 2);

Next you move the saved color to the last entry :

AnimateEntry(myWindow, numcolor - 1, &changecolor);

Finally you save the new version of the palette into the color table for use during the next animation:

Palette2CTab(srcPalette, StoreCTab);

The sample does not animate the entire palette, because the Palette Manager will not let you animate white (entry 0) and black (entry 255).

USING THE COLOR MANAGER
All of this can be done with the Color Manager, but to do it and get the same functionality, you will need to generate considerably more code. The main problem with the Color Manager is that it does not provide automatic support for multiple monitors, color arbitration, compatibility with other applications, and several other features that are basic to any real color application.

OTHER AUTOMATIC FEATURES OF THE PALETTE MANAGER

KEEP IT SIMPLE
The Palette Manager takes care of several details for you, and the result is a simpler, more elegant application. It also provides more compatibility; that is, it helps ensure that the application will work under GC QuickDraw and will be friendly under MultiFinder.

TAKE CARE OF THE ENVIRONMENT
Like a good citizen in a community, an application must take care of its environment. If applications have no concern for their environments, users will constantly need to reboot their machines to get back to stable ground. The Palette Manager provides this support without extra code.

Background. One of the differences between the Color Manager and the Palette Manager is that the Color Manager usually animates the background and the Palette Manager tries not to. The Palette Manager reserves the colors so that no other applications can use them. This means that when the colors are animated, only the foreground animates, if possible. In general the Palette Manager tries to make sure that the colors your application is using will not make a mess of other applications that are running at the same time.

Reserve entries. You can reserve the colors with the Color Manager by using ReserveEntry, but if you do, you can no longer use Index2Color to get the colors you need. But even if you get around that problem, you still cannot call RGBForeColor to set the color; you need to change the graphics port directly, and we all know what a bad idea this is. Once again the Palette Manager is cleaner, because it does not force your application to manipulate the graphics port.

MultiFinder switch. The Palette Manager cleans up when you are switched out in MultiFinder. If another application that uses a different color environment is running at the same time as yours, it gets the colors it wants when it is the frontmost application, and you get the colors you want when you are the frontmost application. The Color Manager does not do this for you; you must do the work yourself.

Multiple monitors. The Palette Manager will work over multiple monitors, while the Color Manager does not. If you wish to run your application on multiple monitors, and you are using the Color Manager, you will have to worry about what monitors your windows span and make sure that SetEntriesis called for each monitor. This can get really cumbersome.

GIVE ME SPEED
Generally you would expect the Color Manager to run faster, since the AnimatePalettetrap ultimately calls SetEntries. However, the Palette Manager is just as fast as the Color Manager. And don't forget that while the Palette Manager is just as fast as the Color Manager, it is also providing the support for all of its cool features; the Color Manager provides none of this support.

USE THE PALETTE MANAGER

I hope that I have convinced you that the Palette Manager is considerably better at color table animation than the Color Manager. Let's go over the highlights:s
  • The Palette Manager makes your application more compatible with GC QuickDraw and MultiFinder. The Color Manager does not do this for you.
  • The Palette Manager protects your application from making a mess of the color environment by not letting it animate black and white. With the Color Manager you have to protect yourself.
  • The Palette Manager arbitrates the colors in the color environment for you. This means that you don't have to worry about your window getting the colors it needs when it is the frontmost window. Color Manager does not do this for you.
  • The Palette Manager takes care of multiple monitor support for you. Multiple monitor support comes free with the Palette Manager; there are no freebies in the Color Manager.
  • The Palette Manager can cleanly reserve the entries that you wish to animate; there is no clean way to do this with the Color Manager.

Table 1. Palette Manager versus Color Manager

Palette ManagerColor Manager
Compatible with MultiFinderX
Compatible with GC QuickDrawX
Compatible with other color appsX
Easy to useX
Automatic color arbitrationX
Easy support of multiple monitorsX
Requires you to implement all featuresX
Renders your app hardware dependentX
Not worth the effortX

If this has convinced you that the Palette Manager is the way to go, I think you've taken a big step toward making the next great color application that Mac users around the world will love.

PALETTE MANAGER HISTORY

1987 - The Palette Manager arrived on the scene late in the development of the original Macintosh II. The engineer who was responsible for the Palette Manager was given only a few weeks to produce it. Under the circumstances, it's amazing that the Palette Manager worked at all.

1988 - System 6.0.2 shipped with a new version of the Palette Manager that was much closer to the way it was supposed to work, but it still didn't do all that people wanted it to.

1989 - The first version of 32-Bit QuickDraw included a lot of big changes and improvements to the Palette Manager. There were still a few problems, but the Palette Manager was finally able to do what people really wanted it to.

1990 - The 32-Bit QuickDraw version of the Palette Manager was made part of the system software in 6.0.5. The last of the major problems were ironed out, and the new Palette Manager was available to everyone who ran system 6.0.5 or greater, with or without 32-Bit QuickDraw.

GetEntries: ONE OF THE GOTCHAS IN THE COLOR MANAGER

Some people think they can call GetEntries to save the current color table and later rebuild the color table with SetEntries. Unfortunately, GetEntries and SetEntries do not work well together.

Each time you call GetEntries and follow it with SetEntries, the color table will get a little lighter. What happens is that when you call SetEntries, the colors are gamma corrected and GetEntries does not reverse this effect. So each gamma correction causes the color table to get lighter. For more information about gamma correction take a look at "Designing Cards and Drivers for the Macintosh Family."


OTHER FEATURES OF THE PALETTE MANAGER

ctFlags BIT 14If you would like to use an offscreen world and to animate the images that you create, this is the bit for you. If you

  • set bit 14 of the ctFlags field in your color table
  • use this color table to pick your colors when drawing in the offscreen world
  • set the usage of your palette to pmAnimated + pmExplicit and then call CopyBits or DrawPicture; the colors will map correctly and the image will animate. This feature allows an offscreen world full access to the color table via the index values. If you do not follow these three steps, you will get only a black-and-white image out of the DrawPicture or CopyBits. I have included this feature in the sample GiMeDaPalette. The code below is just clippings from the sample itself.

- - - - - - - - - - - - - - - - -
mycolors = GetCTable (clutID);
(*mycolors)->ctFlags |= 0x4000;
...
srcPalette = NewPalette (numcolor, mycolors,
             pmAnimated + pmExplicit, 0);
...
err = NewGWorld (&offscreenGWorld, 8, &WinMinusScroll,
      mycolors, nil, nil);
...
DrawPicture (ThePict, &WinMinusScroll);

pmAnimated+pmExplicit This is one of the really cool features of the Palette Manager. Generally when you build a palette, the color matching will just put the colors where they fit best, but when you have a usage of explicit, the colors are placed in the exact index locations in which you want them. This is important because you can't set a palette to an offscreen world, and you generally want to be able to draw off screen first and then copy to the screen. This is a problem because you still need access to the palette colors. But you can achieve this access by building the palette in such a way that you know the colors are at the exact index values you havespecified in your color table. This feature in conjunction with the ctFlags bit 14 feature will give you the access you need in your offsceen world. For more information about this feature take a look at the Palette Manager chapter in Inside Macintosh , volume VI (chapter 20).

pmInhibit This usage option is yet another cool feature of the Palette Manager. It allows the application to specify which colors are to show up on which monitors. So if you have a palette of 256 colors, but the window is on a 4 bit/pixel screen, you'll get the 16 of the 256 colors that are labeled as pmInhibitC2. In my sample the function, SetInhibited, sets the first two colors to always show. The next 12 entries will be available only if the window is on a screen that is set to 4 or 8 bits/pixel. The rest of the colors will be available only if the window is on an 8 bit/pixel screen. You can also set the palette to consider whether the window is on a gray scale screen or not. For more information about this feature take a look at the Palette Manager chapter in Inside Macintosh , volume VI (chapter 20) and the sample code.

INTERRUPTS
You may have noticed that a call to AnimatePalette or SetEntries turns off interrupts. All Apple video cards, and most third-party video cards, turn off interrupts until the next VBL (vertical bLanking interrupt) when SetEntries is called. This generates a cleaner, smoother color animation, but it is a real headache for anyone who wants to animate colors and at the same time take in data on the serial lines or play a sound. Any kind of interrupt-driven process, which requires a lot of attention, is not going to work when SetEntries is being called repeatedly. *

RICH COLLYER Eagle Scout/Fractal Hacker, claims there is nothing unusual about himself. He's done the routine, everyday stuff we all do, such as attending a sacrifice at a temple in the Himalayas, climbing a 17,500-foot peak (climbing the 20,000-foot one would have been "unreasonable"), and strolling the byways of Katmandu and Bhutan. His adventurous tendencies led him to pursue a physics degree from Cal Poly with a specialty in computational fluid dynamics, and routinely compels him to climb rocks. He's survived at least three 20-foot falls; outwardly he seems unscathed, but we have to wonder. He has a distinct penchant toward chaos, named his dog Precious of Bonshaw, and fosters a burning desire to be a DTS engineer. Nothing unusual. *

Thanks to Our Technical Reviewers Guillermo Ortiz, Forrest Tanaka, David Van Brink, John Zap *

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
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 »

Price Scanner via MacPrices.net

Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
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

Jobs Board

Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular 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
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.