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