Merge branch 'upstream' into subtree
[nikiroo-utils.git] / bits / CellAttributes.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.bits;
30
31 /**
32 * The attributes used by a Cell: color, bold, blink, etc.
33 */
34 public class CellAttributes {
35
36 // ------------------------------------------------------------------------
37 // Constants --------------------------------------------------------------
38 // ------------------------------------------------------------------------
39
40 /**
41 * Bold attribute.
42 */
43 private static final int BOLD = 0x01;
44
45 /**
46 * Blink attribute.
47 */
48 private static final int BLINK = 0x02;
49
50 /**
51 * Reverse attribute.
52 */
53 private static final int REVERSE = 0x04;
54
55 /**
56 * Underline attribute.
57 */
58 private static final int UNDERLINE = 0x08;
59
60 /**
61 * Protected attribute.
62 */
63 private static final int PROTECT = 0x10;
64
65 // ------------------------------------------------------------------------
66 // Variables --------------------------------------------------------------
67 // ------------------------------------------------------------------------
68
69 /**
70 * Boolean flags.
71 */
72 private int flags = 0;
73
74 /**
75 * Foreground color. Color.WHITE, Color.RED, etc.
76 */
77 private Color foreColor = Color.WHITE;
78
79 /**
80 * Background color. Color.WHITE, Color.RED, etc.
81 */
82 private Color backColor = Color.BLACK;
83
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
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() {
105 // NOP
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
122 /**
123 * Getter for bold.
124 *
125 * @return bold value
126 */
127 public final boolean isBold() {
128 return ((flags & BOLD) == 0 ? false : true);
129 }
130
131 /**
132 * Setter for bold.
133 *
134 * @param bold new bold value
135 */
136 public final void setBold(final boolean bold) {
137 if (bold) {
138 flags |= BOLD;
139 } else {
140 flags &= ~BOLD;
141 }
142 }
143
144 /**
145 * Getter for blink.
146 *
147 * @return blink value
148 */
149 public final boolean isBlink() {
150 return ((flags & BLINK) == 0 ? false : true);
151 }
152
153 /**
154 * Setter for blink.
155 *
156 * @param blink new blink value
157 */
158 public final void setBlink(final boolean blink) {
159 if (blink) {
160 flags |= BLINK;
161 } else {
162 flags &= ~BLINK;
163 }
164 }
165
166 /**
167 * Getter for reverse.
168 *
169 * @return reverse value
170 */
171 public final boolean isReverse() {
172 return ((flags & REVERSE) == 0 ? false : true);
173 }
174
175 /**
176 * Setter for reverse.
177 *
178 * @param reverse new reverse value
179 */
180 public final void setReverse(final boolean reverse) {
181 if (reverse) {
182 flags |= REVERSE;
183 } else {
184 flags &= ~REVERSE;
185 }
186 }
187
188 /**
189 * Getter for underline.
190 *
191 * @return underline value
192 */
193 public final boolean isUnderline() {
194 return ((flags & UNDERLINE) == 0 ? false : true);
195 }
196
197 /**
198 * Setter for underline.
199 *
200 * @param underline new underline value
201 */
202 public final void setUnderline(final boolean underline) {
203 if (underline) {
204 flags |= UNDERLINE;
205 } else {
206 flags &= ~UNDERLINE;
207 }
208 }
209
210 /**
211 * Getter for protect.
212 *
213 * @return protect value
214 */
215 public final boolean isProtect() {
216 return ((flags & PROTECT) == 0 ? false : true);
217 }
218
219 /**
220 * Setter for protect.
221 *
222 * @param protect new protect value
223 */
224 public final void setProtect(final boolean protect) {
225 if (protect) {
226 flags |= PROTECT;
227 } else {
228 flags &= ~PROTECT;
229 }
230 }
231
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 }
249
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 }
267
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 *
280 * @param foreColorRGB new foreColor RGB value
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 *
298 * @param backColorRGB new backColor RGB value
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
313 /**
314 * Set to default: white foreground on black background, no
315 * bold/underline/blink/rever/protect.
316 */
317 public void reset() {
318 flags = 0;
319 foreColor = Color.WHITE;
320 backColor = Color.BLACK;
321 foreColorRGB = -1;
322 backColorRGB = -1;
323 }
324
325 /**
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
330 */
331 @Override
332 public boolean equals(final Object rhs) {
333 if (!(rhs instanceof CellAttributes)) {
334 return false;
335 }
336
337 CellAttributes that = (CellAttributes) rhs;
338 return ((flags == that.flags)
339 && (foreColor == that.foreColor)
340 && (backColor == that.backColor)
341 && (foreColorRGB == that.foreColorRGB)
342 && (backColorRGB == that.backColorRGB));
343 }
344
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;
355 hash = (B * hash) + flags;
356 hash = (B * hash) + foreColor.hashCode();
357 hash = (B * hash) + backColor.hashCode();
358 hash = (B * hash) + foreColorRGB;
359 hash = (B * hash) + backColorRGB;
360 return hash;
361 }
362
363 /**
364 * Set my field values to that's field.
365 *
366 * @param rhs another CellAttributes instance
367 */
368 public void setTo(final Object rhs) {
369 CellAttributes that = (CellAttributes) rhs;
370
371 this.flags = that.flags;
372 this.foreColor = that.foreColor;
373 this.backColor = that.backColor;
374 this.foreColorRGB = that.foreColorRGB;
375 this.backColorRGB = that.backColorRGB;
376 }
377
378 /**
379 * Make human-readable description of this CellAttributes.
380 *
381 * @return displayable String
382 */
383 @Override
384 public String toString() {
385 if ((foreColorRGB >= 0) || (backColorRGB >= 0)) {
386 return String.format("RGB: #%06x on #%06x",
387 (foreColorRGB & 0xFFFFFF),
388 (backColorRGB & 0xFFFFFF));
389 }
390 return String.format("%s%s%s on %s", (isBold() ? "bold " : ""),
391 (isBlink() ? "blink " : ""), foreColor, backColor);
392 }
393
394 }