文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>5.3 Basic Computer Color

5.3 Basic Computer Color

时间:2010-09-20  来源:Sean You

5.3 Basic Computer Color

Computer monitors emit a mixture of red, green, and blue light through each pixel. When the light mixture enters the eye and strikes an area of the retina, cone receptor cells are stimulated and neural impulses are sent down the optic nerve toward the brain. The brain interprets the signal and generates a color. As the light mixture varies, the cells are stimulated differently, which in turn generates a different color in the mind. Figure 5.8 shows how mixing red, green, and blue produces different colors; it also shows different intensities of red. By using different intensities for each color component and mixing them together, we can describe all the colors we need to display realistic images.

 
Figure 5.8: (Top) The mixing of pure red, green, and blue colors to get new colors. (Bottom) Different shades of red found by controlling the intensity of red light. A full-color version of this image is available in the download for this book, and is named Figure 5_8.bmp.

The best way to get comfortable with describing colors by RGB (red, green, blue) values is to use a paint program like Adobe Photoshop, or even the Win32 Color dialog box (Figure 5.9), and experiment with different RGB combinations to see the colors they produce.

 
Figure 5.9: The Color dialog box.

A monitor has a maximum intensity of red, green, and blue light it can emit. To describe the intensities of light, it is useful to use a normalized range from 0 to 1. 0 denotes no intensity and 1 denotes the full intensity. Intermediate values denote intermediate intensities. For example, the values (0.25, 0.67, 1.0) mean the light mixture consists of 25% intensity of red light, 67% intensity of green light, and 100% intensity of blue light. As this example implies, we can represent a color by a 3D color vector (r, g, b), where 0 ≤ r, g, b ≤ 1, and each color component describes the intensity of red, green, and blue light, respectively, in the mixture.

5.3.1 Color Operations

Some vector operations also apply to color vectors. For example, we can add color vectors to get new colors:

 

By combining a medium-intensity green color with a low-intensity blue color, we get a dark-green color.

Colors can also be subtracted to get new colors:

 

That is, we start with white and subtract out the red and green parts, and we end up with blue.

Scalar multiplication also makes sense. Consider the following:

 

That is, we start with white and multiply by 0.5, and we end up with a medium shade of gray. On the other hand, the operation 2(0.25, 0.0, 0.0) = (0.5, 0.0, 0.0) doubles the intensity of the red component.

Obviously expressions like the dot product and cross product do not make sense for color vectors. However, color vectors do get their own special color operation called modulation or componentwise multiplication. It is defined as:

 

This operation is mainly used in lighting equations. For example, suppose we have an incoming ray of light with color (r, g, b) and it strikes a surface that reflects 50% red light, 75% green light, and 25% blue light, and absorbs the rest. Then the color of the reflected light ray is given by:

 

So we can see that the light ray lost some intensity when it struck the surface, since the surface absorbed some of the light.

When doing color operations, it is possible that your color components extend outside the [0, 1] interval; consider the equation, (1, 0.1, 0.6) + (0.0, 0.3, 0.5) = (1, 0.4, 1.1), for example. Since 1.0 represents the maximum intensity of a color component, you cannot increase it. Thus 1.1 is just as intense as 1.0. So what we do is clamp 1.1 → 1.0. Likewise, a monitor cannot emit negative light, so any negative color component (which could result from a subtraction operation) should be clamped to 0.0.

5.3.2 128-Bit Color

It is common to incorporate an additional color component called the alpha component. The alpha component is often used to denote the opacity of a color, which is useful in blending (see Chapter 8). (Since we are not using blending yet, just set the alpha component to 1 for now.)

Including the alpha component means we can represent a color by a 4D color vector (r, g, b, a) where 0 ≤ r, g, b, a ≤ 1. To represent a color with 128 bits, we use a floating-point value for each component. The D3DX library provides the following structure, complete with helpful overloaded operators:

 

 1 typedef struct D3DXCOLOR
2 {
3 #ifdef __cplusplus
4 public:
5 D3DXCOLOR() {};
6 D3DXCOLOR(UINT argb);
7 D3DXCOLOR(CONST FLOAT *);
8 D3DXCOLOR(CONST D3DXFLOAT16 *);
9 D3DXCOLOR(FLOAT r, FLOAT g, FLOAT b, FLOAT a);
10
11 // casting
12 operator UINT () const;
13
14 operator FLOAT* ();
15 operator CONST FLOAT* () const;
16
17 // assignment operators
18 D3DXCOLOR& operator += (CONST D3DXCOLOR&);
19 D3DXCOLOR& operator -= (CONST D3DXCOLOR&);
20 D3DXCOLOR& operator *= (FLOAT);
21 D3DXCOLOR& operator /= (FLOAT);
22
23 // unary operators
24 D3DXCOLOR operator + () const;
25 D3DXCOLOR operator - () const;
26
27 // binary operators
28 D3DXCOLOR operator + (CONST D3DXCOLOR&) const;
29 D3DXCOLOR operator - (CONST D3DXCOLOR&) const;
30 D3DXCOLOR operator * (FLOAT) const;
31 D3DXCOLOR operator / (FLOAT) const;
32
33 friend D3DXCOLOR operator * (FLOAT, CONST D3DXCOLOR&);
34
35 BOOL operator == (CONST D3DXCOLOR&) const;
36 BOOL operator != (CONST D3DXCOLOR&) const;
37
38 #endif //__cplusplus
39 FLOAT r, g, b, a;
40 } D3DXCOLOR, *LPD3DXCOLOR;
41
42

 

 

For componentwise multiplication, the D3DX library provides the following function:

D3DXCOLOR* D3DXColorModulate(
    D3DXCOLOR* pOut,      // Returns (cr, cg, cb, ca)   (kr, kg, kb, ka)
    CONST D3DXCOLOR* pC1, // (cr, cg, cb, ca)
    CONST D3DXCOLOR* pC2  // (kr, kg, kb, ka)
);

5.3.3 32-Bit Color

To represent a color with 32 bits, a byte is given to each component. Since each color is given an 8-bit byte, we can represent 256 different shades for each color component — 0 being no intensity, 255 being full intensity, and intermediate values being intermediate intensities. A byte per color component may seem small, but when we look at all the combinations (256×256×256 = 16,777,216), we see that millions of distinct colors can be represented.

A 32-bit color can be converted to a 128-bit color by mapping the integer range [0, 255] onto the real-valued interval [0, 1]. This is done by dividing by 255. That is, if 0 ≤ n ≤ 255 is an integer, then  gives the intensity in the normalized range from 0 to 1. For example, the 32-bit color (80, 140, 200, 255) becomes:

 

On the other hand, a 128-bit color can be converted to a 32-bit color by multiplying each component by 255 and rounding to the nearest integer. For example:

 

Additional bit operations must usually be done when converting a 32-bit color to a 128-bit color and conversely because the 8-bit color components are usually packed into a 32-bit integer value (e.g., an unsigned int). The D3DXCOLOR class has a constructor that takes an unsigned integer (UINT) and constructs a D3DXCOLOR object from it. Figure 5.10 shows how the 8-bit color components are packed into a UINT. Note that this is just one way to pack the color components. Another format might be ABGR or RGBA, instead of ARGB; however, the D3DXCOLOR class assumes the ARGB layout. The D3DXCOLOR class also overloads operator UINT, so that a D3DXCOLOR object can be cast to a 32-bit color value packed into a UINT with format ARGB.


Figure 5.10: A 32-bit color, where a byte is allocated for each color component alpha, red, green, and blue.

Typically, 128-bit color values are used where many color operations will take place (e.g., in a pixel shader); in this way, we have many bits of accuracy for the calculations so arithmetic errors do not accumulate too much. The final pixel color, however, is usually stored in a 32-bit color value in the back buffer; current physical display devices cannot take advantage of the higher resolution color [Verth04].

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载