Manipulating Color and Pixels with Visual Basic VB Help

In the last chapter, we explored the methods and techniques for drawing· shapes. There are only three shape-drawing methods, but when coupled with the various properties of the PictureBox control or the Form object, they’re quite powerful and flexible. The examples you’ve seen illustrate how far you can go with Visual Basic’s drawing methods. This chapter explores Visual Basic’s two methods for manipulating pixels: Set and Point. PSet turns on pixels, and Point reads their values. Similar to their drawing counterparts, the methods for manipulating pixels are quite flexible, and you can do a lot with them, but they are slow.
In the latter part of this chapter, you’ll see how image processing applications work, and you’ll learn how to implement your own image p,rocessing techniques in VISualBasic. The Image application isn’t nearly as fast or as elaborate as professional image processing applications, but you can USe it to experiment. In Chapter 12, Optimizing VBApplications, we’ll optimize the speed of the Image application using API functions. The optimized application is’also called Image and can be found Image2 folder. Use Chapter 12’5 Image project if you like this chapter’s Image application and want to process your images or incorporate it in your own applications. In this.chapter we’ll look at the basic image processing techniques and the core of an image processing application, without worrying too much about efficiency. Finally, we’ll take a quick look at how to use palettes. Let’s begin by examining how computers manipulate color, and then look at VISual Basic’s function for specifying Color values

Specifying Colors

Many of you probably have already used the Color common dialog box, which lets you specify colors by manipulating their basic components. If you attempt to specify a Color value through the common dialog box, you’ll see three boxes- Red, Green, and Blue (RGB)-whose values change as you move the cross-shaped .
pointer over the color spectrum. (The Color common dialog box will be discussed in detail in Chapter 8, Advanced Acti’oeX Controls.) These are the values of the three basic colors that computers use to specify colors. Any color that can be represented . on a computer monitor is specified by means of the RGB colors. By mixing per centar of ilie&e basic COlOIS, you can design almost ?any color.The mode! of designing colors based on the intensities of their RGB ~omponents is called the RCB model, and it’s a fundamental concept in computer graphics. If you aren’t familiar with this model, this section might be well worth reading. Eye color you can imagine can be constructed by mixing the appropriate percentages of the three basic colors. Each color, therefore, is represented by a triplet (Red, Green, Blue), in which red, green, and blue are three bytes that represent the basic color components. The smallest value, 0, indicates the absence of color. The largest value, 255, indicates full intensity, or saturation. The triplet (0, 0, 0) is black, because all colors are missing, and the triplet (255, 255, 255) is white. Other colors have various combinations: (255, 0, 0) is a pure red, (0, 255, 255) is a pure cyan (what you get when you mix green and blue), and (0, 128, 128) is a mid-cyan (a mix of mid-green and mid-blue tones). The possible combinations of the three basic color components are 256 x 256 x 256, or 16,777,216 colors.

Notice that we use the term basic colors and not primary colors, which are the three colors used in designing colors with paint. The concept is the same; you mix the primary colors until you get the desired result. The primary colors used in painting, however, are different. They ere the colors red, yellow, and blue. Painters can get
any shade imaginable by mixing the appropriate percentages of red, yellow, and blue-paint. On a ccmputcr monitor, you can design any color by mixing the appropriate percentages of ed, green, and blue,
The process of generating colors with three basic components is based on the RGB Color Cube. shown in Figure 7.1. The three dimensions’ of the color cube correspond to he three basic colors. The cube’s comers are assigned each of the three primary colors, their complements, and the colors black and white. Complementary
colors are easily calculated by subtracting the Color values from 255. For example, the color (0, O. 255) is a pure blue tone. Its complementary color is (255- 0,255-0,255-255), or (?!is, 255, OJ, which is a pure yellow tone. Blue and yellow are complementary” corers, and they are mapped to opposite comers of the cube. The same is true for red. and cyan, green and magenta, and black and white. If . you add acolor to its complement, you get white.
Notice that the components of the colors at the comers of the cube have either :u:J.O cr full intensity, As you move from one comer to another along the same .dge of it cube, online of its components changes value. For example, as you move from the green to the yellow comer, the red component change from 0 to 255. The other two eomponents remain the same. As you move between these two
comers, you get-all the available tones from green to yellow (256 in all). Similarly; you move from the yellow to the red comer, the only component that changes is the green, and you gel all available shades rom yellow this range of similar colors is called gradient.

Although you can specify more than 16 million colors, you can’t have more than 256 shades of gray. The reason is that a gray tone, including the two extremes (black and white), is made up of equal values of all three primary colors. Youcan see this on the RGBcube. Gray shades lie on the cube’s diagonal that goes from black to white. As you move along this path, all three basic components change value, but they are always equal. The value (128, 128, 128) is a mid-gray tone, but the values (127, 128, 128)and (129, 128, 128) aren’t gray tones, although they are too close for the human eye to distinguish. That’s why it’s wasteful to store grayscale pictures using L6-million-color True Color file formats (see the True Color and Palette Systems sidebar for more information). A 256-color file format stores a ‘grayscale just as accurately and more compactly. Once you know an image is grayscale, you needn’t store all three bytes per pixel. One value is adequate (the other two components have the same value).

Defining Colors with the RGBO Function

For defining colors, Visual Basic provides the RGBO function, which accepts three arguments: .
RGB(Red, Green, Blue)
With the RGBOfunction are you can define any color imaginable. I mentioned earlylier that the triplet (255, 255, 0) is a pure yellow tone. To specify this Color value with the RGBOfunction, you can use statement such as the following: newColor – RGB(255, 255, 0)The newColor variable is a Long integer (a Long integer is made up of four bytes) and it can be assigned to any color property. To change the Form’s background

color to yellow, you can assign the newColor variable to the BackColor property, like this:
Forml.8ackColor – newColor or you can combine both statements into one like this: Forml.8ackC~lor – RG8(255, 255, 0) Recall in Chapter 5, Basic ActiveX Controls, the Colors application used the RGB() function to design colors. You can open this application again to see how it translates the values of the three scroll bars to RGB values. It simply plugs fhe values of the scroll bars into the RGBOfunction and assigns the result to the PictureBox control’s BackC~lor property ..

Posted on November 5, 2015 in Manipulating Color and Pixels with Visual Basic

Share the Story

Back to Top
Share This