-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-colourmap.c
142 lines (129 loc) · 3.47 KB
/
lib-colourmap.c
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
/*
* Copyright Neil Brown ©2019-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Provide a colour-name service.
* A client (normally a display) call "colour:map" passing a
* string name of a colour. The callback is passed the chosen colour
* as RGB values (0-1000) in num1 num2 and x, and as string in hex format #RRggBB
* Alternate interfaces are "colour:map:bg" and "colour:map:fg"
* The are passes a reference colour in 'str2' (#rrggbb).
* for colour:map:bg, the reference should be the default background
* for colour:map:fg, the reference should be the chosen background.
* One day these might modify the reult to provide better contrast.
*
* Colours have a base name and modifiers.
* The base name is either a word (e.g. "green" "rebeccapurple") or a
* hex colour #rrggbb. In either case, case is ignored.
* The modifier can adjust value or saturation.
* -nn (0-99) reduces the brightness - scales each channel towards zero.
* 0 means black. 99 means no-change
* +nn (0-99) reduces saturation - scales each channel towards max.
* 0 means no change, 99 means white.
* So "white-50" and "black+50" are both mid-grey. "yellow-90+90" is a pale yellow.
*/
#define _GNU_SOURCE for_asprintf
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "core.h"
static struct colours {
char *name;
int r,g,b;
} colours[] = {
{ "black", 0, 0, 0 },
{ "white", 1000, 1000, 1000 },
{ "red", 1000, 0, 0 },
{ "green", 0, 1000, 0 },
{ "blue", 0, 0, 1000 },
{ "yellow", 1000, 1000, 0 },
{ "magenta", 1000, 0, 1000 },
{ "cyan", 0, 1000, 1000 },
{ "darkblue", 0, 0, 550 },
{ "purple", 500, 0, 500 },
{ "grey", 500, 500, 500 },
{ "pink", 1000, 800, 800 },
};
static void gethex(char *col safe, int rgb[])
{
char b[3];
int i;
b[2] = 0;
if (strlen(col) != 6)
return;
for (i = 0; i < 3; i++) {
b[0] = col[i*2];
b[1] = col[i+2+1];
rgb[i] = strtoul(b, NULL, 16) * 1000 / 255;
}
}
static bool find_colour(char *col, int rgb[])
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(colours); i++) {
if (strcasecmp(col, colours[i].name) == 0) {
rgb[0] = colours[i].r;
rgb[1] = colours[i].g;
rgb[2] = colours[i].b;
return True;
}
}
return False;
}
static void add_value(char *n, int rgb[])
{
int scale = atoi(n);
int i;
if (scale < 0 || scale > 99)
return;
for (i = 0; i < 3; i++)
rgb[i] = rgb[i] * scale / 99;
}
static void add_sat(char *n, int rgb[])
{
int scale = atoi(n);
int i;
if (scale < 0 || scale > 99)
return;
for (i = 0; i < 3; i++)
rgb[i] = 1000 - ( (99 - scale) * (1000 - rgb[i]) / 99);
}
DEF_CMD(colour_map)
{
char *col;
char *m, *p;
int rgb[3] = { 500, 500, 500};
int ret;
if (!ci->str)
return Enoarg;
col = strdup(ci->str);
p = strchr(col, '+');
if (p)
*p++ = 0;
m = strchr(col, '-');
if (m)
*m++ = 0;
if (col[0] == '#')
gethex(col+1, rgb);
else if (!find_colour(col, rgb))
find_colour("grey", rgb);
if (m)
add_value(m, rgb);
if (p)
add_sat(p, rgb);
free(col);
asprintf(&col, "#%02x%02x%02x", rgb[0]*255/1000,
rgb[1]*255/1000, rgb[2]*255/1000);
ret = comm_call(ci->comm2, "colour:callback", ci->focus,
rgb[0], NULL, col,
rgb[1], NULL, NULL, rgb[2], 0);
free(col);
return ret;
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &colour_map,
0, NULL, "colour:map");
call_comm("global-set-command-prefix", ed, &colour_map,
0, NULL, "colour:map:");
}