NetSurf
nscolour.c
Go to the documentation of this file.
1/*
2 * Copyright 2020 Michael Drake <tlsa@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * NetSurf UI colours (implementation).
22 *
23 * Builds common colours used throughout NetSurf's interface.
24 */
25
26#include <stdio.h>
27#include <assert.h>
28#include <stddef.h>
29#include <stdbool.h>
30
31#include "netsurf/inttypes.h"
32#include "netsurf/plot_style.h"
33
34#include "utils/errors.h"
35#include "utils/nscolour.h"
37
39
40/**
41 * Set some colours up from a couple of system colour entries.
42 *
43 * \param[in] name_bg Name of choices string for background colour lookup.
44 * \param[in] name_fg Name of choices string for foreground colour lookup.
45 * \param[in] bg_num Numerator for background adjustment ratio.
46 * \param[in] bg_den Denominator for backfground adjustment ratio.
47 * \param[out] bg Returns the background colour.
48 * \param[out] bg_hover Returns the hovered background colour.
49 * \param[out] fg Returns the foreground colour.
50 * \param[out] fg_subtle Returns the subtle foreground colour.
51 * \param[out] fg_faded Returns the faded foreground colour.
52 * \param[out] fg_good Returns the good foreground colour.
53 * \param[out] fg_bad Returns the bad foreground colour.
54 * \param[out] border Returns the border colour.
55 */
57 const char *name_bg,
58 const char *name_fg,
59 unsigned bg_num,
60 unsigned bg_den,
61 colour *bg,
62 colour *bg_hover,
63 colour *fg,
64 colour *fg_subtle,
65 colour *fg_faded,
66 colour *fg_good,
67 colour *fg_bad,
69{
70 nserror res;
71 bool dark_mode;
72 colour bg_sys;
73
74 assert(name_bg != NULL);
75 assert(name_fg != NULL);
76 assert(bg != NULL);
77 assert(fg != NULL);
78
79 /* user configured background colour */
80 res = ns_system_colour_char(name_bg, &bg_sys);
81 if (res != NSERROR_OK) {
82 return res;
83 }
84
85 /* user configured foreground colour */
86 res = ns_system_colour_char(name_fg, fg);
87 if (res != NSERROR_OK) {
88 return res;
89 }
90
91 /* if there is a valid background fraction apply it */
92 if (bg_num < bg_den) {
93 *bg = mix_colour(bg_sys, *fg, 255 * bg_num / bg_den);
94 } else {
95 *bg = bg_sys;
96 }
97
98 dark_mode = colour_lightness(*fg) > colour_lightness(*bg);
99
100 if (bg_hover != NULL) {
101 *bg_hover = dark_mode ?
104 }
105
106 if (fg_subtle != NULL) {
107 *fg_subtle = mix_colour(*fg, *bg, 255 * 25 / 32);
108 }
109
110 if (fg_faded != NULL) {
111 *fg_faded = mix_colour(*fg, *bg, 255 * 20 / 32);
112 }
113
114 if (fg_good != NULL) {
115 *fg_good = colour_engorge_component(*fg, !dark_mode,
117 }
118
119 if (fg_bad != NULL) {
120 *fg_bad = colour_engorge_component(*fg, !dark_mode,
122 }
123
124 if (border != NULL) {
125 *border = mix_colour(*fg, bg_sys, 255 * 8 / 32);
126 }
127
128 return NSERROR_OK;
129}
130
131/* Exported interface, documented in utils/nscolour.h */
133{
134 nserror res;
135
136 res = nscolour__get("Window", "WindowText", 16, 16,
145 if (res != NSERROR_OK) {
146 return res;
147 }
148
149 res = nscolour__get("Window", "WindowText", 15, 16,
158 if (res != NSERROR_OK) {
159 return res;
160 }
161
162 res = nscolour__get("Highlight", "HighlightText", 16, 16,
164 NULL,
167 NULL,
168 NULL,
169 NULL,
170 NULL);
171 if (res != NSERROR_OK) {
172 return res;
173 }
174
175 res = ns_system_colour_char("Scrollbar",
177 if (res != NSERROR_OK) {
178 return res;
179 }
180
181 res = nscolour__get("ButtonFace", "ButtonText", 16, 16,
183 NULL,
185 NULL,
186 NULL,
187 NULL,
188 NULL,
189 NULL);
190 if (res != NSERROR_OK) {
191 return res;
192 }
193
201
202 return NSERROR_OK;
203}
204
205/* Exported interface, documented in utils/nscolour.h */
206nserror nscolour_get_stylesheet(const char **stylesheet_out)
207{
208 static char buffer[640];
209 int ret;
210
211 assert(stylesheet_out != NULL);
212
213 ret = snprintf(buffer, sizeof(buffer),
214 ".ns-odd-bg {\n"
215 "\tbackground-color: #%06"PRIx32";\n"
216 "}\n"
217 ".ns-odd-bg-hover {\n"
218 "\tbackground-color: #%06"PRIx32";\n"
219 "}\n"
220 ".ns-odd-fg {\n"
221 "\tcolor: #%06"PRIx32";\n"
222 "}\n"
223 ".ns-odd-fg-subtle {\n"
224 "\tcolor: #%06"PRIx32";\n"
225 "}\n"
226 ".ns-odd-fg-faded {\n"
227 "\tcolor: #%06"PRIx32";\n"
228 "}\n"
229 ".ns-odd-fg-good {\n"
230 "\tcolor: #%06"PRIx32";\n"
231 "}\n"
232 ".ns-odd-fg-bad {\n"
233 "\tcolor: #%06"PRIx32";\n"
234 "}\n"
235 ".ns-even-bg {\n"
236 "\tbackground-color: #%06"PRIx32";\n"
237 "}\n"
238 ".ns-even-bg-hover {\n"
239 "\tbackground-color: #%06"PRIx32";\n"
240 "}\n"
241 ".ns-even-fg {\n"
242 "\tcolor: #%06"PRIx32";\n"
243 "}\n"
244 ".ns-even-fg-subtle {\n"
245 "\tcolor: #%06"PRIx32";\n"
246 "}\n"
247 ".ns-even-fg-faded {\n"
248 "\tcolor: #%06"PRIx32";\n"
249 "}\n"
250 ".ns-even-fg-good {\n"
251 "\tcolor: #%06"PRIx32";\n"
252 "}\n"
253 ".ns-even-fg-bad {\n"
254 "\tcolor: #%06"PRIx32";\n"
255 "}\n"
256 ".ns-border {\n"
257 "\tborder-color: #%06"PRIx32";\n"
258 "}\n",
274 assert(ret > 0 && (size_t)ret < sizeof(buffer));
275 if (ret < 0 || (size_t)ret >= sizeof(buffer)) {
276 /* Error or buffer too small */
277 return NSERROR_NOSPACE;
278 }
279
280 *stylesheet_out = buffer;
281 return NSERROR_OK;
282}
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOSPACE
Insufficient space.
Definition: errors.h:59
@ NSERROR_OK
No error.
Definition: errors.h:30
Netsurf additional integer type formatting macros.
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)
Set some colours up from a couple of system colour entries.
Definition: nscolour.c:56
nserror nscolour_update(void)
Update the nscolour table from the current nsoptions.
Definition: nscolour.c:132
colour nscolours[NSCOLOUR__COUNT]
NetSurf UI colour table.
Definition: nscolour.c:38
nserror nscolour_get_stylesheet(const char **stylesheet_out)
Get a pointer to a stylesheet for nscolours.
Definition: nscolour.c:206
NetSurf UI colours (interface).
@ NSCOLOUR_BUTTON_BG
Definition: nscolour.h:57
@ NSCOLOUR_WIN_EVEN_FG
Definition: nscolour.h:44
@ NSCOLOUR_TEXT_INPUT_FG
Definition: nscolour.h:51
@ NSCOLOUR_TEXT_INPUT_FG_SUBTLE
Definition: nscolour.h:52
@ NSCOLOUR_WIN_ODD_FG_FADED
Definition: nscolour.h:38
@ NSCOLOUR_WIN_ODD_BG
Definition: nscolour.h:34
@ NSCOLOUR_WIN_ODD_BG_HOVER
Definition: nscolour.h:35
@ NSCOLOUR_WIN_ODD_FG_GOOD
Definition: nscolour.h:39
@ NSCOLOUR_WIN_EVEN_BORDER
Definition: nscolour.h:49
@ NSCOLOUR_SEL_FG
Definition: nscolour.h:54
@ NSCOLOUR_WIN_EVEN_BG_HOVER
Definition: nscolour.h:43
@ NSCOLOUR_BUTTON_FG
Definition: nscolour.h:58
@ NSCOLOUR_WIN_EVEN_FG_SUBTLE
Definition: nscolour.h:45
@ NSCOLOUR_SEL_BG
Definition: nscolour.h:53
@ NSCOLOUR_WIN_ODD_BORDER
Definition: nscolour.h:41
@ NSCOLOUR_WIN_ODD_FG_SUBTLE
Definition: nscolour.h:37
@ NSCOLOUR__COUNT
Definition: nscolour.h:59
@ NSCOLOUR_SCROLL_WELL
Definition: nscolour.h:56
@ NSCOLOUR_TEXT_INPUT_BG
Definition: nscolour.h:50
@ NSCOLOUR_WIN_EVEN_FG_BAD
Definition: nscolour.h:48
@ NSCOLOUR_WIN_EVEN_BG
Definition: nscolour.h:42
@ NSCOLOUR_WIN_ODD_FG_BAD
Definition: nscolour.h:40
@ NSCOLOUR_WIN_EVEN_FG_FADED
Definition: nscolour.h:46
@ NSCOLOUR_WIN_EVEN_FG_GOOD
Definition: nscolour.h:47
@ NSCOLOUR_WIN_ODD_FG
Definition: nscolour.h:36
@ NSCOLOUR_SEL_FG_SUBTLE
Definition: nscolour.h:55
plotter style interfaces, generic styles and style colour helpers.
#define half_darken_colour(c1)
Definition: plot_style.h:128
#define colour_rb_swap(c)
Definition: plot_style.h:217
#define blend_colour(c0, c1)
Definition: plot_style.h:163
@ PLOT_COLOUR_COMPONENT_RED
Definition: plot_style.h:224
@ PLOT_COLOUR_COMPONENT_GREEN
Definition: plot_style.h:225
#define colour_lightness(c0)
Obtain the luminance of a colour according to ITU BT.601.
Definition: plot_style.h:181
#define mix_colour(c0, c1, p)
Definition: plot_style.h:198
#define colour_to_bw_nearest(c0)
Definition: plot_style.h:188
static colour colour_engorge_component(colour col, bool dark, enum plot_colour_component comp)
Engorge a particular colour channel.
Definition: plot_style.h:237
#define half_lighten_colour(c1)
Definition: plot_style.h:144
Container for border values during table border calculations.
Definition: table.c:42
nserror ns_system_colour_char(const char *name, colour *colour_out)
Obtain a system colour from a name.
Definition: system_colour.c:78
Interface to system colour values.
uint32_t colour
Colour type: XBGR.
Definition: types.h:35