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

Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
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 »

Price Scanner via MacPrices.net

May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL 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
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.