#5 append RGB colors when -Djexer.ECMA48.rgbColor=true
[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 *
a2018e99 6 * Copyright (C) 2017 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
36 /**
7b5261bc
KL
37 * Bold attribute.
38 */
39 private boolean bold;
40
41 /**
42 * Getter for bold.
43 *
44 * @return bold value
45 */
7c870d89 46 public final boolean isBold() {
7b5261bc
KL
47 return bold;
48 }
49
50 /**
51 * Setter for bold.
52 *
53 * @param bold new bold value
54 */
55 public final void setBold(final boolean bold) {
56 this.bold = bold;
57 }
58
59 /**
60 * Blink attribute.
61 */
62 private boolean blink;
63
64 /**
65 * Getter for blink.
66 *
67 * @return blink value
68 */
7c870d89 69 public final boolean isBlink() {
7b5261bc
KL
70 return blink;
71 }
72
73 /**
74 * Setter for blink.
75 *
76 * @param blink new blink value
77 */
78 public final void setBlink(final boolean blink) {
79 this.blink = blink;
80 }
81
82 /**
83 * Reverse attribute.
84 */
85 private boolean reverse;
86
87 /**
88 * Getter for reverse.
89 *
90 * @return reverse value
91 */
7c870d89 92 public final boolean isReverse() {
7b5261bc
KL
93 return reverse;
94 }
95
96 /**
97 * Setter for reverse.
98 *
99 * @param reverse new reverse value
100 */
101 public final void setReverse(final boolean reverse) {
102 this.reverse = reverse;
103 }
104
105 /**
106 * Underline attribute.
624ce48e 107 */
7b5261bc 108 private boolean underline;
624ce48e
KL
109
110 /**
7b5261bc
KL
111 * Getter for underline.
112 *
113 * @return underline value
624ce48e 114 */
7c870d89 115 public final boolean isUnderline() {
7b5261bc
KL
116 return underline;
117 }
624ce48e
KL
118
119 /**
7b5261bc
KL
120 * Setter for underline.
121 *
122 * @param underline new underline value
624ce48e 123 */
7b5261bc
KL
124 public final void setUnderline(final boolean underline) {
125 this.underline = underline;
126 }
624ce48e
KL
127
128 /**
7b5261bc 129 * Protected attribute.
624ce48e 130 */
7b5261bc 131 private boolean protect;
624ce48e
KL
132
133 /**
7b5261bc
KL
134 * Getter for protect.
135 *
136 * @return protect value
624ce48e 137 */
7c870d89 138 public final boolean isProtect() {
7b5261bc
KL
139 return protect;
140 }
141
142 /**
143 * Setter for protect.
144 *
145 * @param protect new protect value
146 */
147 public final void setProtect(final boolean protect) {
148 this.protect = protect;
149 }
624ce48e
KL
150
151 /**
152 * Foreground color. Color.WHITE, Color.RED, etc.
153 */
7b5261bc
KL
154 private Color foreColor;
155
156 /**
157 * Getter for foreColor.
158 *
159 * @return foreColor value
160 */
161 public final Color getForeColor() {
162 return foreColor;
163 }
164
165 /**
166 * Setter for foreColor.
167 *
168 * @param foreColor new foreColor value
169 */
170 public final void setForeColor(final Color foreColor) {
171 this.foreColor = foreColor;
172 }
624ce48e
KL
173
174 /**
175 * Background color. Color.WHITE, Color.RED, etc.
176 */
7b5261bc
KL
177 private Color backColor;
178
179 /**
180 * Getter for backColor.
181 *
182 * @return backColor value
183 */
184 public final Color getBackColor() {
185 return backColor;
186 }
187
188 /**
189 * Setter for backColor.
190 *
191 * @param backColor new backColor value
192 */
193 public final void setBackColor(final Color backColor) {
194 this.backColor = backColor;
195 }
624ce48e
KL
196
197 /**
7b5261bc
KL
198 * Set to default: white foreground on black background, no
199 * bold/underline/blink/rever/protect.
624ce48e
KL
200 */
201 public void reset() {
7b5261bc
KL
202 bold = false;
203 blink = false;
204 reverse = false;
205 underline = false;
206 protect = false;
207 foreColor = Color.WHITE;
208 backColor = Color.BLACK;
624ce48e
KL
209 }
210
211 /**
7b5261bc
KL
212 * Public constructor sets default values of the cell to white-on-black,
213 * no bold/blink/reverse/underline/protect.
214 *
215 * @see #reset()
624ce48e
KL
216 */
217 public CellAttributes() {
7b5261bc 218 reset();
624ce48e
KL
219 }
220
15ea4d73
KL
221 /**
222 * Public constructor makes a copy from another instance.
223 *
224 * @param that another CellAttributes instance
225 * @see #reset()
226 */
227 public CellAttributes(final CellAttributes that) {
228 setTo(that);
229 }
230
624ce48e 231 /**
7b5261bc
KL
232 * Comparison check. All fields must match to return true.
233 *
234 * @param rhs another CellAttributes instance
235 * @return true if all fields are equal
624ce48e
KL
236 */
237 @Override
7b5261bc
KL
238 public boolean equals(final Object rhs) {
239 if (!(rhs instanceof CellAttributes)) {
240 return false;
241 }
624ce48e 242
7b5261bc 243 CellAttributes that = (CellAttributes) rhs;
6358f6e5
KL
244 return ((foreColor == that.foreColor)
245 && (backColor == that.backColor)
246 && (bold == that.bold)
7b5261bc
KL
247 && (reverse == that.reverse)
248 && (underline == that.underline)
6358f6e5
KL
249 && (blink == that.blink)
250 && (protect == that.protect));
624ce48e
KL
251 }
252
e826b451
KL
253 /**
254 * Hashcode uses all fields in equals().
255 *
256 * @return the hash
257 */
258 @Override
259 public int hashCode() {
260 int A = 13;
261 int B = 23;
262 int hash = A;
263 hash = (B * hash) + (bold ? 1 : 0);
264 hash = (B * hash) + (blink ? 1 : 0);
265 hash = (B * hash) + (underline ? 1 : 0);
266 hash = (B * hash) + (reverse ? 1 : 0);
267 hash = (B * hash) + (protect ? 1 : 0);
268 hash = (B * hash) + foreColor.hashCode();
269 hash = (B * hash) + backColor.hashCode();
270 return hash;
271 }
272
624ce48e 273 /**
7b5261bc
KL
274 * Set my field values to that's field.
275 *
276 * @param rhs another CellAttributes instance
624ce48e 277 */
e826b451 278 public void setTo(final Object rhs) {
7b5261bc
KL
279 CellAttributes that = (CellAttributes) rhs;
280
281 this.bold = that.bold;
282 this.blink = that.blink;
283 this.reverse = that.reverse;
284 this.underline = that.underline;
285 this.protect = that.protect;
286 this.foreColor = that.foreColor;
287 this.backColor = that.backColor;
288 }
289
290 /**
291 * Make human-readable description of this CellAttributes.
292 *
293 * @return displayable String
624ce48e
KL
294 */
295 @Override
296 public String toString() {
7b5261bc
KL
297 if (bold) {
298 return String.format("bold %s on %s",
299 foreColor, backColor);
300 } else {
301 return String.format("%s on %s", foreColor, backColor);
302 }
624ce48e
KL
303 }
304
305}