xref: /openbsd/usr.bin/tmux/colour.c (revision 404b540a)
1 /* $OpenBSD: colour.c,v 1.2 2009/09/10 17:16:24 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Colour to string conversion functions. Bit 8 of the colour means it is one
28  * of the 256 colour palette.
29  */
30 
31 void
32 colour_set_fg(struct grid_cell *gc, int c)
33 {
34 	if (c & 0x100)
35 		gc->flags |= GRID_FLAG_FG256;
36 	gc->fg = c;
37 }
38 
39 void
40 colour_set_bg(struct grid_cell *gc, int c)
41 {
42 	if (c & 0x100)
43 		gc->flags |= GRID_FLAG_BG256;
44 	gc->bg = c;
45 }
46 
47 const char *
48 colour_tostring(int c)
49 {
50 	static char	s[32];
51 
52 	if (c & 0x100) {
53 		xsnprintf(s, sizeof s, "colour%u", c & ~0x100);
54 		return (s);
55 	}
56 
57 	switch (c) {
58 	case 0:
59 		return ("black");
60 	case 1:
61 		return ("red");
62 	case 2:
63 		return ("green");
64 	case 3:
65 		return ("yellow");
66 	case 4:
67 		return ("blue");
68 	case 5:
69 		return ("magenta");
70 	case 6:
71 		return ("cyan");
72 	case 7:
73 		return ("white");
74 	case 8:
75 		return ("default");
76 	}
77 	return (NULL);
78 }
79 
80 int
81 colour_fromstring(const char *s)
82 {
83 	const char	*errstr;
84 	int		 n;
85 
86 	if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) {
87 		n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr);
88 		if (errstr != NULL)
89 			return (-1);
90 		return (n | 0x100);
91 	}
92 
93 	if (strcasecmp(s, "black") == 0 || (s[0] == '0' && s[1] == '\0'))
94 		return (0);
95 	if (strcasecmp(s, "red") == 0 || (s[0] == '1' && s[1] == '\0'))
96 		return (1);
97 	if (strcasecmp(s, "green") == 0 || (s[0] == '2' && s[1] == '\0'))
98 		return (2);
99 	if (strcasecmp(s, "yellow") == 0 || (s[0] == '3' && s[1] == '\0'))
100 		return (3);
101 	if (strcasecmp(s, "blue") == 0 || (s[0] == '4' && s[1] == '\0'))
102 		return (4);
103 	if (strcasecmp(s, "magenta") == 0 || (s[0] == '5' && s[1] == '\0'))
104 		return (5);
105 	if (strcasecmp(s, "cyan") == 0 || (s[0] == '6' && s[1] == '\0'))
106 		return (6);
107 	if (strcasecmp(s, "white") == 0 || (s[0] == '7' && s[1] == '\0'))
108 		return (7);
109 	if (strcasecmp(s, "default") == 0 || (s[0] == '8' && s[1] == '\0'))
110 		return (8);
111 	return (-1);
112 }
113 
114 u_char
115 colour_256to16(u_char c)
116 {
117 	static const u_char table[256] = {
118 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
119 		 0,  4,  4,  4, 12, 12,  2,  6,  4,  4, 12, 12,  2,  2,  6,  4,
120 		12, 12,  2,  2,  2,  6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10,
121 		10, 10, 10, 14,  1,  5,  4,  4, 12, 12,  3,  8,  4,  4, 12, 12,
122 		 2,  2,  6,  4, 12, 12,  2,  2,  2,  6, 12, 12, 10, 10, 10, 10,
123 		14, 12, 10, 10, 10, 10, 10, 14,  1,  1,  5,  4, 12, 12,  1,  1,
124 		 5,  4, 12, 12,  3,  3,  8,  4, 12, 12,  2,  2,  2,  6, 12, 12,
125 		10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,  1,  1,  1,  5,
126 		12, 12,  1,  1,  1,  5, 12, 12,  1,  1,  1,  5, 12, 12,  3,  3,
127 		 3,  7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,
128 		 9,  9,  9,  9, 13, 12,  9,  9,  9,  9, 13, 12,  9,  9,  9,  9,
129 		13, 12,  9,  9,  9,  9, 13, 12, 11, 11, 11, 11,  7, 12, 10, 10,
130 		10, 10, 10, 14,  9,  9,  9,  9,  9, 13,  9,  9,  9,  9,  9, 13,
131 		 9,  9,  9,  9,  9, 13,  9,  9,  9,  9,  9, 13,  9,  9,  9,  9,
132 		 9, 13, 11, 11, 11, 11, 11, 15,  0,  0,  0,  0,  0,  0,  8,  8,
133 		 8,  8,  8,  8,  7,  7,  7,  7,  7,  7, 15, 15, 15, 15, 15, 15
134 	};
135 
136 	return (table[c]);
137 }
138 
139 u_char
140 colour_256to88(u_char c)
141 {
142 	static const u_char table[256] = {
143 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
144 		16, 17, 17, 18, 18, 19, 20, 21, 21, 22, 22, 23, 20, 21, 21, 22,
145 		22, 23, 24, 25, 25, 26, 26, 27, 24, 25, 25, 26, 26, 27, 28, 29,
146 		29, 30, 30, 31, 32, 33, 33, 34, 34, 35, 36, 37, 37, 38, 38, 39,
147 		36, 37, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43, 40, 41, 41, 42,
148 		42, 43, 44, 45, 45, 46, 46, 47, 32, 33, 33, 34, 34, 35, 36, 37,
149 		37, 38, 38, 39, 36, 37, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43,
150 		40, 41, 41, 42, 42, 43, 44, 45, 45, 46, 46, 47, 48, 49, 49, 50,
151 		50, 51, 52, 53, 53, 54, 54, 55, 52, 53, 53, 54, 54, 55, 56, 57,
152 		57, 58, 58, 59, 56, 57, 57, 58, 58, 59, 60, 61, 61, 62, 62, 63,
153 		48, 49, 49, 50, 50, 51, 52, 53, 53, 54, 54, 55, 52, 53, 53, 54,
154 		54, 55, 56, 57, 57, 58, 58, 59, 56, 57, 57, 58, 58, 59, 60, 61,
155 		61, 62, 62, 63, 64, 65, 65, 66, 66, 67, 68, 69, 69, 70, 70, 71,
156 		68, 69, 69, 70, 70, 71, 72, 73, 73, 74, 74, 75, 72, 73, 73, 74,
157 		74, 75, 76, 77, 77, 78, 78, 79,  0,  0, 80, 80, 80, 81, 81, 81,
158 		82, 82, 82, 83, 83, 83, 84, 84, 84, 85, 85, 85, 86, 86, 86, 87
159 	};
160 
161 	return (table[c]);
162 }
163