2
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
4
This file is part of PircBot.
6
This software is dual-licensed, allowing you to choose between the GNU
7
General Public License (GPL) and the www.jibble.org Commercial License.
8
Since the GPL may be too restrictive for use in a proprietary application,
9
a commercial license is also provided. Full license information can be
10
found at http://www.jibble.org/licenses/
15
package org.jibble.pircbot;
18
* The Colors class provides several static fields and methods that you may
19
* find useful when writing an IRC Bot.
21
* This class contains constants that are useful for formatting lines
22
* sent to IRC servers. These constants allow you to apply various
23
* formatting to the lines, such as colours, boldness, underlining
26
* The class contains static methods to remove colours and formatting
27
* from lines of IRC text.
29
* Here are some examples of how to use the contants from within a
30
* class that extends PircBot and imports org.jibble.pircbot.*;
32
* <pre> sendMessage("#cs", Colors.BOLD + "A bold hello!");
33
* <b>A bold hello!</b>
34
* sendMessage("#cs", Colors.RED + "Red" + Colors.NORMAL + " text");
35
* <font color="red">Red</font> text
36
* sendMessage("#cs", Colors.BOLD + Colors.RED + "Bold and red");
37
* <b><font color="red">Bold and red</font></b></pre>
39
* Please note that some IRC channels may be configured to reject any
40
* messages that use colours. Also note that older IRC clients may be
41
* unable to correctly display lines that contain colours and other
44
* Note that this class name has been spelt in the American style in
45
* order to remain consistent with the rest of the Java API.
49
* @author Paul James Mutton,
50
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
51
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
57
* Removes all previously applied color and formatting attributes.
59
public static final String NORMAL = "\u000f";
65
public static final String BOLD = "\u0002";
71
public static final String UNDERLINE = "\u001f";
75
* Reversed text (may be rendered as italic text in some clients).
77
public static final String REVERSE = "\u0016";
81
* White coloured text.
83
public static final String WHITE = "\u000300";
87
* Black coloured text.
89
public static final String BLACK = "\u000301";
93
* Dark blue coloured text.
95
public static final String DARK_BLUE = "\u000302";
99
* Dark green coloured text.
101
public static final String DARK_GREEN = "\u000303";
107
public static final String RED = "\u000304";
111
* Brown coloured text.
113
public static final String BROWN = "\u000305";
117
* Purple coloured text.
119
public static final String PURPLE = "\u000306";
123
* Olive coloured text.
125
public static final String OLIVE = "\u000307";
129
* Yellow coloured text.
131
public static final String YELLOW = "\u000308";
135
* Green coloured text.
137
public static final String GREEN = "\u000309";
141
* Teal coloured text.
143
public static final String TEAL = "\u000310";
147
* Cyan coloured text.
149
public static final String CYAN = "\u000311";
153
* Blue coloured text.
155
public static final String BLUE = "\u000312";
159
* Magenta coloured text.
161
public static final String MAGENTA = "\u000313";
165
* Dark gray coloured text.
167
public static final String DARK_GRAY = "\u000314";
171
* Light gray coloured text.
173
public static final String LIGHT_GRAY = "\u000315";
177
* This class should not be constructed.
185
* Removes all colours from a line of IRC text.
187
* @since PircBot 1.2.0
189
* @param line the input text.
191
* @return the same text, but with all colours removed.
193
public static String removeColors(String line) {
194
int length = line.length();
195
StringBuffer buffer = new StringBuffer();
198
char ch = line.charAt(i);
199
if (ch == '\u0003') {
201
// Skip "x" or "xy" (foreground color).
204
if (Character.isDigit(ch)) {
208
if (Character.isDigit(ch)) {
212
// Now skip ",x" or ",xy" (background color).
219
if (Character.isDigit(ch)) {
223
if (Character.isDigit(ch)) {
242
else if (ch == '\u000f') {
250
return buffer.toString();
255
* Remove formatting from a line of IRC text.
257
* @since PircBot 1.2.0
259
* @param line the input text.
261
* @return the same text, but without any bold, underlining, reverse, etc.
263
public static String removeFormatting(String line) {
264
int length = line.length();
265
StringBuffer buffer = new StringBuffer();
266
for (int i = 0; i < length; i++) {
267
char ch = line.charAt(i);
268
if (ch == '\u000f' || ch == '\u0002' || ch == '\u001f' || ch == '\u0016') {
269
// Don't add this character.
275
return buffer.toString();
280
* Removes all formatting and colours from a line of IRC text.
282
* @since PircBot 1.2.0
284
* @param line the input text.
286
* @return the same text, but without formatting and colour characters.
289
public static String removeFormattingAndColors(String line) {
290
return removeFormatting(removeColors(line));