summaryrefslogtreecommitdiff
path: root/utils/nscolour.c
blob: 381da1167225bd823294762e9d23a977b985950b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright 2020 Michael Drake <tlsa@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * NetSurf UI colours (implementation).
 *
 * Builds common colours used throughout NetSurf's interface.
 */

#include <stdio.h>
#include <assert.h>
#include <stddef.h>
#include <stdbool.h>

#include "netsurf/plot_style.h"

#include "utils/errors.h"
#include "utils/nscolour.h"
#include "desktop/system_colour.h"

colour nscolours[NSCOLOUR__COUNT];

/**
 * Set some colours up from a couple of system colour entries.
 *
 * \param[in]  name_bg    Name of choices string for background colour lookup.
 * \param[in]  name_fg    Name of choices string for foreground colour lookup.
 * \param[in]  bg_num     Numerator for background adjustment ratio.
 * \param[in]  bg_den     Denominator for backfground adjustment ratio.
 * \param[out] bg         Returns the background colour.
 * \param[out] bg_hover   Returns the hovered background colour.
 * \param[out] fg         Returns the foreground colour.
 * \param[out] fg_subtle  Returns the subtle foreground colour.
 * \param[out] fg_faded   Returns the faded foreground colour.
 * \param[out] fg_good    Returns the good foreground colour.
 * \param[out] fg_bad     Returns the bad foreground colour.
 * \param[out] border     Returns the border colour.
 */
static nserror nscolour__get(
		const char *name_bg,
		const char *name_fg,
		unsigned bg_num,
		unsigned bg_den,
		colour *bg,
		colour *bg_hover,
		colour *fg,
		colour *fg_subtle,
		colour *fg_faded,
		colour *fg_good,
		colour *fg_bad,
		colour *border)
{
	nserror res;
	bool dark_mode;
	colour bg_temp;

	assert(name_bg != NULL);
	assert(name_fg != NULL);
	assert(bg != NULL);
	assert(fg != NULL);

	res = ns_system_colour_char(name_bg, &bg_temp);
	if (res != NSERROR_OK) {
		return res;
	}

	res = ns_system_colour_char(name_fg, fg);
	if (res != NSERROR_OK) {
		return res;
	}

	*bg = mix_colour(bg_temp, *fg, 255 * bg_num / bg_den);

	dark_mode = colour_lightness(*fg) > colour_lightness(*bg);

	if (bg_hover != NULL) {
		*bg_hover = dark_mode ?
				half_lighten_colour(*bg) :
				half_darken_colour(*bg);
	}

	if (fg_subtle != NULL) {
		*fg_subtle = mix_colour(*fg, *bg, 255 * 25 / 32);
	}

	if (fg_faded != NULL) {
		*fg_faded = mix_colour(*fg, *bg, 255 * 20 / 32);
	}

	if (fg_good != NULL) {
		*fg_good = colour_engorge_component(*fg, !dark_mode,
				PLOT_COLOUR_COMPONENT_GREEN);
	}

	if (fg_bad != NULL) {
		*fg_bad = colour_engorge_component(*fg, !dark_mode,
				PLOT_COLOUR_COMPONENT_RED);
	}

	if (border != NULL) {
		*border = mix_colour(*fg, bg_temp, 255 * 8 / 32);
	}

	return NSERROR_OK;
}

/* Exported interface, documented in utils/nscolour.h */
nserror nscolour_update(void)
{
	nserror res;

	res = nscolour__get("Window", "WindowText", 16, 16,
			&nscolours[NSCOLOUR_WIN_EVEN_BG],
			&nscolours[NSCOLOUR_WIN_EVEN_BG_HOVER],
			&nscolours[NSCOLOUR_WIN_EVEN_FG],
			&nscolours[NSCOLOUR_WIN_EVEN_FG_SUBTLE],
			&nscolours[NSCOLOUR_WIN_EVEN_FG_FADED],
			&nscolours[NSCOLOUR_WIN_EVEN_FG_GOOD],
			&nscolours[NSCOLOUR_WIN_EVEN_FG_BAD],
			&nscolours[NSCOLOUR_WIN_EVEN_BORDER]);
	if (res != NSERROR_OK) {
		return res;
	}

	res = nscolour__get("Window", "WindowText", 15, 16,
			&nscolours[NSCOLOUR_WIN_ODD_BG],
			&nscolours[NSCOLOUR_WIN_ODD_BG_HOVER],
			&nscolours[NSCOLOUR_WIN_ODD_FG],
			&nscolours[NSCOLOUR_WIN_ODD_FG_SUBTLE],
			&nscolours[NSCOLOUR_WIN_ODD_FG_FADED],
			&nscolours[NSCOLOUR_WIN_ODD_FG_GOOD],
			&nscolours[NSCOLOUR_WIN_ODD_FG_BAD],
			&nscolours[NSCOLOUR_WIN_ODD_BORDER]);
	if (res != NSERROR_OK) {
		return res;
	}

	res = nscolour__get("Highlight", "HighlightText", 16, 16,
			&nscolours[NSCOLOUR_SEL_BG],
			NULL,
			&nscolours[NSCOLOUR_SEL_FG],
			&nscolours[NSCOLOUR_SEL_FG_SUBTLE],
			NULL,
			NULL,
			NULL,
			NULL);
	if (res != NSERROR_OK) {
		return res;
	}

	res = ns_system_colour_char("Scrollbar",
			&nscolours[NSCOLOUR_SCROLL_WELL]);
	if (res != NSERROR_OK) {
		return res;
	}

	res = nscolour__get("ButtonFace", "ButtonText", 16, 16,
			&nscolours[NSCOLOUR_BUTTON_BG],
			NULL,
			&nscolours[NSCOLOUR_BUTTON_FG],
			NULL,
			NULL,
			NULL,
			NULL,
			NULL);
	if (res != NSERROR_OK) {
		return res;
	}

	return NSERROR_OK;
}