WSL/SLF GitLab Repository

colors.cc 4.43 KB
Newer Older
1
2
3
4
5
/*****************************************************************************/
/*  Copyright 2019 WSL Institute for Snow and Avalanche Research  SLF-DAVOS  */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
8
9
10
11
12
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   INIshell is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14

15
16
   You should have received a copy of the GNU General Public License
   along with INIshell.  If not, see <http://www.gnu.org/licenses/>.
17
18
*/

Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
19
#include "colors.h"
20
21
22
#include "src/main/os.h"
#include "src/main/settings.h"

23
#include <QDebug>
Mathias Bavay's avatar
Mathias Bavay committed
24
#include <QPalette>
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
25
26
27

namespace colors {

28
29
30
31
32
33
/**
 * @brief Decide whether to use dark mode dependent on user preference and system settings.
 * @return True if dark mode should be enabled.
 */
bool useDarkTheme()
{
34
	bool use_dark = false; //OFF if not set
35
	const QString darkmode_setting( getSetting("user::appearance::darkmode", "value") );
36
	if (darkmode_setting == "AUTO")
37
		use_dark = os::isDarkTheme();
38
39
40
41
42
	else if (darkmode_setting == "ON")
		use_dark = true;
	return use_dark;
}

43
44
45
46
47
/**
 * @brief Get color for a specific event or item.
 * @param[in] INIshell's name for the color.
 * @return Qt usable color.
 */
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
48
49
50
QColor getQColor(const QString &colorname)
{
	QString name(colorname.toLower());
51
	const bool use_darkmode = useDarkTheme(); //decide whether to use dark mode
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
52

53
	/* substitutions */
54
	if (name == "app_bg")
Mathias Bavay's avatar
Mathias Bavay committed
55
		return (use_darkmode)? QColor(0x31363b) : QColor("white");
56
	else if (name == "normal")
57
		return (use_darkmode)? QColor("white") : QColor();
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
58
	else if (name == "info")
Mathias Bavay's avatar
Mathias Bavay committed
59
		name = "sl_base01";
60
61
	else if (name == "error")
		name = "sl_red";
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
62
	else if (name == "warning")
63
64
65
66
67
		name = "sl_orange";
	else if (name == "special")
		name = "sl_blue";
	else if (name == "important")
		name = "sl_red";
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
68
	else if (name == "helptext")
69
		name = "sl_base1";
70
	else if (name == "mandatory")
71
		name = "sl_red";
72
	else if (name == "default_values")
73
		name = "sl_base00";
Michael Reisecker's avatar
Michael Reisecker committed
74
	else if (name == "invalid_values")
75
		name = "sl_yellow";
76
77
78
79
80
81
	else if (name == "number")
		name = "sl_cyan";
	else if (name == "groupborder")
		name = "sl_base1";
	else if (name == "frameborder")
		name = "sl_base1";
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
82

83
84
	/* syntax highlighter */
	if (name == "syntax_known_key")
85
		name = "sl_blue";
86
	else if (name == "syntax_unknown_key")
87
		name = "sl_yellow";
88
	else if (name == "syntax_known_section")
Mathias Bavay's avatar
Mathias Bavay committed
89
		name = (use_darkmode)? "sl_base2" : "sl_base02";
90
	else if (name == "syntax_unknown_section")
91
		name = "sl_orange";
92
	else if (name == "syntax_value")
93
		name = "sl_green";
Mathias Bavay's avatar
Mathias Bavay committed
94
95
	else if (name == "coordinate")
		name = "sl_cyan";
Mathias Bavay's avatar
Mathias Bavay committed
96
97
	else if (name == "syntax_background") {
		if (use_darkmode) return QColor(0x41464b);
98
		name = "sl_base3";
Mathias Bavay's avatar
Mathias Bavay committed
99
	} else if (name == "syntax_invalid")
100
		name = "sl_red";
101
	else if (name == "syntax_comment")
102
		name = "sl_base1";
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	/* solarized named colors */
	if (name == "sl_base03")
		return QColor(0x002b36);
	else if (name == "sl_base02")
		return QColor(0x073642);
	else if (name == "sl_base01")
		return QColor(0x586e75);
	else if (name == "sl_base00")
		return QColor(0x657b83);
	else if (name == "sl_base0")
		return QColor(0x839496);
	else if (name == "sl_base1")
		return QColor(0x93a1a1);
	else if (name == "sl_base2")
		return QColor(0xeee8d5);
	else if (name == "sl_base3")
		return QColor(0xfdf6e3);
	else if (name == "sl_yellow")
		return QColor(0xb58900);
	else if (name == "sl_orange")
		return QColor(0xcb4b16);
	else if (name == "sl_red")
		return QColor(0xdc322f);
	else if (name == "sl_magenta")
		return QColor(0xd33682);
	else if (name == "sl_violet")
		return QColor(0x6c71c4);
	else if (name == "sl_blue")
		return QColor(0x268bd2);
	else if (name == "sl_cyan")
		return QColor(0x2aa198);
	else if (name == "sl_green")
		return QColor(0x859900);
137

138
#ifdef DEBUG //Qt will also correctly choose for strings like "red" but usually we want to be specific
Michael Reisecker's avatar
Michael Reisecker committed
139
140
141
142
143
	if (!name.isEmpty() && name.at(0) != "#") {
		const QColor testcol(name);
		if (!testcol.isValid()) //Qt could not parse the string
			qDebug() << "Custom color not found:" << name;
	}
144
145
#endif

146
	return {name}; //let Qt pick (e. g. hex codes), black if not valid
Michael Reisecker's avatar
Cleanup  
Michael Reisecker committed
147
148
149
}

} //namespace colors