Working principle of the color palette)

Source: Internet
Author: User
The display can be set to 16, 256, 64 K, true color, and other display modes. The first two modes require a color palette. In 16 or 256 color mode, Program You must correctly set the color you want to display to the color palette to display the expected color.

Working principle for a palette in 256-Color Display Mode

Windows is a multitasking operating system that can run multiple programs at the same time. If you have to set the color palette for several programs, conflicts may occur. To avoid this conflict, Windows uses a logical palette as a buffer between the application that uses colors and the system palette (physical palette.

In Windows, an application uses a system palette (physical palette) by using one or more logical presets ). In the 256 color system palette, Windows retains 20 colors as static colors, which are used to display windows interfaces, and applications generally cannot change. The default system color palette contains only the 20 static colors, and other items in the color palette are empty. To use a new color, the application must implement a logical palette containing the required color into the system palette. In the implementation process, windows first completely matches the items in the logical color palette with those in the system color palette, windows adds it to the blank items in the system palette. The system palette has a total of 236 blank items for use. If the system palette is full, in Windows, the remaining items in the logical palette are matched to the nearest possible color in the system palette.

Each device context has a logical color palette. The default logical color palette contains only 20 retain colors. To use a new color, A new logical palette should be created and selected into the device context. However, the new color cannot be used in this case. The program can only implement the logical color palette in the device context to the system color palette. When the logical palette is implemented to the system palette, Windows creates a palette ing table. When the device context is drawn by color in the logical palette, the GDI plotting function queries the palette ing table to convert the pixel value from the index of the logical palette to the index of the system palette, in this way, when the pixel is output to the video memory, it has the correct color value.
This ing relationship is explained, so that you can understand the buffering effect of the logical palette. In this figure, the GDI plotting function uses the color in index 1 of the logical palette to plot. by querying the palette ing table, it is found that the index No. 23rd in the system palette exactly matches it, in this way, the actual pixel value output to the video memory is 23. Note that the figure also shows incomplete color matching, that is, index 15 in the logical palette and index 46 in the system palette.

each window to use additional colors implements its own logical palette. Each color in the logical palette has the same or similar matching in the system palette. The higher the implementation priority of the palette, the higher the matching accuracy. Windows requires that the logical color palette of the activity window (if any) has the highest implementation priority. This is because the activity window is the current window that interacts with the user and should be properly colored. The priority of the inactive window is determined from top to bottom in the Z sequence (the Z sequence is the overlapping order of the overlapping windows ). The activity window has the right to use its logical color palette as the foreground color palette, while the activity window can only implement the background color palette.
tip: The term "active window" or "foreground window" refers to the window currently interacting with users. The title bar at the top of the window is highlighted, the title bar of the inactive window is gray. An active window must be a top-level window. A top-level window is a window without a parent window or a parent window is a desktop window, which generally has a title and border, it mainly includes the frame window and dialog box. The overlapping window of terms refers to the window used as the main window of the application. We can regard the dialog box as a special overlapping window.

CreatepaletteOnly a logical color palette is created. At this time, the color palette is only an isolated color table and does not affect the system. The program must callCDC: selectpaletteSelect the logical palette to the device context to use, and then callCDC: realizepaletteImplement the logical palette to the system palette. Function declaration:

Cpalette * selectpalette (cpalette * ppalette, bool bforcebackground );
This function selects the specified color palette to the device context. Parameters Ppalette Point to Cpalette Object. Parameters Bforcebackground If yes True The selected color palette is always used as the background color palette. If Bforcebackground Yes False The device context is attached to a window. When the window is a child window of an activity window or activity window, the selected color palette is implemented as the foreground color palette. Otherwise, it is implemented as the background color palette. This parameter is ignored if the color palette is a memory device context. The function returns the palette used by the device context. If an error occurs, it returns Null .

Uint realizepalette ();
This function implements the logical color palette in the device context to the system color palette. The Return Value of the function indicates how many items are changed in the palette ing table.

to display a special color in a window, generally, wm_paint implements your own logical color palette for messages. That is to say, in onpaint or ondraw selectpalette and realizepalette . If the color displayed in the window is important, call selectpalette should be specified bforcebackground parameter: false .

the foreground color palette has the highest priority for colors, it has an unconditional system usage palette (20 ) power, that is, if needed, the foreground palette will overwrite the system palette 236 table items, whether or not these table items are used by other windows. The background palette does not have the permission to damage the items used in the system palette.

Please note that the foreground color palette should be unique. If an activity window needs to implement several logical presets at the same time, only one palette can be used as the foreground palette.CDC: selectpaletteOnly oneBforcebackgroundSpecifiedFalse, OtherBforcebackgroundRequiredTrue. The color palette of the window with the input focus is usually implemented as the foreground color palette. Other windows can only use the background color palette. If all the child windows in the activity window use the foreground color palette, it will lead to an endless loop of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.