|< Day Day Up >|
Hack 22 Colorize Text
Not only can you add formatting to text, but you can also add colors. Use them to make your messages stand out.
Color can be useful. With it, one can add extra emphasis to typed words, and make them look prettier and more eye-catching. Color can also be annoying. It is important to steer clear of color combinations that are unnecessary or hard to read. Color is most effective when used sparingly.
Most IRC clients support color, both in terms of displaying colors correctly and allowing you to type messages that contain colors. Colors are achieved by placing special control codes within messages. IRC clients that do not support colors are likely to remove these special codes. At worst, they may actually display the characters used to make up the control codes. This can look rather peculiar, as they are not visible ASCII characters.
For the majority of IRC clients that do support color, there are 16 basic colors to choose from. The appearance of these colors is entirely dependant on your IRC client, but fortunately there is agreement on what these colors are. They are listed in Table 4-1.
4.7.1 Using Colors in mIRC
mIRC fully supports the set of 16 basic colors. When you are writing a message, you can change the color by pressing Ctrl-K. This inserts a special control character into the message and pops up a small window with a list of all 16 colors, as shown in Figure 4-15. If you click on one of these colors, it will insert the number for that color. Anything you type after this will be displayed in your chosen color.
Figure 4-15. Sending colored messages from mIRC
When composing a message, you will notice that color does not show until you have sent it to the server. You will, however, be shown the color codes that will be used. You can use more than one color in the same message. Unlike HTML, you do not need to end these color code "tags"—if you want to start using a new color, just enter a new color code and mIRC will forget about the old color, as shown in Figure 4-16.
Figure 4-16. Using red and blue text in the same message
mIRC uses a pure white color for its default background, so it may not make sense to send white text. Unfortunately, not all IRC clients use the same background color, so this is another thing you will need to take into account when formatting text. Some clients, particularly those with Unix origins, use black backgrounds, so it may not even make sense to use black text!
Fortunately, you can specify the background color as well as the foreground color. This is done by adding another color code after the foreground color. A single comma is used to separate the two codes, as shown in Figure 4-17.
Figure 4-17. Specifying foreground and background colors
4.7.3 Problems with Coloring Numbers
If you wanted to color the number 123 in black, then you may fall foul of a slight problem with the color chooser in mIRC. The color code will be set as 1 (black), but because the next character in the message is also a number, it will result in 23 being colored in cyan (color code 11), rather than 123 being colored in black (color code 1). The solution to this problem is to ensure that all of your color codes contain two digits. That way, any subsequent digits will be interpreted as being part of the message text instead of a color code. If your desired color code consists of one digit, simply bulk it up by adding a leading zero.
4.7.4 Sending Colors from Bots
If you are sending colored messages from an IRC bot, you can insert the special characters into the string that you are going to send. The special character used to signify the start of a color code is ASCII character 0x03, which can be represented by the Unicode character \u0003.
Here are some examples of color codes being placed into Java Strings:
String plain = "A plain message"; String red1 = "\u000304A red message"; String red2 = "\u0003" + "04" + "A red message"; String whiteOnBlack = "\u000300,01" + "White text on black background";
4.7.5 Sending Colors from PircBot
The PircBot Java IRC API [Hack #35] includes a class called Colors, which can be used to add color to messages. It contains some useful constants so you don't have to keep remembering all the special color codes.
Creating a colored string with PircBot is more intuitive:
String red = Colors.RED + "A red message";
|< Day Day Up >|