Have you ever noticed that computer people like to describe colors in a variety of ways? Web developers use one format, REALbasic programmers another, and Mac OS X resource hackers use yet a third.
This week's tutorial will show you how to build a color converter. Beginners should enjoy the project, because it is easy to complete. Pros can benefit too, because the tool is very useful. (I use mine daily.)
Programmers have several different schemes for describing color. The web folks like to talk in hex, REALbasic programmers in integers, and the 'plist' files of Mac OS X use decimals. Below is a chart showing the color schemes and their numeric ranges.
| Web | $00 - $FF |
| REALbasic | 0 - 255 |
| 'plist' files | 0 - 1 |
Build the Interface
This week's interface is very simple to build. Open Window1 and drag the following controls into your interface:
You may arrange your interface in any fashion you desire. The idea of the application is that a user will push PushButton1 and choose a color from the color picker. Once a color has been selected, our project will convert the color into 3 color formats:
Pictured below is a sample interface. (Note how the extra StaticText and Line controls spruce up the interface.)
Add the Code
Like the interface, the code portion of this week's tutorial is very easy to recreate. Open the Action event of PushButton1 and add the following code:
Dim r,g,b as StringFirst, we allow the user to select a color using the standard color picker. On Mac OS X, it looks like this:
If the user selects a color, we convert the red, green, and blue values of the Color object into the three color formats: Hex, RB, and 'plist'. There is a bit more involved in the Hex conversion than the others. This is due to the fact that all numbers below 16 will return only one digit in hex. However, HTML requires double digits. The extra code tacks on a leading "0" (zero) if the string is only one character long.
The REALbasic color format has a range of 0 to 255. No conversion is necessary.
Finally, the 'plist' format is somewhat unique, because we must use Double type variables in this conversion. 'plist' colors fall in the range between 0 and 1. Since decimal points are involved, Double variables are required. The Format command cleans up the decimal points and puts them into a string which we then display.
Test and Build
That's it! To test, select Debug->Run. Once you have it down pat, select File->Build Application. This week's example is compatible with Mac OS Classic, Mac OS Carbon, and Microsoft Windows.
As usual, you may:
And, for the first time in this column... Microsoft Windows!