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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom 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.