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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.