ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ColorScheme.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ColorScheme.h"
9 
10 #include <utility>
11 
12 static QTextCharFormat FormatHelper(const QColor &color,
13  const QFont::Style style = QFont::Style::StyleNormal,
14  const QFont::Weight weight = QFont::Weight::Normal)
15 {
16  QTextCharFormat charFormat;
17 
18  charFormat.setForeground(color);
19  charFormat.setFontWeight(weight);
20  if (style == QFont::Style::StyleItalic)
21  {
22  charFormat.setFontItalic(true);
23  }
24 
25  return charFormat;
26 }
27 
28 static QTextCharFormat FormatHelper(const QColor &color, QFont::Weight weight)
29 {
30  return FormatHelper(color, QFont::Style::StyleNormal, weight);
31 }
32 
33 ColorScheme::ColorScheme(QString name,
34  QVector<QTextCharFormat> &&formats,
35  const QTextFormat &defaultFormat,
36  QColor backgroundColor,
37  QColor currentLineHighlightColor)
38  : m_name(std::move(name)),
39  m_formats(formats),
40  m_defaultFormat(defaultFormat),
41  m_backgroundColor(std::move(backgroundColor)),
42  m_currentLineHighlightColor(std::move(currentLineHighlightColor))
43 {
44  Q_ASSERT(formats.size() == static_cast<int>(PythonHighlighter::CodeElement::End));
45 }
46 
48 {
49  return m_defaultFormat.foreground().color();
50 }
51 
53 {
54  return m_backgroundColor;
55 }
56 
58 {
59  return m_currentLineHighlightColor;
60 }
61 
63 {
64  const auto index = static_cast<int>(e);
65  Q_ASSERT(index < m_formats.size());
66  return m_formats[index];
67 }
68 
70 {
71  QVector<QTextCharFormat> formats{FormatHelper(Qt::blue),
73  FormatHelper(Qt::darkGray),
74  FormatHelper(Qt::black, QFont::Bold),
76  FormatHelper(Qt::darkMagenta, QFont::StyleItalic),
77  FormatHelper(Qt::darkGreen, QFont::StyleItalic),
78  FormatHelper(Qt::black, QFont::StyleItalic),
79  FormatHelper("brown")};
80 
81  return {"Default",
82  std::move(formats),
84  Qt::white,
85  QColor(Qt::yellow).lighter(160)};
86 };
87 
89 {
90  QVector<QTextCharFormat> formats{FormatHelper(QColor(255, 121, 198)),
92  FormatHelper(Qt::darkGray),
93  FormatHelper(QColor(80, 250, 123), QFont::Bold),
94  FormatHelper(QColor(241, 250, 140)),
95  FormatHelper(Qt::darkMagenta, QFont::StyleItalic),
96  FormatHelper(QColor(98, 114, 164), QFont::StyleItalic),
97  FormatHelper(QColor(255, 184, 108), QFont::StyleItalic),
98  FormatHelper("#bd93f9")};
99 
100  return {"Dracula",
101  std::move(formats),
102  FormatHelper(QColor(248, 248, 242)),
103  QColor(40, 42, 54),
104  QColor(68, 71, 90)};
105 }
106 
107 const QString &ColorScheme::name() const
108 {
109  return m_name;
110 };
112 
114 {
115  if (s_AvailableColorSchemes.empty())
116  {
119  }
121 }
122 
124 {
125  const auto it =
126  std::find_if(s_AvailableColorSchemes.begin(),
128  [name](const ColorScheme &colorScheme) { return colorScheme.name() == name; });
129 
130  if (it != s_AvailableColorSchemes.end())
131  {
132  return &*it;
133  }
134  return nullptr;
135 }
static ColorScheme::Vector s_AvailableColorSchemes
static QTextCharFormat FormatHelper(const QColor &color, const QFont::Style style=QFont::Style::StyleNormal, const QFont::Weight weight=QFont::Weight::Normal)
Definition: ColorScheme.cpp:12
std::string name
math::float4 color
ColorScheme to be used by the PythonHighlighter & Editor.
Definition: ColorScheme.h:14
static ColorScheme Dracula()
Definition: ColorScheme.cpp:88
QColor foregroundColor() const
Color to be used for regular text.
Definition: ColorScheme.cpp:47
const QString & name() const
The name of the color scheme.
QColor currentLineHighlightColor() const
Color to be used if a highlight of the current line is shown.
Definition: ColorScheme.cpp:57
static const Vector & AvailableColorSchemes()
Returns the vector of available color schemes.
const QTextCharFormat & operator[](PythonHighlighter::CodeElement e) const
Returns the format to be used for the given code element.
Definition: ColorScheme.cpp:62
static ColorScheme Default()
Default color scheme.
Definition: ColorScheme.cpp:69
std::vector< ColorScheme > Vector
Definition: ColorScheme.h:16
static const ColorScheme * ColorSchemeByName(const QString &name)
QColor backgroundColor() const
Color to be used as the background for the displayed text.
Definition: ColorScheme.cpp:52
constexpr Rgb black(0, 0, 0)
constexpr Rgb magenta(MAX, 0, MAX)
constexpr Rgb white(MAX, MAX, MAX)
constexpr Rgb red(MAX, 0, 0)
constexpr Rgb blue(0, 0, MAX)
constexpr Rgb yellow(MAX, MAX, 0)
Definition: Eigen.h:85