terminalgui  0.1.0
Widgets for your terminal, powered by Qt! Create textual GUI (TUI) in your console easily.
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Tg::Color Class Reference

Represents colors in a terminal. More...

#include <tgcolor.h>

Public Types

enum  Predefined {
  Predefined::Invalid = -1, Predefined::Empty = 0, Predefined::Black = 30, Predefined::Red = 31,
  Predefined::Green = 32, Predefined::Yellow = 33, Predefined::Blue = 34, Predefined::Magenta = 35,
  Predefined::Cyan = 36, Predefined::White = 37, Predefined::Gray = 90, Predefined::Pink = 91,
  LightRed = Pink, Predefined::LightGreen = 92, Predefined::LightYellow = 93, Predefined::LightBlue = 94,
  Predefined::LightMagenta = 95, Predefined::LightCyan = 96, Predefined::LightWhite = 97
}
 Contains predefined colors supported by all terminals, including the oldest ones. More...
 

Public Member Functions

 Color ()
 Constructs a color of type Predefined (it's set to Predefined::Invalid).
 
 Color (const Predefined predefined)
 Constructs a Color with one of the predefined colors. More...
 
 Color (const quint8 red, const quint8 green, const quint8 blue)
 Constructs a 24 bit color using red, green and blue components. More...
 
QString rgb () const
 Returns 24 bit color information in a format understood by the terminal. More...
 
quint8 red () const
 Returns intensity of red color. More...
 
quint8 green () const
 Returns intensity of green color. More...
 
quint8 blue () const
 Returns intensity of blue color. More...
 
Predefined predefined () const
 Returns a Predefined color value if Color is not 24 bit, or Predefined::Invalid otherwise.
 
bool isEmpty () const
 Returns true if a Predefined color is Predefined::Empty. More...
 
bool isPredefined () const
 Returns true is Color type is Predefined (as opposed to 24 bit color). More...
 
bool isTrueColor () const
 Returns true if Color is 24 bit (as opposed to Predefined). More...
 
bool operator== (const Color &other) const
 Returns true is other is the same.
 
bool operator!= (const Color &other) const
 Returns true if other is different.
 

Static Public Member Functions

static QString code (const Color &foreground, const Color &background=Color::Predefined::Empty)
 Returns ANSI-coded sequence, as understood by terminal, for given foreground color and background color. More...
 
static QString code (const Color &color, const bool isBackground, const bool forceTrueColor)
 Returns ANSI-coded sequence, as understood by terminal, for given color. More...
 
static QString end ()
 Returns ANSI sequence which ends color definition.
 

Detailed Description

Represents colors in a terminal.

In terminals, color is used mainly in 2 places:

Each character ("pixel") inside the terminal can have these 2 colors set completely independently. Use Color::code() to get a ready-made

Terminal GUI supports 2 color schemes: Predefined and 24 bit (true color). Currently Color class does not convert colors of one type to another.

Member Enumeration Documentation

◆ Predefined

enum Tg::Color::Predefined
strong

Contains predefined colors supported by all terminals, including the oldest ones.

A terminal emulator can override these colors based on user-chosen profile (TODO: verify this...).

Note
There is a difference between an Invalid color and an Empty one. With Empty, the terminal will supply a default color in it's place (usually black). With Invalid, code is specifically not defined and should not be replaced by a default one.
Todo:
Add automatic conversion between Predefined and 24 bit colors.
Enumerator
Invalid 

Color is not defined and cannot be drawn. Used to indicate unset colors.

Empty 

Color is defined as empty - default terminal color will be used.

Black 

Default black color, depends on terminal settings.

Red 

Default red color, depends on terminal settings.

Green 

Default green color, depends on terminal settings.

Yellow 

Default yellow color, depends on terminal settings.

Blue 

Default blue color, depends on terminal settings.

Magenta 

Default magenta color, depends on terminal settings.

Cyan 

Default cyan color, depends on terminal settings.

White 

Default white color, depends on terminal settings.

Gray 

Default gray color, depends on terminal settings.

Pink 

Default pink color, depends on terminal settings.

LightGreen 

Default lighter green color, depends on terminal settings.

LightYellow 

Default lighter yellow color, depends on terminal settings.

LightBlue 

Default lighter blue color, depends on terminal settings.

LightMagenta 

Default lighter magenta color, depends on terminal settings.

LightCyan 

Default lighter cyan color, depends on terminal settings.

LightWhite 

Default lighter white color, depends on terminal settings. How can white be any lighter? It's usually ligter than Predefined::Gray, but not as white as Predefined::White. It's weird

Constructor & Destructor Documentation

◆ Color() [1/2]

Tg::Color::Color ( const Predefined  predefined)

Constructs a Color with one of the predefined colors.

Note
Automatic conversion between Predefined and 24 bit color is not supported (yet?).

◆ Color() [2/2]

Tg::Color::Color ( const quint8  red,
const quint8  green,
const quint8  blue 
)

Constructs a 24 bit color using red, green and blue components.

Each variable should be a number between 0 and 255.

Note
Automatic conversion between 24 bit and Predefined color is not supported (yet?).

Member Function Documentation

◆ blue()

quint8 Tg::Color::blue ( ) const

Returns intensity of blue color.

Warning
Returns 0 when Color type is Predefined.

◆ code() [1/2]

QString Tg::Color::code ( const Color color,
const bool  isBackground,
const bool  forceTrueColor 
)
static

Returns ANSI-coded sequence, as understood by terminal, for given color.

Depending on isBackground, color will be encoded as background or foreground color (this is important only for Color::Predefined colors).

If forceTrueColor is true, returned sequence will use 24-bit color.

◆ code() [2/2]

QString Tg::Color::code ( const Color foreground,
const Color background = Color::Predefined::Empty 
)
static

Returns ANSI-coded sequence, as understood by terminal, for given foreground color and background color.

For best results, it is recommended for both colors to be of the same type (either Color::Predefined or 24-bit color).

◆ green()

quint8 Tg::Color::green ( ) const

Returns intensity of green color.

Warning
Returns 0 when Color type is Predefined.

◆ isEmpty()

bool Tg::Color::isEmpty ( ) const

Returns true if a Predefined color is Predefined::Empty.

In such case, the terminal will provide a default color.

◆ isPredefined()

bool Tg::Color::isPredefined ( ) const

Returns true is Color type is Predefined (as opposed to 24 bit color).

See also
isTrueColor

◆ isTrueColor()

bool Tg::Color::isTrueColor ( ) const

Returns true if Color is 24 bit (as opposed to Predefined).

See also
isPredefined

◆ red()

quint8 Tg::Color::red ( ) const

Returns intensity of red color.

Warning
Returns 0 when Color type is Predefined.

◆ rgb()

QString Tg::Color::rgb ( ) const

Returns 24 bit color information in a format understood by the terminal.

Resulting string looks like this:

123;123;123
Warning
Will return 0;0;0 when Color type is Predefined.

The documentation for this class was generated from the following files: