cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
Term::Color Class Reference

#include <cpp-terminal/color.hpp>

Public Types

enum class  Type : std::uint8_t {
  Unset , NoColor , Bit3 , Bit4 ,
  Bit8 , Bit24
}
 
enum class  Name : std::uint8_t {
  Black = 0 , Red = 1 , Green = 2 , Yellow = 3 ,
  Blue = 4 , Magenta = 5 , Cyan = 6 , White = 7 ,
  Default = 9 , Gray = 60 , BrightBlack = 60 , BrightRed = 61 ,
  BrightGreen = 62 , BrightYellow = 63 , BrightBlue = 64 , BrightMagenta = 65 ,
  BrightCyan = 66 , BrightWhite = 67
}
 The 3bit/4bit colors for the terminal. More...
 

Public Member Functions

bool operator== (const Color &) const
 
bool operator!= (const Color &) const
 
 Color ()
 
 Color (const Term::Color::Name &name)
 
 Color (const std::uint8_t &color)
 
 Color (const std::uint8_t &red, const std::uint8_t &green, const std::uint8_t &blue)
 
Type getType () const
 
Name to3bits () const
 
Name to4bits () const
 
std::uint8_t to8bits () const
 
std::array< std::uint8_t, 3 > to24bits () const
 

Private Attributes

Type m_Type {Type::Unset}
 
union { 
 
   std::uint8_t   m_bit8 
 
   std::array< std::uint8_t, 3 >   m_bit24 
 
};  
 

Detailed Description

Definition at line 21 of file color.hpp.

Member Enumeration Documentation

◆ Name

enum class Term::Color::Name : std::uint8_t
strong

The 3bit/4bit colors for the terminal.

Get the foreground color: Color4 + 30, Background color: Color4 + 40 See https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit.

Enumerator
Black 

Black FG: 30, BG: 40.

Red 

Red FG: 31, BG: 41.

Green 

Green FG: 32, BG: 42.

Yellow 

Yellow FG: 33, BG: 43.

Blue 

Blue FG: 34, BG: 44.

Magenta 

Magenta FG: 35, BG: 45.

Cyan 

Cyan FG: 36, BG: 46.

White 

White FG: 37, BG: 47.

Default 

Use the default terminal color, FG: 39, BG: 49.

Gray 

Gray FG: 90, BG: 100.

BrightBlack 

BrightBlack FG: 90, BG: 100.

BrightRed 

BrightRed FG: 91, BG: 101.

BrightGreen 

BrightGreen FG: 92, BG: 102.

BrightYellow 

BrightYellow FG: 93, BG: 103.

BrightBlue 

BrightBlue FG: 94, BG: 104.

BrightMagenta 

BrightMagenta FG: 95, BG: 105.

BrightCyan 

BrightCyan FG: 96, BG: 106.

BrightWhite 

BrightWhite FG: 97, BG: 107.

Definition at line 39 of file color.hpp.

40 {
41 Black = 0,
42 Red = 1,
43 Green = 2,
44 Yellow = 3,
45 Blue = 4,
46 Magenta = 5,
47 Cyan = 6,
48 White = 7,
49 Default = 9,
50 Gray = 60,
51 BrightBlack = 60,
52 BrightRed = 61,
53 BrightGreen = 62,
54 BrightYellow = 63,
55 BrightBlue = 64,
56 BrightMagenta = 65,
57 BrightCyan = 66,
58 BrightWhite = 67
59 };
@ Cyan
Cyan FG: 36, BG: 46.
@ White
White FG: 37, BG: 47.
@ BrightBlue
BrightBlue FG: 94, BG: 104.
@ BrightRed
BrightRed FG: 91, BG: 101.
@ BrightWhite
BrightWhite FG: 97, BG: 107.
@ BrightMagenta
BrightMagenta FG: 95, BG: 105.
@ Yellow
Yellow FG: 33, BG: 43.
@ Default
Use the default terminal color, FG: 39, BG: 49.
@ BrightYellow
BrightYellow FG: 93, BG: 103.
@ BrightBlack
BrightBlack FG: 90, BG: 100.
@ Blue
Blue FG: 34, BG: 44.
@ Gray
Gray FG: 90, BG: 100.
@ BrightGreen
BrightGreen FG: 92, BG: 102.
@ Magenta
Magenta FG: 35, BG: 45.
@ BrightCyan
BrightCyan FG: 96, BG: 106.
@ Green
Green FG: 32, BG: 42.
@ Black
Black FG: 30, BG: 40.
@ Red
Red FG: 31, BG: 41.

◆ Type

enum class Term::Color::Type : std::uint8_t
strong
Enumerator
Unset 
NoColor 
Bit3 
Bit4 
Bit8 
Bit24 

Definition at line 24 of file color.hpp.

Constructor & Destructor Documentation

◆ Color() [1/4]

Term::Color::Color ( )

Definition at line 23 of file color.cpp.

23: m_bit8(0) {}
std::uint8_t m_bit8
Definition color.hpp:76

◆ Color() [2/4]

Term::Color::Color ( const Term::Color::Name & name)

Definition at line 25 of file color.cpp.

25: m_Type(Type::Bit4), m_bit8(static_cast<std::uint8_t>(name)) {}
Type m_Type
Definition color.hpp:73

◆ Color() [3/4]

Term::Color::Color ( const std::uint8_t & color)

Definition at line 27 of file color.cpp.

27: m_Type(Type::Bit8), m_bit8(color) {}

◆ Color() [4/4]

Term::Color::Color ( const std::uint8_t & red,
const std::uint8_t & green,
const std::uint8_t & blue )

Definition at line 29 of file color.cpp.

30{
31 // Hack for gcc4.7
32 m_bit24[0] = r;
33 m_bit24[1] = b;
34 m_bit24[2] = g;
35}
std::array< std::uint8_t, 3 > m_bit24
Definition color.hpp:77

Member Function Documentation

◆ getType()

Term::Color::Type Term::Color::getType ( ) const

Definition at line 37 of file color.cpp.

37{ return m_Type; }

◆ operator!=()

bool Term::Color::operator!= ( const Color & color) const

Definition at line 21 of file color.cpp.

21{ return !(*this == color); }

◆ operator==()

bool Term::Color::operator== ( const Color & color) const

Definition at line 14 of file color.cpp.

15{
16 if(color.getType() != getType()) { return false; }
17 if(getType() == Term::Color::Type::Bit24) { return m_bit24 == color.to24bits(); }
18 return m_bit8 == color.to8bits();
19}
Type getType() const
Definition color.cpp:37

◆ to24bits()

std::array< std::uint8_t, 3 > Term::Color::to24bits ( ) const

Definition at line 77 of file color.cpp.

77{ return m_bit24; }

◆ to3bits()

Term::Color::Name Term::Color::to3bits ( ) const

Definition at line 39 of file color.cpp.

40{
41 if(getType() == Type::Bit3) { return static_cast<Term::Color::Name>(m_bit8); }
42 const Term::Color::Name ret{to4bits()};
43 if(ret >= Term::Color::Name::Gray) { return static_cast<Term::Color::Name>(static_cast<uint8_t>(ret) - static_cast<uint8_t>(Term::Color::Name::Gray)); }
44 return ret;
45}
Name to4bits() const
Definition color.cpp:48
Name
The 3bit/4bit colors for the terminal.
Definition color.hpp:40

◆ to4bits()

Term::Color::Name Term::Color::to4bits ( ) const

Definition at line 48 of file color.cpp.

49{
50 //https://ajalt.github.io/colormath/converter/
51 // clang-format off
52 static const constexpr std::array<std::uint8_t,256> table ={{
53 0, 1, 2, 3, 4, 5, 6, 7, 60, 61, 62, 63, 64, 65, 66, 67, 0, 4, 4, 4, 64, 64, 2, 6, 4, 4, 64, 64, 2, 2, 6, 4, 64, 64, 2, 2, 2, 6, 64, 64, 62, 62, 62, 62, 66, 64, 62, 62, 62, 62, 62, 66,
54 1, 5, 4, 4, 64, 64, 3, 60, 4, 4, 64, 64, 2, 2, 6, 4, 64, 64, 2, 2, 2, 6, 64, 64, 62, 62, 62, 62, 66, 64, 62, 62, 62, 62, 62, 66, 1, 1, 5, 4, 64, 64, 1, 1, 5, 4, 64, 64, 3, 3, 60, 4,
55 64, 64, 2, 2, 2, 6, 64, 64, 62, 62, 62, 62, 66, 64, 62, 62, 62, 62, 62, 66, 1, 1, 1, 5, 64, 64, 1, 1, 1, 5, 64, 64, 1, 1, 1, 5, 64, 64, 3, 3, 3, 7, 64, 64, 62, 62, 62, 62, 66, 64, 62, 62,
56 62, 62, 62, 66, 61, 61, 61, 61, 65, 64, 61, 61, 61, 61, 65, 64, 61, 61, 61, 61, 65, 64, 61, 61, 61, 61, 65, 64, 63, 63, 63, 63, 7, 64, 62, 62, 62, 62, 62, 66, 61, 61, 61, 61, 61, 65, 61, 61, 61, 61, 61, 65,
57 61, 61, 61, 61, 61, 65, 61, 61, 61, 61, 61, 65, 61, 61, 61, 61, 61, 65, 63, 63, 63, 63, 63, 67, 0, 0, 0, 0, 0, 0, 60, 60, 60, 60, 60, 60, 7, 7, 7, 7, 7, 7, 67, 67, 67, 67, 67, 67}};
58 // clang-format on
59 if((getType() == Term::Color::Type::Bit24) || (getType() == Term::Color::Type::Bit8)) { return static_cast<Term::Color::Name>(table[to8bits()]); }
60 return static_cast<Term::Color::Name>(m_bit8);
61}
std::uint8_t to8bits() const
Definition color.cpp:64

◆ to8bits()

std::uint8_t Term::Color::to8bits ( ) const

Definition at line 64 of file color.cpp.

65{
67 {
68 // check gray scale in 24 steps
69 if(m_bit24[0] == m_bit24[1] && m_bit24[0] == m_bit24[2]) { return static_cast<std::uint8_t>(232 + m_bit24[0] / 32 + m_bit24[1] / 32 + m_bit24[2] / 32); }
70 // normal color space
71 return static_cast<std::uint8_t>(16 + 36 * (m_bit24[0] / 51) + 6 * (m_bit24[1] / 51) + (m_bit24[2] / 51));
72 }
73 return m_bit8;
74}

Member Data Documentation

◆ [union]

union { ... } Term::Color

◆ m_bit24

std::array<std::uint8_t, 3> Term::Color::m_bit24

Definition at line 77 of file color.hpp.

◆ m_bit8

std::uint8_t Term::Color::m_bit8

Definition at line 76 of file color.hpp.

◆ m_Type

Type Term::Color::m_Type {Type::Unset}
private

Definition at line 73 of file color.hpp.


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