Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
624ce48e KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
624ce48e | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
624ce48e | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
624ce48e | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
624ce48e | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
624ce48e KL |
28 | */ |
29 | package jexer.bits; | |
30 | ||
31 | /** | |
32 | * The attributes used by a Cell: color, bold, blink, etc. | |
33 | */ | |
34 | public class CellAttributes { | |
35 | ||
d36057df KL |
36 | // ------------------------------------------------------------------------ |
37 | // Variables -------------------------------------------------------------- | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
624ce48e | 40 | /** |
7b5261bc KL |
41 | * Bold attribute. |
42 | */ | |
43 | private boolean bold; | |
44 | ||
d36057df KL |
45 | /** |
46 | * Blink attribute. | |
47 | */ | |
48 | private boolean blink; | |
49 | ||
50 | /** | |
51 | * Reverse attribute. | |
52 | */ | |
53 | private boolean reverse; | |
54 | ||
55 | /** | |
56 | * Underline attribute. | |
57 | */ | |
58 | private boolean underline; | |
59 | ||
60 | /** | |
61 | * Protected attribute. | |
62 | */ | |
63 | private boolean protect; | |
64 | ||
65 | /** | |
66 | * Foreground color. Color.WHITE, Color.RED, etc. | |
67 | */ | |
68 | private Color foreColor; | |
69 | ||
70 | /** | |
71 | * Background color. Color.WHITE, Color.RED, etc. | |
72 | */ | |
73 | private Color backColor; | |
74 | ||
051e2913 KL |
75 | /** |
76 | * Foreground color as 24-bit RGB value. Negative value means not set. | |
77 | */ | |
78 | private int foreColorRGB = -1; | |
79 | ||
80 | /** | |
81 | * Background color as 24-bit RGB value. Negative value means not set. | |
82 | */ | |
83 | private int backColorRGB = -1; | |
84 | ||
d36057df KL |
85 | // ------------------------------------------------------------------------ |
86 | // Constructors ----------------------------------------------------------- | |
87 | // ------------------------------------------------------------------------ | |
88 | ||
89 | /** | |
90 | * Public constructor sets default values of the cell to white-on-black, | |
91 | * no bold/blink/reverse/underline/protect. | |
92 | * | |
93 | * @see #reset() | |
94 | */ | |
95 | public CellAttributes() { | |
96 | reset(); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Public constructor makes a copy from another instance. | |
101 | * | |
102 | * @param that another CellAttributes instance | |
103 | * @see #reset() | |
104 | */ | |
105 | public CellAttributes(final CellAttributes that) { | |
106 | setTo(that); | |
107 | } | |
108 | ||
109 | // ------------------------------------------------------------------------ | |
110 | // CellAttributes --------------------------------------------------------- | |
111 | // ------------------------------------------------------------------------ | |
112 | ||
7b5261bc KL |
113 | /** |
114 | * Getter for bold. | |
115 | * | |
116 | * @return bold value | |
117 | */ | |
7c870d89 | 118 | public final boolean isBold() { |
7b5261bc KL |
119 | return bold; |
120 | } | |
121 | ||
122 | /** | |
123 | * Setter for bold. | |
124 | * | |
125 | * @param bold new bold value | |
126 | */ | |
127 | public final void setBold(final boolean bold) { | |
128 | this.bold = bold; | |
129 | } | |
130 | ||
7b5261bc KL |
131 | /** |
132 | * Getter for blink. | |
133 | * | |
134 | * @return blink value | |
135 | */ | |
7c870d89 | 136 | public final boolean isBlink() { |
7b5261bc KL |
137 | return blink; |
138 | } | |
139 | ||
140 | /** | |
141 | * Setter for blink. | |
142 | * | |
143 | * @param blink new blink value | |
144 | */ | |
145 | public final void setBlink(final boolean blink) { | |
146 | this.blink = blink; | |
147 | } | |
148 | ||
7b5261bc KL |
149 | /** |
150 | * Getter for reverse. | |
151 | * | |
152 | * @return reverse value | |
153 | */ | |
7c870d89 | 154 | public final boolean isReverse() { |
7b5261bc KL |
155 | return reverse; |
156 | } | |
157 | ||
158 | /** | |
159 | * Setter for reverse. | |
160 | * | |
161 | * @param reverse new reverse value | |
162 | */ | |
163 | public final void setReverse(final boolean reverse) { | |
164 | this.reverse = reverse; | |
165 | } | |
166 | ||
624ce48e | 167 | /** |
7b5261bc KL |
168 | * Getter for underline. |
169 | * | |
170 | * @return underline value | |
624ce48e | 171 | */ |
7c870d89 | 172 | public final boolean isUnderline() { |
7b5261bc KL |
173 | return underline; |
174 | } | |
624ce48e KL |
175 | |
176 | /** | |
7b5261bc KL |
177 | * Setter for underline. |
178 | * | |
179 | * @param underline new underline value | |
624ce48e | 180 | */ |
7b5261bc KL |
181 | public final void setUnderline(final boolean underline) { |
182 | this.underline = underline; | |
183 | } | |
624ce48e | 184 | |
624ce48e | 185 | /** |
7b5261bc KL |
186 | * Getter for protect. |
187 | * | |
188 | * @return protect value | |
624ce48e | 189 | */ |
7c870d89 | 190 | public final boolean isProtect() { |
7b5261bc KL |
191 | return protect; |
192 | } | |
193 | ||
194 | /** | |
195 | * Setter for protect. | |
196 | * | |
197 | * @param protect new protect value | |
198 | */ | |
199 | public final void setProtect(final boolean protect) { | |
200 | this.protect = protect; | |
201 | } | |
624ce48e | 202 | |
7b5261bc KL |
203 | /** |
204 | * Getter for foreColor. | |
205 | * | |
206 | * @return foreColor value | |
207 | */ | |
208 | public final Color getForeColor() { | |
209 | return foreColor; | |
210 | } | |
211 | ||
212 | /** | |
213 | * Setter for foreColor. | |
214 | * | |
215 | * @param foreColor new foreColor value | |
216 | */ | |
217 | public final void setForeColor(final Color foreColor) { | |
218 | this.foreColor = foreColor; | |
219 | } | |
624ce48e | 220 | |
7b5261bc KL |
221 | /** |
222 | * Getter for backColor. | |
223 | * | |
224 | * @return backColor value | |
225 | */ | |
226 | public final Color getBackColor() { | |
227 | return backColor; | |
228 | } | |
229 | ||
230 | /** | |
231 | * Setter for backColor. | |
232 | * | |
233 | * @param backColor new backColor value | |
234 | */ | |
235 | public final void setBackColor(final Color backColor) { | |
236 | this.backColor = backColor; | |
237 | } | |
624ce48e | 238 | |
051e2913 KL |
239 | /** |
240 | * Getter for foreColor RGB. | |
241 | * | |
242 | * @return foreColor value. Negative means unset. | |
243 | */ | |
244 | public final int getForeColorRGB() { | |
245 | return foreColorRGB; | |
246 | } | |
247 | ||
248 | /** | |
249 | * Setter for foreColor RGB. | |
250 | * | |
3f499f9f | 251 | * @param foreColorRGB new foreColor RGB value |
051e2913 KL |
252 | */ |
253 | public final void setForeColorRGB(final int foreColorRGB) { | |
254 | this.foreColorRGB = foreColorRGB; | |
255 | } | |
256 | ||
257 | /** | |
258 | * Getter for backColor RGB. | |
259 | * | |
260 | * @return backColor value. Negative means unset. | |
261 | */ | |
262 | public final int getBackColorRGB() { | |
263 | return backColorRGB; | |
264 | } | |
265 | ||
266 | /** | |
267 | * Setter for backColor RGB. | |
268 | * | |
3f499f9f | 269 | * @param backColorRGB new backColor RGB value |
051e2913 KL |
270 | */ |
271 | public final void setBackColorRGB(final int backColorRGB) { | |
272 | this.backColorRGB = backColorRGB; | |
273 | } | |
274 | ||
275 | /** | |
276 | * See if this cell uses RGB or ANSI colors. | |
277 | * | |
278 | * @return true if this cell has a RGB color | |
279 | */ | |
280 | public final boolean isRGB() { | |
281 | return (foreColorRGB >= 0) || (backColorRGB >= 0); | |
282 | } | |
283 | ||
624ce48e | 284 | /** |
7b5261bc KL |
285 | * Set to default: white foreground on black background, no |
286 | * bold/underline/blink/rever/protect. | |
624ce48e KL |
287 | */ |
288 | public void reset() { | |
051e2913 KL |
289 | bold = false; |
290 | blink = false; | |
291 | reverse = false; | |
292 | underline = false; | |
293 | protect = false; | |
294 | foreColor = Color.WHITE; | |
295 | backColor = Color.BLACK; | |
296 | foreColorRGB = -1; | |
297 | backColorRGB = -1; | |
624ce48e | 298 | } |
15ea4d73 | 299 | |
624ce48e | 300 | /** |
7b5261bc KL |
301 | * Comparison check. All fields must match to return true. |
302 | * | |
303 | * @param rhs another CellAttributes instance | |
304 | * @return true if all fields are equal | |
624ce48e KL |
305 | */ |
306 | @Override | |
7b5261bc KL |
307 | public boolean equals(final Object rhs) { |
308 | if (!(rhs instanceof CellAttributes)) { | |
309 | return false; | |
310 | } | |
624ce48e | 311 | |
7b5261bc | 312 | CellAttributes that = (CellAttributes) rhs; |
6358f6e5 KL |
313 | return ((foreColor == that.foreColor) |
314 | && (backColor == that.backColor) | |
051e2913 KL |
315 | && (foreColorRGB == that.foreColorRGB) |
316 | && (backColorRGB == that.backColorRGB) | |
6358f6e5 | 317 | && (bold == that.bold) |
7b5261bc KL |
318 | && (reverse == that.reverse) |
319 | && (underline == that.underline) | |
6358f6e5 KL |
320 | && (blink == that.blink) |
321 | && (protect == that.protect)); | |
624ce48e KL |
322 | } |
323 | ||
e826b451 KL |
324 | /** |
325 | * Hashcode uses all fields in equals(). | |
326 | * | |
327 | * @return the hash | |
328 | */ | |
329 | @Override | |
330 | public int hashCode() { | |
331 | int A = 13; | |
332 | int B = 23; | |
333 | int hash = A; | |
334 | hash = (B * hash) + (bold ? 1 : 0); | |
335 | hash = (B * hash) + (blink ? 1 : 0); | |
336 | hash = (B * hash) + (underline ? 1 : 0); | |
337 | hash = (B * hash) + (reverse ? 1 : 0); | |
338 | hash = (B * hash) + (protect ? 1 : 0); | |
339 | hash = (B * hash) + foreColor.hashCode(); | |
340 | hash = (B * hash) + backColor.hashCode(); | |
051e2913 KL |
341 | hash = (B * hash) + foreColorRGB; |
342 | hash = (B * hash) + backColorRGB; | |
e826b451 KL |
343 | return hash; |
344 | } | |
345 | ||
624ce48e | 346 | /** |
7b5261bc KL |
347 | * Set my field values to that's field. |
348 | * | |
349 | * @param rhs another CellAttributes instance | |
624ce48e | 350 | */ |
e826b451 | 351 | public void setTo(final Object rhs) { |
7b5261bc KL |
352 | CellAttributes that = (CellAttributes) rhs; |
353 | ||
051e2913 KL |
354 | this.bold = that.bold; |
355 | this.blink = that.blink; | |
356 | this.reverse = that.reverse; | |
357 | this.underline = that.underline; | |
358 | this.protect = that.protect; | |
359 | this.foreColor = that.foreColor; | |
360 | this.backColor = that.backColor; | |
361 | this.foreColorRGB = that.foreColorRGB; | |
362 | this.backColorRGB = that.backColorRGB; | |
7b5261bc KL |
363 | } |
364 | ||
365 | /** | |
366 | * Make human-readable description of this CellAttributes. | |
367 | * | |
368 | * @return displayable String | |
624ce48e KL |
369 | */ |
370 | @Override | |
371 | public String toString() { | |
051e2913 KL |
372 | if ((foreColorRGB >= 0) || (backColorRGB >= 0)) { |
373 | return String.format("RGB: #%06x on #%06x", | |
374 | (foreColorRGB & 0xFFFFFF), | |
375 | (backColorRGB & 0xFFFFFF)); | |
376 | } | |
a0d734e6 KL |
377 | return String.format("%s%s%s on %s", (bold == true ? "bold " : ""), |
378 | (blink == true ? "blink " : ""), foreColor, backColor); | |
624ce48e KL |
379 | } |
380 | ||
381 | } |