~psycojoker/+junk/cerkbot

« back to all changes in this revision

Viewing changes to src/org/jibble/pircbot/Colors.java

  • Committer: Laurent P
  • Date: 2008-09-11 19:53:20 UTC
  • Revision ID: psycojoker@gmail.com-20080911195320-7ne5l93cbtze4zk2
init cerkbot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
Copyright Paul James Mutton, 2001-2007, http://www.jibble.org/
 
3
 
 
4
This file is part of PircBot.
 
5
 
 
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/
 
11
 
 
12
*/
 
13
 
 
14
 
 
15
package org.jibble.pircbot;
 
16
 
 
17
/**
 
18
 * The Colors class provides several static fields and methods that you may
 
19
 * find useful when writing an IRC Bot.
 
20
 *  <p>
 
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
 
24
 * and reverse text.
 
25
 *  <p>
 
26
 * The class contains static methods to remove colours and formatting
 
27
 * from lines of IRC text.
 
28
 *  <p>
 
29
 * Here are some examples of how to use the contants from within a
 
30
 * class that extends PircBot and imports org.jibble.pircbot.*;
 
31
 * 
 
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>
 
38
 * 
 
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
 
42
 * control characters.
 
43
 *  <p>
 
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.
 
46
 *
 
47
 *
 
48
 * @since   0.9.12
 
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)
 
52
 */
 
53
public class Colors {
 
54
 
 
55
    
 
56
    /**
 
57
     * Removes all previously applied color and formatting attributes.
 
58
     */
 
59
    public static final String NORMAL = "\u000f";
 
60
 
 
61
 
 
62
    /**
 
63
     * Bold text.
 
64
     */
 
65
    public static final String BOLD = "\u0002";
 
66
    
 
67
 
 
68
    /**
 
69
     * Underlined text.
 
70
     */
 
71
    public static final String UNDERLINE = "\u001f";
 
72
 
 
73
 
 
74
    /**
 
75
     * Reversed text (may be rendered as italic text in some clients).
 
76
     */
 
77
    public static final String REVERSE = "\u0016";
 
78
    
 
79
 
 
80
    /**
 
81
     * White coloured text.
 
82
     */
 
83
    public static final String WHITE = "\u000300";
 
84
    
 
85
 
 
86
    /**
 
87
     * Black coloured text.
 
88
     */
 
89
    public static final String BLACK = "\u000301";
 
90
    
 
91
 
 
92
    /**
 
93
     * Dark blue coloured text.
 
94
     */
 
95
    public static final String DARK_BLUE = "\u000302";
 
96
    
 
97
 
 
98
    /**
 
99
     * Dark green coloured text.
 
100
     */
 
101
    public static final String DARK_GREEN = "\u000303";
 
102
    
 
103
 
 
104
    /**
 
105
     * Red coloured text.
 
106
     */
 
107
    public static final String RED = "\u000304";
 
108
    
 
109
 
 
110
    /**
 
111
     * Brown coloured text.
 
112
     */
 
113
    public static final String BROWN = "\u000305";
 
114
    
 
115
 
 
116
    /**
 
117
     * Purple coloured text.
 
118
     */
 
119
    public static final String PURPLE = "\u000306";
 
120
    
 
121
 
 
122
    /**
 
123
     * Olive coloured text.
 
124
     */
 
125
    public static final String OLIVE = "\u000307";
 
126
    
 
127
 
 
128
    /**
 
129
     * Yellow coloured text.
 
130
     */
 
131
    public static final String YELLOW = "\u000308";
 
132
    
 
133
 
 
134
    /**
 
135
     * Green coloured text.
 
136
     */
 
137
    public static final String GREEN = "\u000309";
 
138
    
 
139
 
 
140
    /**
 
141
     * Teal coloured text.
 
142
     */
 
143
    public static final String TEAL = "\u000310";
 
144
    
 
145
 
 
146
    /**
 
147
     * Cyan coloured text.
 
148
     */
 
149
    public static final String CYAN = "\u000311";
 
150
    
 
151
 
 
152
    /**
 
153
     * Blue coloured text.
 
154
     */
 
155
    public static final String BLUE = "\u000312";
 
156
    
 
157
 
 
158
    /**
 
159
     * Magenta coloured text.
 
160
     */
 
161
    public static final String MAGENTA = "\u000313";
 
162
    
 
163
 
 
164
    /**
 
165
     * Dark gray coloured text.
 
166
     */
 
167
    public static final String DARK_GRAY = "\u000314";
 
168
    
 
169
 
 
170
    /**
 
171
     * Light gray coloured text.
 
172
     */
 
173
    public static final String LIGHT_GRAY = "\u000315";
 
174
 
 
175
    
 
176
    /**
 
177
     * This class should not be constructed.
 
178
     */
 
179
    private Colors() {
 
180
        
 
181
    }
 
182
    
 
183
    
 
184
    /**
 
185
     * Removes all colours from a line of IRC text.
 
186
     * 
 
187
     * @since PircBot 1.2.0
 
188
     * 
 
189
     * @param line the input text.
 
190
     * 
 
191
     * @return the same text, but with all colours removed.
 
192
     */
 
193
    public static String removeColors(String line) {
 
194
        int length = line.length();
 
195
        StringBuffer buffer = new StringBuffer();
 
196
        int i = 0;
 
197
        while (i < length) {
 
198
            char ch = line.charAt(i);
 
199
            if (ch == '\u0003') {
 
200
                i++;
 
201
                // Skip "x" or "xy" (foreground color).
 
202
                if (i < length) {
 
203
                    ch = line.charAt(i);
 
204
                    if (Character.isDigit(ch)) {
 
205
                        i++;
 
206
                        if (i < length) {
 
207
                            ch = line.charAt(i);
 
208
                            if (Character.isDigit(ch)) {
 
209
                                i++;
 
210
                            }
 
211
                        }
 
212
                        // Now skip ",x" or ",xy" (background color).
 
213
                        if (i < length) {
 
214
                            ch = line.charAt(i);
 
215
                            if (ch == ',') {
 
216
                                i++;
 
217
                                if (i < length) {
 
218
                                    ch = line.charAt(i);
 
219
                                    if (Character.isDigit(ch)) {
 
220
                                        i++;
 
221
                                        if (i < length) {
 
222
                                            ch = line.charAt(i);
 
223
                                            if (Character.isDigit(ch)) {
 
224
                                                i++;
 
225
                                            }
 
226
                                        }
 
227
                                    }
 
228
                                    else {
 
229
                                        // Keep the comma.
 
230
                                        i--;
 
231
                                    }
 
232
                                }
 
233
                                else {
 
234
                                    // Keep the comma.
 
235
                                    i--;
 
236
                                }
 
237
                            }
 
238
                        }
 
239
                    }
 
240
                }
 
241
            }
 
242
            else if (ch == '\u000f') {
 
243
                i++;
 
244
            }
 
245
            else {
 
246
                buffer.append(ch);
 
247
                i++;
 
248
            }
 
249
        }
 
250
        return buffer.toString();
 
251
    }
 
252
    
 
253
    
 
254
    /**
 
255
     * Remove formatting from a line of IRC text.
 
256
     * 
 
257
     * @since PircBot 1.2.0
 
258
     * 
 
259
     * @param line the input text.
 
260
     * 
 
261
     * @return the same text, but without any bold, underlining, reverse, etc.
 
262
     */
 
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.
 
270
            }
 
271
            else {
 
272
                buffer.append(ch);
 
273
            }
 
274
        }
 
275
        return buffer.toString();
 
276
    }
 
277
    
 
278
    
 
279
    /**
 
280
     * Removes all formatting and colours from a line of IRC text.
 
281
     * 
 
282
     * @since PircBot 1.2.0
 
283
     *
 
284
     * @param line the input text.
 
285
     * 
 
286
     * @return the same text, but without formatting and colour characters.
 
287
     * 
 
288
     */
 
289
    public static String removeFormattingAndColors(String line) {
 
290
        return removeFormatting(removeColors(line));
 
291
    }
 
292
    
 
293
}