allow getMetas without synchronized
[fanfix.git] / src / jexer / bits / CellAttributes.java
CommitLineData
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 */
29package jexer.bits;
30
31/**
32 * The attributes used by a Cell: color, bold, blink, etc.
33 */
34public class CellAttributes {
35
d36057df 36 // ------------------------------------------------------------------------
027de5ae 37 // Constants --------------------------------------------------------------
d36057df
KL
38 // ------------------------------------------------------------------------
39
624ce48e 40 /**
7b5261bc
KL
41 * Bold attribute.
42 */
027de5ae 43 private static final int BOLD = 0x01;
7b5261bc 44
d36057df
KL
45 /**
46 * Blink attribute.
47 */
027de5ae 48 private static final int BLINK = 0x02;
d36057df
KL
49
50 /**
51 * Reverse attribute.
52 */
027de5ae 53 private static final int REVERSE = 0x04;
d36057df
KL
54
55 /**
56 * Underline attribute.
57 */
027de5ae 58 private static final int UNDERLINE = 0x08;
d36057df
KL
59
60 /**
61 * Protected attribute.
62 */
027de5ae
KL
63 private static final int PROTECT = 0x10;
64
027de5ae
KL
65 // ------------------------------------------------------------------------
66 // Variables --------------------------------------------------------------
67 // ------------------------------------------------------------------------
68
69 /**
70 * Boolean flags.
71 */
72 private int flags = 0;
d36057df
KL
73
74 /**
75 * Foreground color. Color.WHITE, Color.RED, etc.
76 */
027de5ae 77 private Color foreColor = Color.WHITE;
d36057df
KL
78
79 /**
80 * Background color. Color.WHITE, Color.RED, etc.
81 */
027de5ae 82 private Color backColor = Color.BLACK;
d36057df 83
051e2913
KL
84 /**
85 * Foreground color as 24-bit RGB value. Negative value means not set.
86 */
87 private int foreColorRGB = -1;
88
89 /**
90 * Background color as 24-bit RGB value. Negative value means not set.
91 */
92 private int backColorRGB = -1;
93
d36057df
KL
94 // ------------------------------------------------------------------------
95 // Constructors -----------------------------------------------------------
96 // ------------------------------------------------------------------------
97
98 /**
99 * Public constructor sets default values of the cell to white-on-black,
100 * no bold/blink/reverse/underline/protect.
101 *
102 * @see #reset()
103 */
104 public CellAttributes() {
027de5ae 105 // NOP
d36057df
KL
106 }
107
108 /**
109 * Public constructor makes a copy from another instance.
110 *
111 * @param that another CellAttributes instance
112 * @see #reset()
113 */
114 public CellAttributes(final CellAttributes that) {
115 setTo(that);
116 }
117
118 // ------------------------------------------------------------------------
119 // CellAttributes ---------------------------------------------------------
120 // ------------------------------------------------------------------------
121
7b5261bc
KL
122 /**
123 * Getter for bold.
124 *
125 * @return bold value
126 */
7c870d89 127 public final boolean isBold() {
027de5ae 128 return ((flags & BOLD) == 0 ? false : true);
7b5261bc
KL
129 }
130
131 /**
132 * Setter for bold.
133 *
134 * @param bold new bold value
135 */
136 public final void setBold(final boolean bold) {
027de5ae
KL
137 if (bold) {
138 flags |= BOLD;
139 } else {
140 flags &= ~BOLD;
141 }
7b5261bc
KL
142 }
143
7b5261bc
KL
144 /**
145 * Getter for blink.
146 *
147 * @return blink value
148 */
7c870d89 149 public final boolean isBlink() {
027de5ae 150 return ((flags & BLINK) == 0 ? false : true);
7b5261bc
KL
151 }
152
153 /**
154 * Setter for blink.
155 *
156 * @param blink new blink value
157 */
158 public final void setBlink(final boolean blink) {
027de5ae
KL
159 if (blink) {
160 flags |= BLINK;
161 } else {
162 flags &= ~BLINK;
163 }
7b5261bc
KL
164 }
165
7b5261bc
KL
166 /**
167 * Getter for reverse.
168 *
169 * @return reverse value
170 */
7c870d89 171 public final boolean isReverse() {
027de5ae 172 return ((flags & REVERSE) == 0 ? false : true);
7b5261bc
KL
173 }
174
175 /**
176 * Setter for reverse.
177 *
178 * @param reverse new reverse value
179 */
180 public final void setReverse(final boolean reverse) {
027de5ae
KL
181 if (reverse) {
182 flags |= REVERSE;
183 } else {
184 flags &= ~REVERSE;
185 }
7b5261bc
KL
186 }
187
624ce48e 188 /**
7b5261bc
KL
189 * Getter for underline.
190 *
191 * @return underline value
624ce48e 192 */
7c870d89 193 public final boolean isUnderline() {
027de5ae 194 return ((flags & UNDERLINE) == 0 ? false : true);
7b5261bc 195 }
624ce48e
KL
196
197 /**
7b5261bc
KL
198 * Setter for underline.
199 *
200 * @param underline new underline value
624ce48e 201 */
7b5261bc 202 public final void setUnderline(final boolean underline) {
027de5ae
KL
203 if (underline) {
204 flags |= UNDERLINE;
205 } else {
206 flags &= ~UNDERLINE;
207 }
7b5261bc 208 }
624ce48e 209
624ce48e 210 /**
7b5261bc
KL
211 * Getter for protect.
212 *
213 * @return protect value
624ce48e 214 */
7c870d89 215 public final boolean isProtect() {
027de5ae 216 return ((flags & PROTECT) == 0 ? false : true);
7b5261bc
KL
217 }
218
219 /**
220 * Setter for protect.
221 *
222 * @param protect new protect value
223 */
224 public final void setProtect(final boolean protect) {
027de5ae
KL
225 if (protect) {
226 flags |= PROTECT;
227 } else {
228 flags &= ~PROTECT;
229 }
7b5261bc 230 }
624ce48e 231
7b5261bc
KL
232 /**
233 * Getter for foreColor.
234 *
235 * @return foreColor value
236 */
237 public final Color getForeColor() {
238 return foreColor;
239 }
240
241 /**
242 * Setter for foreColor.
243 *
244 * @param foreColor new foreColor value
245 */
246 public final void setForeColor(final Color foreColor) {
247 this.foreColor = foreColor;
248 }
624ce48e 249
7b5261bc
KL
250 /**
251 * Getter for backColor.
252 *
253 * @return backColor value
254 */
255 public final Color getBackColor() {
256 return backColor;
257 }
258
259 /**
260 * Setter for backColor.
261 *
262 * @param backColor new backColor value
263 */
264 public final void setBackColor(final Color backColor) {
265 this.backColor = backColor;
266 }
624ce48e 267
051e2913
KL
268 /**
269 * Getter for foreColor RGB.
270 *
271 * @return foreColor value. Negative means unset.
272 */
273 public final int getForeColorRGB() {
274 return foreColorRGB;
275 }
276
277 /**
278 * Setter for foreColor RGB.
279 *
3f499f9f 280 * @param foreColorRGB new foreColor RGB value
051e2913
KL
281 */
282 public final void setForeColorRGB(final int foreColorRGB) {
283 this.foreColorRGB = foreColorRGB;
284 }
285
286 /**
287 * Getter for backColor RGB.
288 *
289 * @return backColor value. Negative means unset.
290 */
291 public final int getBackColorRGB() {
292 return backColorRGB;
293 }
294
295 /**
296 * Setter for backColor RGB.
297 *
3f499f9f 298 * @param backColorRGB new backColor RGB value
051e2913
KL
299 */
300 public final void setBackColorRGB(final int backColorRGB) {
301 this.backColorRGB = backColorRGB;
302 }
303
304 /**
305 * See if this cell uses RGB or ANSI colors.
306 *
307 * @return true if this cell has a RGB color
308 */
309 public final boolean isRGB() {
310 return (foreColorRGB >= 0) || (backColorRGB >= 0);
311 }
312
624ce48e 313 /**
7b5261bc
KL
314 * Set to default: white foreground on black background, no
315 * bold/underline/blink/rever/protect.
624ce48e
KL
316 */
317 public void reset() {
027de5ae 318 flags = 0;
051e2913
KL
319 foreColor = Color.WHITE;
320 backColor = Color.BLACK;
321 foreColorRGB = -1;
322 backColorRGB = -1;
624ce48e 323 }
15ea4d73 324
624ce48e 325 /**
7b5261bc
KL
326 * Comparison check. All fields must match to return true.
327 *
328 * @param rhs another CellAttributes instance
329 * @return true if all fields are equal
624ce48e
KL
330 */
331 @Override
7b5261bc
KL
332 public boolean equals(final Object rhs) {
333 if (!(rhs instanceof CellAttributes)) {
334 return false;
335 }
624ce48e 336
7b5261bc 337 CellAttributes that = (CellAttributes) rhs;
027de5ae
KL
338 return ((flags == that.flags)
339 && (foreColor == that.foreColor)
6358f6e5 340 && (backColor == that.backColor)
051e2913 341 && (foreColorRGB == that.foreColorRGB)
027de5ae 342 && (backColorRGB == that.backColorRGB));
624ce48e
KL
343 }
344
e826b451
KL
345 /**
346 * Hashcode uses all fields in equals().
347 *
348 * @return the hash
349 */
350 @Override
351 public int hashCode() {
352 int A = 13;
353 int B = 23;
354 int hash = A;
027de5ae 355 hash = (B * hash) + flags;
e826b451
KL
356 hash = (B * hash) + foreColor.hashCode();
357 hash = (B * hash) + backColor.hashCode();
051e2913
KL
358 hash = (B * hash) + foreColorRGB;
359 hash = (B * hash) + backColorRGB;
e826b451
KL
360 return hash;
361 }
362
624ce48e 363 /**
7b5261bc
KL
364 * Set my field values to that's field.
365 *
366 * @param rhs another CellAttributes instance
624ce48e 367 */
e826b451 368 public void setTo(final Object rhs) {
7b5261bc
KL
369 CellAttributes that = (CellAttributes) rhs;
370
027de5ae 371 this.flags = that.flags;
051e2913
KL
372 this.foreColor = that.foreColor;
373 this.backColor = that.backColor;
374 this.foreColorRGB = that.foreColorRGB;
375 this.backColorRGB = that.backColorRGB;
7b5261bc
KL
376 }
377
378 /**
379 * Make human-readable description of this CellAttributes.
380 *
381 * @return displayable String
624ce48e
KL
382 */
383 @Override
384 public String toString() {
051e2913
KL
385 if ((foreColorRGB >= 0) || (backColorRGB >= 0)) {
386 return String.format("RGB: #%06x on #%06x",
387 (foreColorRGB & 0xFFFFFF),
388 (backColorRGB & 0xFFFFFF));
389 }
027de5ae
KL
390 return String.format("%s%s%s on %s", (isBold() ? "bold " : ""),
391 (isBlink() ? "blink " : ""), foreColor, backColor);
624ce48e
KL
392 }
393
394}