📄 sec-gdkvisual.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> Visuals and Colormaps </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="GDK Basics" href="cha-gdk.html"> <link rel="PREVIOUS" title="GdkWindow" href= "sec-gdkwindow.html"> <link rel="NEXT" title="Drawables and Pixmaps" href= "sec-gdkdrawable.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="sec-gdkwindow.html"><font color="#0000ff" size="2"><b><<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="sec-gdkdrawable.html"><font color="#0000ff" size="2"><b>Next >>></b></font></a> </td> </tr> </table> </div> <div class="SECT1"> <h1 class="SECT1"> <a name="SEC-GDKVISUAL">Visuals and Colormaps</a> </h1> <p> Unfortunately, not all hardware is created equal. The most primitive X servers support only two colors; each pixel is either on or off. This is referred to as a "one bit per pixel (bpp)" display. A display with one bit per pixel is said to have a <i class="FIRSTTERM">depth</i> of one. More advanced X servers support 24 or 32 bits per pixel, and allow you to specify a different depth on a window-by-window basis. 24 bits per pixel allows 2^24 different pixels, which includes more colors than the human eye can differentiate. </p> <p> Conceptually, a bitmap display consists of a rectangular grid of pixels. Each pixel consists of some fixed number of bits; pixels are mapped to visible colors in a hardware-dependent way. One way to think about this is to imagine a two-dimensional array of integers, where the integer size is chosen to hold the required number of bits. Alternatively, you can think of a display like this as a stack of <i class="FIRSTTERM">bit planes</i>, or two-dimensional arrays of bits. If all the planes are parallel to one another, a pixel is a perpendicular line passing through the same coordinates on each plane, taking a single bit from each one. This is the origin of the term <i class="FIRSTTERM">depth</i>, since the number of bits per pixel is equal to the depth of the stack of bit planes. </p> <p> In the X Window System, pixels represent entries in a color lookup table. A <i class="FIRSTTERM">color</i> is a red, green, blue (RGB) value---monitors mix red, green, and blue light in some ratio to display each pixel. Take an eight bit display, for example: eight bits are not enough to encode a color in-place; only a few arbitrary RGB values would be possible. Instead, the bits are interpreted as an integer and used to index an array of RGB color values. This table of colors is called the <i class="FIRSTTERM"> colormap</i>; it can sometimes be modified to contain the colors you plan to use, though this is hardware-dependent---some colormaps are read-only. </p> <p> A <i class="FIRSTTERM">visual</i> is required to determine how a pixel's bit pattern is converted into a visible color. Thus, a visual also defines how colormaps work. On an 8-bit display, the X server might interpret each pixel as an index into a single colormap containing the 256 possible colors. 24-bit visuals typically have three colormaps: one for shades of red, one for shades of green, and one for shades of blue. Each colormap is indexed with an eight-bit value; the three eight-bit values are packed into a 24-bit pixel. The visual defines the meaning of the pixel contents. Visuals also define whether the colormap is read-only or modifiable. </p> <p> In short, a visual is a description of the color capabilities of a particular X server. In Xlib, you have to do a lot of fooling around with visuals; GDK and GTK+ shield you from most of the mess. </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z114"><span class="STRUCTNAME"> GdkVisual</span></a> </h2> <p> Xlib can report a list of all available visuals and information about each; GDK keeps a client-side copy of this information in a struct called <span class= "STRUCTNAME">GdkVisual</span>. GDK can report the available visuals, and rank them in different ways. Most of the time you will only use <tt class="FUNCTION"> gdk_visual_get_system()</tt>, which returns a pointer to the default visual (<a href= "sec-gdkvisual.html#FL-GDKVISUAL">Figure 2</a>). (If you're writing a <tt class="CLASSNAME">GtkWidget</tt>, <tt class="FUNCTION">gtk_widget_get_visual()</tt> returns the visual you should use; more on this in <a href= "cha-widget.html">the chapter called <i>Writing a <tt class="CLASSNAME">GtkWidget</tt></i></a>.) The returned visual is not a copy, so there is no need to free it; GDK keeps visuals around permanently. </p> <div class="FIGURE"> <a name="FL-GDKVISUAL"></a> <div class="FUNCSYNOPSIS"> <a name="FL-GDKVISUAL.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <gdk/gdk.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">GdkVisual* <tt class= "FUNCTION"> gdk_visual_get_system</tt></code>(void);</code> </p> </div> <p> <b>Figure 2. Default Visual</b> </p> </div> <p> For reference, here are the contents of <span class= "STRUCTNAME">GdkVisual</span>; most of the members are used to calculate pixel values from colors. Since this is fairly involved and rarely used, this book glosses over the topic. The <span class="STRUCTNAME">depth</span> member is convenient sometimes. <a href= "sec-gdkvisual.html#SEC-TYPESOFVISUAL">the section called <i>Types of Visual</i></a> has more to say about the <span class="STRUCTNAME">type</span> member. </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> typedef struct _GdkVisual GdkVisual;struct _GdkVisual{ GdkVisualType type; gint depth; GdkByteOrder byte_order; gint colormap_size; gint bits_per_rgb; guint32 red_mask; gint red_shift; gint red_prec; guint32 green_mask; gint green_shift; gint green_prec; guint32 blue_mask; gint blue_shift; gint blue_prec;}; </pre> </td> </tr> </table> <div class="SECT3"> <h3 class="SECT3"> <a name="SEC-TYPESOFVISUAL">Types of Visual</a> </h3> <p> Visuals differ along several dimensions. They can be grayscale or RGB, colormaps can be modifiable or fixed, and the pixel value can either index a single colormap or contain packed red, green, and blue indexes. Here are the possible values for <span class="STRUCTNAME"> GdkVisualType</span>: </p> <ul> <li> <p> <span class="STRUCTNAME"> GDK_VISUAL_STATIC_GRAY</span> means the display is either monochrome or gray scale, and the colormap cannot be modified. A pixel value is simply a level of gray; each pixel is "hard coded" to represent a certain on-screen color. </p> </li> <li> <p> <span class="STRUCTNAME"> GDK_VISUAL_GRAYSCALE</span> means the display has a modifiable colormap, but only levels of gray are possible. The pixel represents an entry in the colormap, so a given pixel can represent a different level of gray at different times. </p> </li> <li> <p> <span class="STRUCTNAME"> GDK_VISUAL_STATIC_COLOR</span> represents a color display which uses a single read-only colormap rather than a separate colormap for each of red, green, and blue. The display is almost certainly 12-bit or less (a 24-bit display using a single colormap would need a colormap with 2^24 entries, occupying close to half a gigabyte---not very practical!). This is an annoying visual, because relatively few colors are available and you can't change which colors they are. </p> </li> <li> <p> <span class="STRUCTNAME"> GDK_VISUAL_PSEUDO_COLOR</span> is the most common visual on low-end PC hardware from several years ago. If you have a one-megabyte 256-color video card, this is most likely your X server's visual. It represents a color display with a read/write colormap. Pixels index a single colormap. </p> </li> <li> <p> <span class="STRUCTNAME"> GDK_VISUAL_TRUE_COLOR</span> is a color display with three read-only colormaps, one for each of red, green, and blue. A pixel contains three indexes, one per colormap. There is a fixed mathematical relationship between pixels and RGB triplets; you can get a pixel from red, green, and blue values in [0, 255] using the formula: <span class="STRUCTNAME">gulong pixel = (gulong)(red*65536 + green*256 + blue)</span>. </p> </li> <li> <p> <span class="STRUCTNAME"> GDK_VISUAL_DIRECT_COLOR</span> is a color display with three read-write colormaps. If you use the GDK color handling routines, they simply fill up all three colormaps to emulate a true color display, then pretend the direct color display is true color. </p> </li> </ul> </div> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z115">Color and <span class="STRUCTNAME"> GdkColormap</span></a> </h2> <p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -