2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
32 * The attributes used by a Cell: color, bold, blink, etc.
34 public class CellAttributes
{
36 // ------------------------------------------------------------------------
37 // Constants --------------------------------------------------------------
38 // ------------------------------------------------------------------------
43 private static final int BOLD
= 0x01;
48 private static final int BLINK
= 0x02;
53 private static final int REVERSE
= 0x04;
56 * Underline attribute.
58 private static final int UNDERLINE
= 0x08;
61 * Protected attribute.
63 private static final int PROTECT
= 0x10;
66 // ------------------------------------------------------------------------
67 // Variables --------------------------------------------------------------
68 // ------------------------------------------------------------------------
73 private int flags
= 0;
76 * Foreground color. Color.WHITE, Color.RED, etc.
78 private Color foreColor
= Color
.WHITE
;
81 * Background color. Color.WHITE, Color.RED, etc.
83 private Color backColor
= Color
.BLACK
;
86 * Foreground color as 24-bit RGB value. Negative value means not set.
88 private int foreColorRGB
= -1;
91 * Background color as 24-bit RGB value. Negative value means not set.
93 private int backColorRGB
= -1;
95 // ------------------------------------------------------------------------
96 // Constructors -----------------------------------------------------------
97 // ------------------------------------------------------------------------
100 * Public constructor sets default values of the cell to white-on-black,
101 * no bold/blink/reverse/underline/protect.
105 public CellAttributes() {
110 * Public constructor makes a copy from another instance.
112 * @param that another CellAttributes instance
115 public CellAttributes(final CellAttributes that
) {
119 // ------------------------------------------------------------------------
120 // CellAttributes ---------------------------------------------------------
121 // ------------------------------------------------------------------------
128 public final boolean isBold() {
129 return ((flags
& BOLD
) == 0 ?
false : true);
135 * @param bold new bold value
137 public final void setBold(final boolean bold
) {
148 * @return blink value
150 public final boolean isBlink() {
151 return ((flags
& BLINK
) == 0 ?
false : true);
157 * @param blink new blink value
159 public final void setBlink(final boolean blink
) {
168 * Getter for reverse.
170 * @return reverse value
172 public final boolean isReverse() {
173 return ((flags
& REVERSE
) == 0 ?
false : true);
177 * Setter for reverse.
179 * @param reverse new reverse value
181 public final void setReverse(final boolean reverse
) {
190 * Getter for underline.
192 * @return underline value
194 public final boolean isUnderline() {
195 return ((flags
& UNDERLINE
) == 0 ?
false : true);
199 * Setter for underline.
201 * @param underline new underline value
203 public final void setUnderline(final boolean underline
) {
212 * Getter for protect.
214 * @return protect value
216 public final boolean isProtect() {
217 return ((flags
& PROTECT
) == 0 ?
false : true);
221 * Setter for protect.
223 * @param protect new protect value
225 public final void setProtect(final boolean protect
) {
234 * Getter for foreColor.
236 * @return foreColor value
238 public final Color
getForeColor() {
243 * Setter for foreColor.
245 * @param foreColor new foreColor value
247 public final void setForeColor(final Color foreColor
) {
248 this.foreColor
= foreColor
;
252 * Getter for backColor.
254 * @return backColor value
256 public final Color
getBackColor() {
261 * Setter for backColor.
263 * @param backColor new backColor value
265 public final void setBackColor(final Color backColor
) {
266 this.backColor
= backColor
;
270 * Getter for foreColor RGB.
272 * @return foreColor value. Negative means unset.
274 public final int getForeColorRGB() {
279 * Setter for foreColor RGB.
281 * @param foreColorRGB new foreColor RGB value
283 public final void setForeColorRGB(final int foreColorRGB
) {
284 this.foreColorRGB
= foreColorRGB
;
288 * Getter for backColor RGB.
290 * @return backColor value. Negative means unset.
292 public final int getBackColorRGB() {
297 * Setter for backColor RGB.
299 * @param backColorRGB new backColor RGB value
301 public final void setBackColorRGB(final int backColorRGB
) {
302 this.backColorRGB
= backColorRGB
;
306 * See if this cell uses RGB or ANSI colors.
308 * @return true if this cell has a RGB color
310 public final boolean isRGB() {
311 return (foreColorRGB
>= 0) || (backColorRGB
>= 0);
315 * Set to default: white foreground on black background, no
316 * bold/underline/blink/rever/protect.
318 public void reset() {
320 foreColor
= Color
.WHITE
;
321 backColor
= Color
.BLACK
;
327 * Comparison check. All fields must match to return true.
329 * @param rhs another CellAttributes instance
330 * @return true if all fields are equal
333 public boolean equals(final Object rhs
) {
334 if (!(rhs
instanceof CellAttributes
)) {
338 CellAttributes that
= (CellAttributes
) rhs
;
339 return ((flags
== that
.flags
)
340 && (foreColor
== that
.foreColor
)
341 && (backColor
== that
.backColor
)
342 && (foreColorRGB
== that
.foreColorRGB
)
343 && (backColorRGB
== that
.backColorRGB
));
347 * Hashcode uses all fields in equals().
352 public int hashCode() {
356 hash
= (B
* hash
) + flags
;
357 hash
= (B
* hash
) + foreColor
.hashCode();
358 hash
= (B
* hash
) + backColor
.hashCode();
359 hash
= (B
* hash
) + foreColorRGB
;
360 hash
= (B
* hash
) + backColorRGB
;
365 * Set my field values to that's field.
367 * @param rhs another CellAttributes instance
369 public void setTo(final Object rhs
) {
370 CellAttributes that
= (CellAttributes
) rhs
;
372 this.flags
= that
.flags
;
373 this.foreColor
= that
.foreColor
;
374 this.backColor
= that
.backColor
;
375 this.foreColorRGB
= that
.foreColorRGB
;
376 this.backColorRGB
= that
.backColorRGB
;
380 * Make human-readable description of this CellAttributes.
382 * @return displayable String
385 public String
toString() {
386 if ((foreColorRGB
>= 0) || (backColorRGB
>= 0)) {
387 return String
.format("RGB: #%06x on #%06x",
388 (foreColorRGB
& 0xFFFFFF),
389 (backColorRGB
& 0xFFFFFF));
391 return String
.format("%s%s%s on %s", (isBold() ?
"bold " : ""),
392 (isBlink() ?
"blink " : ""), foreColor
, backColor
);