Color Palettes:

The reason color palettes exist is so image files can be smaller than they would be if they always contained a byte of data for each color (red, green, blue) for each pixel. When you have three bytes per pixel you get uncompressed file sizes that start around a megabyte and go up fast. The idea with a color palette is that you only have one or sometimes two bytes per pixel in an image. As you can see this cuts the file size fast. In the case of using one byte per pixel you have only one third the data to save. There is a slight size increase over just saving the image data in that you also have to save the color palette for the image.

So what is a color palette anyway? Let’s take a common file type as an example. Using the .GIF file type, images are saved with one byte per pixel plus a color palette. The color palette is a table of numbers that allows, in the case of .GIF files, up to 256 colors to be defined. The way it’s done is that there are three bytes (numbers) in the table for each color defined. These three bytes are the red, green, and blue, values that are used to make the color. In this way each color defined in the palette can be any one of the millions of colors available in the three byte color system. The catch is that you only get to have 256 entries in the palette. So the palette is just a table of numbers with 256 entries where each entry has three numbers that give the red, green, and blue values that are used to make the color.

The way this gets back to the image is in the one byte of data that is saved for each pixel. The value saved for each pixel is the number of the entry in the color palette that is the color the pixel is supposed to be. So the pixel values "point" to the place in the color palette where the color for the pixel is recorded. The reason the color palette is limited, in this case, to 256 colors is that 256 is the largest number that can be stored in one byte of data and just to keep the record straight, the values stored in a byte actually range from 0 to 255.

Where does the palette come from? If you’re creating an image from scratch like an artist would the image editing tool you create the image with allows you to define the color of each entry in the image’s color palette. If you’re editing an existing image, again the more comprehensive image editing tools allow you to redefine any of the colors in an image’s color palette. Say the image you're starting with has more than 256 colors, how is the 256 color palette created if you want a final image with only 256 colors? If the image you're starting with has a palette larger than 256 colors and you choose to save it as say a .GIF file, the image editor will do a bunch of math on the existing palette, keeping track of how many times each color is used, to decide what 256 colors will best reproduce the existing image and saves the resulting 256 color palette in the .GIF file for you. If you started with an image that was three bytes per pixel and so didn’t have a color palette to start with, then the math gets done on the image data to pick the colors for the new image’s color palette. Why did I say "the new image"? If you’ve removed a bunch of color data from an image, it’s not the same image you started with.

The following graphic shows a few lines from a color palette as an example. A 256 color palette would have 256 rows. Here I’ve only shown three rows. Note that the row entry number doesn’t really exist; it’s not part of the table. The first row is row 0 and they are just lined up in order to row 255. In fact in the actual files the palette isn’t in rows and columns. The numbers are just all strung end to end. The computer program that uses the palette just counts down the line to get the data it wants. The second illustration shows how the numbers from the example would look in a computer file. In the computer file the numbers are stored in Hexadecimal form rather than the Decimal numbers we are used to. That’s how you can keep any value between 0 and 255 in one byte of data. A single byte hexadecimal number has values ranging from 00 to FF; where hexadecimal 00 equals decimal 0 and hexadecimal FF equals decimal 255. In the example below decimal 240 equal hexadecimal F0. I know; you're saying "Yah, right". I agree, it's not clear to the casual observer how this business of 0 to 255 decimal in a byte of data is the same as 00 to FF hexadecimal in a byte of data. I have put a little explanation in my page on Bytes. Sorry, but computers don't line up the bytes in pretty colors; I just used the colors to show how the numbers moved from one illustration to the other.



Back to main digital page


Copyright © by Liesel Siobhan

The links at the bottom of your screen will take you to the main topic areas