| By Berndt Hamboeck | Article Rating: |
|
| June 1, 2003 12:00 AM EDT | Reads: |
11,263 |
Have you ever looked at a rainbow and wondered how all the colors got there? Or wondered why grass looks green and your jeans blue? Well, if you sit in front of your computer as often as I do, you might not.
However, the following problem may have occurred: you want to create a cool window in which the data and the headers are displayed in different colors, but you can't find the right one. If you've come across this situation before, then this article is right for you.
What Is Light?
Let's discuss a bit of physics. If you look at a cool DVD movie, you hear sound and see pictures. We can say that it's a combination of light and sound, both of which are made up of vibrations or frequencies. The unit used to measure vibrations per second is hertz (Hz). Sound vibrations occur in the lower regions of the spectrum, whereas light vibrations can be found in the higher frequency areas. If we place these vibrations into an electromagnetic spectrum, then the sound vibrations occur in the lower regions of the spectrum (from 20 to 20,000 Hz), whereas light vibrations (usually called wavelength) can be found in the higher frequency areas. Visible light is a small part of the electromagnetic spectrum, between the wavelengths 0.00078 mm or 780 nm (nanometer = billionth of a meter) to a wavelength of 0.00038 mm (380 nm).
In 1666, Sir Isaac Newton used a glass prism to refract white light at different angles according to wavelength. He saw a rainbow of colors, which he passed through a second prism to re-form white light. He concluded that we perceive the various wavelengths as different colors, and that visible light is only visible because we can see the source and the objects being illuminated. Thank you Mr. Isaac, this is good enough for us; we are happy with this short description (we're developers, not physicists, right?). Sir Newton was playing with additive color mixing and the result was that no light (or color) is black. All light (all colors) is white.
Hue, Saturation, and Brightness
Let's talk about colors. Usually, a color is described as having three dimensions - hue, saturation, and brightness.
1. Hue is the name of the color; it places the color in its correct position in the spectrum.
If you look at Figure 1, the color blue is distinguished from yellow, red, green, and the other colors.
2. Saturation refers to the degree of hue in a color or a color's strength. A neutral gray is considered to have zero saturation. You can experience reduced saturation by setting your monitor to gray scale.
3. Brightness describes differences in the intensity of light reflected from or transmitted by a color image. Photographers very often say that brightness is the single most important quality of light. The hue of an object may be green, but the terms dark and light distinguish the brightness of one object from another. It can be measured and recorded in a numeric value, and the term Lux is used to express the amount of brightness. To give you an idea, Table 1 provides some values.
Now, let's look at how a computer produces different colors.
Color Mixing
There are only two ways to reproduce color: additive and subtractive. These two methods are based on the theory that all colors can be created using three primaries (which I'll describe shortly).
Additive Color Mixing
In additive color mixing, the primary colors are red, green, and blue. Think of additive color mixing as three lights - red, green, and blue - all pointing to the same spot. These lights can give off different amounts of light to create different colors. When none of the lights are on, you have black. If they're all on 100%, you have white. And you can create any other colors in between by varying the amount of light given off by each.
I'd like to describe the principles of additive color synthesis using a simple application (ColorChooser) that I have written for this article (it can be downloaded from http://codexchange.sybase.com, in the PowerBuilder/General section, or from www.sys-con.com/pbdj/sourcec.cfm). The application uses PowerBuilder's built-in RGB function, which takes three arguments - the color value (the range is from 0 to 255) for red, green, and blue. It returns the computed color value. I'll provide some examples of how the color value is generated by PowerBuilder.
The call to the RGB function to get the color value for red would be RGB(255,0,0), which means that PowerBuilder should give me a real cool-looking red, but no green and no blue. This call would return 255 and if we look at the binary value, this would look like "11111111". Okay, this was easy.
Now we would like to see a beautiful green color (my eye specialist said last week I should go outside more and look at some green trees and bushes to help my eyes relax a bit). So let's make him happy and bring a green box (if you reproduce it with the ColorChooser application) on our screen by using RGB(0,255,0) for the function. What do you think the function will return? Did you guess it? Yes, it is the value 65,280, which shows up as a binary value form "1111111100000000". So the RGB function takes the value for G(reen) and multiplies it by 256, which in our case means 255*256 = 65,280.
Last but not least, we would like to display a blue value, so we call the function using RGB(0,0,255). This returns 16711680, which means "111111110000000000000000" in its binary form. So the RGB function calculates the blue part by using our B(lue) value and multiplies it by 65536 (which is in fact 256*256). What does all this mean? It means that the RGB function uses a double word (= 2 words = 4 bytes) to return the color value. It stores the values for red in the first byte, the value for green in the second byte, and the blue value in the third byte. If you mix some values, for example, to create a brown color, you would call the function using RGB(127,64,64), which would be calculated this way:
R(ed)*1 + G(reen)*256 + B(lue)*256*256
which would end in a calculation:
127 + 16384 + 4194304 = 4210815
This also means that the value for white - RGB(255,255,255) - is 16777215, which is a binary value of "111111111111111111111111". If you are one of those old C gurus, you might ask what happens to the last (the highest) byte if PowerBuilder uses a double word. We have just seen three bytes used by PowerBuilder. That's right, but PowerBuilder needs somewhere to store custom colors and this happens here. For example, if you open the DataWindow painter, then open the custom color dialog (Design/Custom Colors), define a custom color as I did in Figure 2 (it is just the color black to display how it works internally), and assign this color to a column (as background color), and then, if you export the source, you'll see that PowerBuilder adds this line to your column:
background.color="16777216"
The binary value would look like this:
00000001000000000000000000000000
PowerBuilder uses the highest byte to store custom colors.
I'd like to mention that if you use your own colors within your code, it might be faster to use the color value, but it's not as readable as the RGB function. For example, what is the following line doing?
dw_1.Object.emp_id.Background.Color = '12632256'
Yes, it is setting the background color, but to which color?
I'd say that this is much more readable (and you might remember it yourself several months later if you look at what you wanted to achieve):
dw_1.Object.emp_id.Background.Color = RGB(192,192,192)
This line of code sets the background color to a gray value, so it looks like this column is disabled. If speed is really an issue in your application, I'd recommend that you define constants for your colors (for example, CONSTANT long DISABLEDLOOK = 12632256).
Let's look at how this color mixing stuff works so we can get the colors we'd like to have.
Get System Color Values in PowerBuilder
The ColorChooser application that I've written uses the RGB function to calculate the color value.
To get the color values your operating system uses, query Windows to dynamically get the current color setting.
[External function declaration]
FUNCTION unsignedlong GetSysColor(int nIndex) LIBRARY "user32.dll"
Use the parameters in Listing 1 defined in winuser.h, which can be found within your Shared/PowerBuilder/cgen/nt/h directory.
Subtractive Color Mixing
If you're one of those people who switch off your computer and take a sheet of paper and start painting, you might be interested in subtractive color mixing. It's just the opposite of additive color mixing. You use pigments to absorb light and reflect the color(s). The primary colors are yellow, cyan, and magenta. To get black, you have to mix the same amount of each of the primary pigments together. And you use none for white.
Summary
Color can be obtained by either an additive or a subtractive process; however, in either case light is required to see color. Color is described as having three dimensions - hue, saturation, and brightness. There are two types of color mixing - additive and subtractive. TV monitors use additive color mixing. You need the colors red, green, and blue. Subtractive color mixing is used when you start painting a (real) picture.
Published June 1, 2003 Reads 11,263
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Berndt Hamboeck
Berndt Hamboeck is a senior consultant for BHITCON (www.bhitcon.net). He's a CSI, SCAPC8, EASAC, SCJP2, and started his Sybase development using PB5. You can reach him under admin@bhitcon.net.
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- How PowerBuilder Got Its Groove Back
- The Cloud Has Cross-Border Ambitions
- Ulitzer Names The World's 30 Most Influential Virtualization Bloggers
- Ulitzer Named "New Media" Partner of Greatly Anticipated iStrategy Event in Berlin
- Risks and Enterprise Mobility?
- Steps for Success in Enterprise Mobility?
- Are Mobile Luddites Resisting Mobility?
- The Difference Between Web Hosting and Cloud Computing
- Sybase CTO to Speak at 4th International Cloud Computing Expo
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- Five Reasons to Choose a Private Cloud
- Seeding The Cloud: The Future of Data Management
- The Threat Behind the Firewall
- Economy Drives Adoption of Virtual Lab Technology
- Tips for Efficient PaaS Application Design
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- Where Are RIA Technologies Headed in 2008?
- PowerBuilder History - How Did It Evolve?
- The Top 250 Players in the Cloud Computing Ecosystem
- Custom Common Dialogs Using SetWindowsHookEx
- DDDW Tips and Tricks
- OLE - Extending the Capabilities of PowerBuilder
- DataWindow.NET How To: Data Entry Form
- Book Excerpt: Sybase Adaptive Server Anywhere
- Sybase ASE 12.5 Performance and Tuning
- Working with SOA & Web Services in PowerBuilder
- Office 2003 Toolbar: A New Look For Your Old PowerBuilder App
- Dynamically Creating DataWindow Objects
































