The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
ZXColors.h
Go to the documentation of this file.
1
2/* **************************************************************************/
24#ifndef ZXCOLORS
25#define ZXCOLORS
26
27#include <vector>
30
31namespace zxeco
32{
33
34/* ***************************************************************************
35*
36* Color system for the screen
37*
38* We have different types of color classes (plus some commodity constants).
39*
40*******************************************************************************/
41
42/* ------------------------------ Constants --------------------------------- */
43
46enum ColorBaseType: uint8_t {
48 BLACK = 0, BLUE, RED, MAGENTA, GREEN, CYAN, YELLOW, WHITE,
51
60 NUMPRINTCOLORS
61 };
62
65enum ColorModVal: uint8_t {
67 OFF = 0, ON = 1,
70 };
71
73
76extern const RGBPalette kZXPalette;
77
78
79/* ------------------------------ Flash, Bright, Inverse, Over -------------- */
80
82class Flash
83{
84 public:
85
86 explicit Flash(ColorModVal cmv);
87 explicit Flash(bool f = true):val_{static_cast<ColorModVal>(f)} {}
88
89 operator ColorModVal(void) const noexcept { return(val_); }
90
91
92 private:
93
94 ColorModVal val_;
95};
96
97
99class Bright
100{
101 public:
102
103 explicit Bright(ColorModVal cmv);
104 explicit Bright(bool b = true):val_{static_cast<ColorModVal>(b)} {}
105
106 operator ColorModVal(void) const noexcept { return(val_); }
107
108
109 private:
110
111 ColorModVal val_;
112};
113
114
117{
118 public:
119
120 explicit Inverse(ColorModVal cmv);
121 explicit Inverse(bool i = true):val_{static_cast<ColorModVal>(i)} {}
122
123 operator ColorModVal(void) const noexcept { return(val_); }
124
125
126 private:
127
128 ColorModVal val_;
129};
130
131
133class Over
134{
135 public:
136
137 explicit Over(ColorModVal cmv);
138 explicit Over(bool o = true):val_{static_cast<ColorModVal>(o)} {}
139
140 operator ColorModVal(void) const noexcept { return(val_); }
141
142
143 private:
144
145 ColorModVal val_;
146};
147
148
149
150/* ----------------------------- BasicColor --------------------------------- */
151
154{
155 public:
156
159
163 /* --------------- Methods ------------------ */
164
165 operator ColorBaseType(void) const noexcept { return(color_); }
167
171 BasicColor contrasting(void) const noexcept
172 { return(BasicColor{ color_ > MAGENTA ? BLACK : WHITE }); }
175 void setNext(bool circular = true);
177
180 std::string to_string(void) const;
182
183 private:
184
185 ColorBaseType color_;
186};
187
188
189/* ------------------------------ BrightColor ---------------------------- */
190
192
195{
196 public:
197
198 /* -------------- Types, consts., etc. ------------------- */
199
202
205
206 /* -------------- Constructors ------------------- */
207
208 BrightColor(BasicColor bc = BLACK, bool br = false): basicc_{bc},
209 brightc_{br} {}
211
214
216 BrightColor(const RGBColor & col):
217 BrightColor{static_cast<RGBPalette::Index>(
218 kZXPalette.closestColor(col))} {}
220
222 /* -------------- Methods ------------------- */
224 BasicColor color(void) const noexcept { return(basicc_); }
227 bool bright(void) const noexcept { return(brightc_); }
230 RGBPalette::Index linear(void) const noexcept
231 { return( static_cast<RGBPalette::Index>(basicc_) +
232 (brightc_ ? FIRSTLINEARCOLORWBRIGHT : 0));}
236 const RGBColor & rgbColor(void) const noexcept
237 { return(kZXPalette[linear()]); }
239
241 std::string to_string(void) const;
243
244 private:
245
246 BasicColor basicc_; // not using bitfields for some implementation-dependent
247 bool brightc_; // traits of that language construct
248};
249
250
251/* ------------------------------ PrintColor, Paper, Ink -------------------- */
252
255{
256 public:
257
260
263 PrintColor(const BasicColor & bc) noexcept:
264 PrintColor{static_cast<ColorBaseType>(bc)} {}
266
269 /* --------------- Methods ------------------ */
271 operator ColorBaseType(void) const noexcept { return(color_); }
273
276 BasicColor basic(void) const;
278
279 bool isBasic(void) const noexcept { return(color_ < NUMBASICCOLORS); }
281
282 std::string to_string(void) const;
284
285 private:
286
287 ColorBaseType color_;
288};
289
290
292class Paper: public PrintColor
293{
294 public:
295
296 explicit Paper(PrintColor p):PrintColor{p} {}
297};
298
299
301class Ink: public PrintColor
302{
303 public:
304
305 explicit Ink(PrintColor p):PrintColor{p} {}
306};
307
308
309/* ------------------------------ AttrColors ------------------------------- */
310
312
315{
316 public:
317
318 /* -------------- Constructors ------------------- */
319
320 AttrColors(void): flashc_{OFF},brightc_{OFF},paperc_{WHITE},inkc_{BLACK} {}
322
323 AttrColors(Flash f, Bright b, Paper p, Ink i): flashc_{f},
324 brightc_{b},
325 paperc_{p},
326 inkc_{i} {}
328
329 AttrColors(uint8_t attr);
331
332 /* -------------- Methods ------------------- */
333
334 uint8_t toAttr(void) const;
336
339 ColorModVal flash(void) const noexcept { return(flashc_); }
341
342 ColorModVal bright(void) const noexcept { return(brightc_); }
344
345 PrintColor paper(void) const noexcept { return(paperc_); }
347
348 PrintColor ink(void) const noexcept { return(inkc_); }
350
351 bool rgb(RGBColor & ink, RGBColor & paper) const;
353
354 void setFlash(ColorModVal f) noexcept { flashc_ = f; }
356
357 void setFlash(bool f = true) noexcept
359 { flashc_ = static_cast<ColorModVal>(f); }
360
361 void setBright(ColorModVal b) noexcept { brightc_ = b; }
363
364 void setBright(bool b = true) noexcept
366 { brightc_ = static_cast<ColorModVal>(b); }
367
368 void setPaper(PrintColor p) noexcept { paperc_ = p; }
370
371 void setInk(PrintColor i) noexcept { inkc_ = i; }
373
374 void swapPaperAndInk(void) noexcept { std::swap(paperc_,inkc_); }
376
377 AttrColors swapped(void) const noexcept
379 { AttrColors res{*this}; res.swapPaperAndInk(); return(res); }
380
381 void adjustByExisting(const AttrColors & ex);
383
402 std::string to_string(bool shorttxt = false) const;
404
405 private:
406
407 ColorModVal flashc_;
408 ColorModVal brightc_;
409 PrintColor paperc_;
410 PrintColor inkc_;
411};
412
413
414/* ------------------------------ Colors --------------------------- */
415
417
419class Colors: public AttrColors
420{
421 public:
422
423 Colors(void): AttrColors{},inv_{OFF},ov_{OFF} {}
425
427 inv_{inv},ov_{ov} {}
429
430
431 ColorModVal inverse(void) const noexcept { return(inv_); }
433
434 void setInverse(ColorModVal inv) noexcept { inv_ = inv; }
436
437 void setInverse(bool i = true) noexcept
439 { inv_ = static_cast<ColorModVal>(i); }
440
441 ColorModVal over(void) const noexcept { return(ov_); }
443
444 void setOver(ColorModVal ov) noexcept { ov_ = ov; }
446
447 void setOver(bool o = true) noexcept { ov_ = static_cast<ColorModVal>(o); }
449
450 Colors & operator<<(Paper p) noexcept { setPaper(p); return(*this); }
451 Colors & operator<<(Ink i) noexcept { setInk(i); return(*this); }
452 Colors & operator<<(Bright b) noexcept { setBright(b); return(*this); }
453 Colors & operator<<(Flash f) noexcept { setFlash(f); return(*this); }
454 Colors & operator<<(Inverse f) noexcept { setInverse(f); return(*this); }
455 Colors & operator<<(Over o) noexcept { setOver(o); return(*this); }
456
457 std::string to_string(void) const
458 { return("Colors " + AttrColors::to_string() + ", inverse " +
459 (inv_ == ON ? "ON" : "OFF") + ", over " +
460 (ov_ == ON ? "ON" : "OFF")); }
462
463 private:
464
465 ColorModVal inv_;
466 ColorModVal ov_;
467};
468
469
470
471} // end namespace zxeco
472
473#endif
474 // ZXColors
476
uint32_t Size
Size of a palette.
Definition: ColorImages.h:151
Size Index
Index of a color in a palette.
Definition: ColorImages.h:154
A RGB color with 8 bits per component.
Definition: ColorImages.h:47
A palette defining a number of RGB colors.
Definition: ColorImages.h:147
PrintColor(ColorBaseType cbt=BLACK)
Default constructor and from a constant color. Call base constructor.
Colors(AttrColors cc, Inverse inv, Over ov)
Constructor from an attribute specification plus inverse and over.
Definition: ZXColors.h:426
BasicColor basic(void) const
Conversion to BasicColor, if a valid one; otherwise, exception.
static constexpr RGBPalette::Size NUMLINEARCOLORS
Number of linear colors. Black is counted twice, being equal w & w/o br.
Definition: ZXColors.h:201
ColorModVal bright(void) const noexcept
Get the bright element.
Definition: ZXColors.h:342
std::string to_string(bool shorttxt=false) const
Return a descriptive text of this color specification.
ColorModVal inverse(void) const noexcept
Get the inverse mode.
Definition: ZXColors.h:431
std::string to_string(void) const
Return a text with the information of the colors.
Definition: ZXColors.h:457
std::string to_string(void) const
Return the name for this color.
BasicColor(ColorBaseType cbt=BLACK)
Default constructor from a given base color.
void setPaper(PrintColor p) noexcept
Change the paper element.
Definition: ZXColors.h:368
AttrColors(void)
Default constructor: no bright, no flash, black ink, white paper.
Definition: ZXColors.h:320
bool isBasic(void) const noexcept
Return TRUE if the color is a basic one and not CONTRAST or TRANSP.
Definition: ZXColors.h:277
BrightColor(RGBPalette::Index li)
Constructor from a linear index, from 0 to NUMLINEARCOLORS-1.
BrightColor(BasicColor bc=BLACK, bool br=false)
Default constructor and constructor from BasicColor.
Definition: ZXColors.h:208
void setInverse(ColorModVal inv) noexcept
Change the inverse mode.
Definition: ZXColors.h:434
bool bright(void) const noexcept
Get the bright level.
Definition: ZXColors.h:226
AttrColors(Flash f, Bright b, Paper p, Ink i)
Constructor from elements.
Definition: ZXColors.h:323
AttrColors swapped(void) const noexcept
< Return a version of these colors but with ink and paper swapped.
Definition: ZXColors.h:377
void swapPaperAndInk(void) noexcept
Swap paper and ink.
Definition: ZXColors.h:374
RGBPalette::Index linear(void) const noexcept
Return the index of this color in the ZX palette.
Definition: ZXColors.h:229
void adjustByExisting(const AttrColors &ex)
Change this attrs as though the ZX would use them for printing on EX.
PrintColor paper(void) const noexcept
Get the paper element.
Definition: ZXColors.h:345
const RGBColor & rgbColor(void) const noexcept
Return the RGBColor used to define this BrightColor.
Definition: ZXColors.h:234
void setFlash(ColorModVal f) noexcept
Change the flash element.
Definition: ZXColors.h:354
void setOver(bool o=true) noexcept
Change the over element to one of ON or OFF.
Definition: ZXColors.h:447
BrightColor(const RGBColor &col)
Constructor from a rgb color.
Definition: ZXColors.h:216
void setInverse(bool i=true) noexcept
< Change the inverse element to one of ON or OFF.
Definition: ZXColors.h:437
Colors(void)
Default constructor: default color attribute plus no inverse or over.
Definition: ZXColors.h:423
static constexpr RGBPalette::Index FIRSTLINEARCOLORWBRIGHT
First linear color that has bright.
Definition: ZXColors.h:204
PrintColor(const BasicColor &bc) noexcept
Constructor from the corresponding BasicColor.
Definition: ZXColors.h:263
std::string to_string(void) const
Return a descriptive text of the color.
BasicColor color(void) const noexcept
Get the basic color.
Definition: ZXColors.h:223
void setBright(ColorModVal b) noexcept
Change the bright element.
Definition: ZXColors.h:361
ColorModVal flash(void) const noexcept
Get the flash element.
Definition: ZXColors.h:339
void setOver(ColorModVal ov) noexcept
Change the over mode.
Definition: ZXColors.h:444
PrintColor ink(void) const noexcept
Get the ink element.
Definition: ZXColors.h:348
std::string to_string(void) const
Return a descriptive string for this color.
void setNext(bool circular=true)
Set this color to the next basic color.
void setBright(bool b=true) noexcept
< Change the bright element to one of ON or OFF.
Definition: ZXColors.h:364
void setInk(PrintColor i) noexcept
Change the ink element.
Definition: ZXColors.h:371
ColorModVal over(void) const noexcept
Get the over mode.
Definition: ZXColors.h:441
bool rgb(RGBColor &ink, RGBColor &paper) const
Get RGB colors for this attribute and return flash.
uint8_t toAttr(void) const
Convert the color specification into a byte for the attribute screen.
void setFlash(bool f=true) noexcept
< Change the flash element to one of ON or OFF.
Definition: ZXColors.h:357
BasicColor contrasting(void) const noexcept
Return the basic color that contrasts with this one.
Definition: ZXColors.h:170
AttrColors(uint8_t attr)
Constructor from the byte that contains the screen attribute.
Complete color specification for an attr (character) cell on the zx screen.
Definition: ZXColors.h:315
One of the 8 basic colors of the ZX, not considering bright or flash.
Definition: ZXColors.h:154
A value of bright that is different to the same value of flash, inv or over.
Definition: ZXColors.h:100
A basic color plus a bright level (on / off).
Definition: ZXColors.h:195
Color specification for a character cell, plus modes of printing.
Definition: ZXColors.h:420
A value of flash that is different to the same value of bright, inv or over.
Definition: ZXColors.h:83
A printcolor for ink as a different type from a color for paper.
Definition: ZXColors.h:302
A value of inv that is different to the same value of flash, bright or over.
Definition: ZXColors.h:117
A value of over that is different to the same value of flash, bright or inv.
Definition: ZXColors.h:134
A printcolor for paper as a different type from a color for ink.
Definition: ZXColors.h:293
A BasicColor or CONTRAST or TRANSPARENT, but not with bright or flash.
Definition: ZXColors.h:255
ColorBaseType
Main color constants.
Definition: ZXColors.h:46
@ ON
Basic states. Must be 0 and 1, respectively.
Definition: ZXColors.h:67
@ TRANSP
To respect existing attribute in screen.
Definition: ZXColors.h:68
@ TRANSPARENT
Definition: ZXColors.h:52
@ CONTRAST
Definition: ZXColors.h:56
@ NUMBASICCOLORS
Number of basic colors.
Definition: ZXColors.h:50
@ WHITE
A basic color.
Definition: ZXColors.h:48
The main namespace of the library, that spans across all the zx modules.
Definition: ZXChars.h:31
ColorModVal
Values of bright, flash, inverse and over.
Definition: ZXColors.h:65
const RGBPalette kZXPalette
The original palette of the ZX.