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;
65 // ------------------------------------------------------------------------
66 // Variables --------------------------------------------------------------
67 // ------------------------------------------------------------------------
72 private int flags
= 0;
75 * Foreground color. Color.WHITE, Color.RED, etc.
77 private Color foreColor
= Color
.WHITE
;
80 * Background color. Color.WHITE, Color.RED, etc.
82 private Color backColor
= Color
.BLACK
;
85 * Foreground color as 24-bit RGB value. Negative value means not set.
87 private int foreColorRGB
= -1;
90 * Background color as 24-bit RGB value. Negative value means not set.
92 private int backColorRGB
= -1;
94 // ------------------------------------------------------------------------
95 // Constructors -----------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * Public constructor sets default values of the cell to white-on-black,
100 * no bold/blink/reverse/underline/protect.
104 public CellAttributes() {
109 * Public constructor makes a copy from another instance.
111 * @param that another CellAttributes instance
114 public CellAttributes(final CellAttributes that
) {
118 // ------------------------------------------------------------------------
119 // CellAttributes ---------------------------------------------------------
120 // ------------------------------------------------------------------------
127 public final boolean isBold() {
128 return ((flags
& BOLD
) == 0 ?
false : true);
134 * @param bold new bold value
136 public final void setBold(final boolean bold
) {
147 * @return blink value
149 public final boolean isBlink() {
150 return ((flags
& BLINK
) == 0 ?
false : true);
156 * @param blink new blink value
158 public final void setBlink(final boolean blink
) {
167 * Getter for reverse.
169 * @return reverse value
171 public final boolean isReverse() {
172 return ((flags
& REVERSE
) == 0 ?
false : true);
176 * Setter for reverse.
178 * @param reverse new reverse value
180 public final void setReverse(final boolean reverse
) {
189 * Getter for underline.
191 * @return underline value
193 public final boolean isUnderline() {
194 return ((flags
& UNDERLINE
) == 0 ?
false : true);
198 * Setter for underline.
200 * @param underline new underline value
202 public final void setUnderline(final boolean underline
) {
211 * Getter for protect.
213 * @return protect value
215 public final boolean isProtect() {
216 return ((flags
& PROTECT
) == 0 ?
false : true);
220 * Setter for protect.
222 * @param protect new protect value
224 public final void setProtect(final boolean protect
) {
233 * Getter for foreColor.
235 * @return foreColor value
237 public final Color
getForeColor() {
242 * Setter for foreColor.
244 * @param foreColor new foreColor value
246 public final void setForeColor(final Color foreColor
) {
247 this.foreColor
= foreColor
;
251 * Getter for backColor.
253 * @return backColor value
255 public final Color
getBackColor() {
260 * Setter for backColor.
262 * @param backColor new backColor value
264 public final void setBackColor(final Color backColor
) {
265 this.backColor
= backColor
;
269 * Getter for foreColor RGB.
271 * @return foreColor value. Negative means unset.
273 public final int getForeColorRGB() {
278 * Setter for foreColor RGB.
280 * @param foreColorRGB new foreColor RGB value
282 public final void setForeColorRGB(final int foreColorRGB
) {
283 this.foreColorRGB
= foreColorRGB
;
287 * Getter for backColor RGB.
289 * @return backColor value. Negative means unset.
291 public final int getBackColorRGB() {
296 * Setter for backColor RGB.
298 * @param backColorRGB new backColor RGB value
300 public final void setBackColorRGB(final int backColorRGB
) {
301 this.backColorRGB
= backColorRGB
;
305 * See if this cell uses RGB or ANSI colors.
307 * @return true if this cell has a RGB color
309 public final boolean isRGB() {
310 return (foreColorRGB
>= 0) || (backColorRGB
>= 0);
314 * Set to default: white foreground on black background, no
315 * bold/underline/blink/rever/protect.
317 public void reset() {
319 foreColor
= Color
.WHITE
;
320 backColor
= Color
.BLACK
;
326 * Comparison check. All fields must match to return true.
328 * @param rhs another CellAttributes instance
329 * @return true if all fields are equal
332 public boolean equals(final Object rhs
) {
333 if (!(rhs
instanceof CellAttributes
)) {
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
));
346 * Hashcode uses all fields in equals().
351 public int hashCode() {
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
;
364 * Set my field values to that's field.
366 * @param rhs another CellAttributes instance
368 public void setTo(final Object rhs
) {
369 CellAttributes that
= (CellAttributes
) rhs
;
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
;
379 * Make human-readable description of this CellAttributes.
381 * @return displayable String
384 public String
toString() {
385 if ((foreColorRGB
>= 0) || (backColorRGB
>= 0)) {
386 return String
.format("RGB: #%06x on #%06x",
387 (foreColorRGB
& 0xFFFFFF),
388 (backColorRGB
& 0xFFFFFF));
390 return String
.format("%s%s%s on %s", (isBold() ?
"bold " : ""),
391 (isBlink() ?
"blink " : ""), foreColor
, backColor
);