Home | History | Annotate | Line # | Download | only in dist
colour.c revision 1.2
      1  1.2    he /* $Id: colour.c,v 1.2 2011/08/22 06:52:35 he Exp $ */
      2  1.1  jmmv 
      3  1.1  jmmv /*
      4  1.1  jmmv  * Copyright (c) 2008 Nicholas Marriott <nicm (at) users.sourceforge.net>
      5  1.1  jmmv  *
      6  1.1  jmmv  * Permission to use, copy, modify, and distribute this software for any
      7  1.1  jmmv  * purpose with or without fee is hereby granted, provided that the above
      8  1.1  jmmv  * copyright notice and this permission notice appear in all copies.
      9  1.1  jmmv  *
     10  1.1  jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.1  jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.1  jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.1  jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.1  jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15  1.1  jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16  1.1  jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1  jmmv  */
     18  1.1  jmmv 
     19  1.1  jmmv #include <sys/types.h>
     20  1.1  jmmv 
     21  1.2    he #include <ctype.h>
     22  1.1  jmmv #include <stdlib.h>
     23  1.1  jmmv #include <string.h>
     24  1.1  jmmv 
     25  1.1  jmmv #include "tmux.h"
     26  1.1  jmmv 
     27  1.1  jmmv /*
     28  1.1  jmmv  * Colour to string conversion functions. Bit 8 of the colour means it is one
     29  1.1  jmmv  * of the 256 colour palette.
     30  1.1  jmmv  */
     31  1.1  jmmv 
     32  1.2    he /* An RGB colour. */
     33  1.2    he struct colour_rgb {
     34  1.2    he 	u_char	r;
     35  1.2    he 	u_char	g;
     36  1.2    he 	u_char	b;
     37  1.2    he };
     38  1.2    he 
     39  1.2    he /* 256 colour RGB table, generated on first use. */
     40  1.2    he struct colour_rgb *colour_rgb_256;
     41  1.2    he 
     42  1.2    he void	colour_rgb_generate256(void);
     43  1.2    he u_int	colour_rgb_distance(struct colour_rgb *, struct colour_rgb *);
     44  1.2    he int	colour_rgb_find(struct colour_rgb *);
     45  1.2    he 
     46  1.2    he /* Generate 256 colour RGB table. */
     47  1.2    he void
     48  1.2    he colour_rgb_generate256(void)
     49  1.2    he {
     50  1.2    he 	struct colour_rgb	*rgb;
     51  1.2    he 	u_int			 i, r, g, b;
     52  1.2    he 
     53  1.2    he 	/*
     54  1.2    he 	 * Allocate the table. The first 16 colours are often changed by users
     55  1.2    he 	 * and terminals so don't include them.
     56  1.2    he 	 */
     57  1.2    he 	colour_rgb_256 = xcalloc(240, sizeof *colour_rgb_256);
     58  1.2    he 
     59  1.2    he 	/* Add the colours first. */
     60  1.2    he 	r = g = b = 0;
     61  1.2    he 	for (i = 240; i > 24; i--) {
     62  1.2    he 		rgb = &colour_rgb_256[240 - i];
     63  1.2    he 
     64  1.2    he 		if (r != 0)
     65  1.2    he 			rgb->r = (r * 40) + 55;
     66  1.2    he 		if (g != 0)
     67  1.2    he 			rgb->g = (g * 40) + 55;
     68  1.2    he 		if (b != 0)
     69  1.2    he 			rgb->b = (b * 40) + 55;
     70  1.2    he 
     71  1.2    he 		b++;
     72  1.2    he 		if (b > 5) {
     73  1.2    he 			b = 0;
     74  1.2    he 			g++;
     75  1.2    he 		}
     76  1.2    he 		if (g > 5) {
     77  1.2    he 			g = 0;
     78  1.2    he 			r++;
     79  1.2    he 		}
     80  1.2    he 	}
     81  1.2    he 
     82  1.2    he 	/* Then add the greys. */
     83  1.2    he 	for (i = 24; i > 0; i--) {
     84  1.2    he 		rgb = &colour_rgb_256[240 - i];
     85  1.2    he 
     86  1.2    he 		rgb->r = 8 + (24 - i) * 10;
     87  1.2    he 		rgb->g = 8 + (24 - i) * 10;
     88  1.2    he 		rgb->b = 8 + (24 - i) * 10;
     89  1.2    he 	}
     90  1.2    he }
     91  1.2    he 
     92  1.2    he /* Get a measure of colour RGB distance. */
     93  1.2    he u_int
     94  1.2    he colour_rgb_distance(struct colour_rgb *rgb1, struct colour_rgb *rgb2)
     95  1.2    he {
     96  1.2    he 	int	r, g, b;
     97  1.2    he 
     98  1.2    he 	r = rgb1->r - rgb2->r;
     99  1.2    he 	g = rgb1->g - rgb2->g;
    100  1.2    he 	b = rgb1->b - rgb2->b;
    101  1.2    he 	return (r * r + g * g + b * b);
    102  1.2    he }
    103  1.2    he 
    104  1.2    he /* Work out the nearest colour from the 256 colour set. */
    105  1.2    he int
    106  1.2    he colour_rgb_find(struct colour_rgb *rgb)
    107  1.2    he {
    108  1.2    he 	u_int	distance, lowest;
    109  1.2    he 	u_int	colour, i;
    110  1.2    he 
    111  1.2    he 	if (colour_rgb_256 == NULL)
    112  1.2    he 		colour_rgb_generate256();
    113  1.2    he 
    114  1.2    he 	colour = 16;
    115  1.2    he 	lowest = UINT_MAX;
    116  1.2    he 	for (i = 1; i < 240; i++) {
    117  1.2    he 		distance = colour_rgb_distance(&colour_rgb_256[i], rgb);
    118  1.2    he 		if (distance < lowest) {
    119  1.2    he 			lowest = distance;
    120  1.2    he 			colour = 16 + i;
    121  1.2    he 		}
    122  1.2    he 	}
    123  1.2    he 	return (colour);
    124  1.2    he }
    125  1.2    he 
    126  1.2    he /* Set grid cell foreground colour. */
    127  1.1  jmmv void
    128  1.1  jmmv colour_set_fg(struct grid_cell *gc, int c)
    129  1.1  jmmv {
    130  1.1  jmmv 	if (c & 0x100)
    131  1.1  jmmv 		gc->flags |= GRID_FLAG_FG256;
    132  1.1  jmmv 	gc->fg = c;
    133  1.1  jmmv }
    134  1.1  jmmv 
    135  1.2    he /* Set grid cell background colour. */
    136  1.1  jmmv void
    137  1.1  jmmv colour_set_bg(struct grid_cell *gc, int c)
    138  1.1  jmmv {
    139  1.1  jmmv 	if (c & 0x100)
    140  1.1  jmmv 		gc->flags |= GRID_FLAG_BG256;
    141  1.1  jmmv 	gc->bg = c;
    142  1.1  jmmv }
    143  1.1  jmmv 
    144  1.2    he /* Convert colour to a string. */
    145  1.1  jmmv const char *
    146  1.1  jmmv colour_tostring(int c)
    147  1.1  jmmv {
    148  1.1  jmmv 	static char	s[32];
    149  1.1  jmmv 
    150  1.1  jmmv 	if (c & 0x100) {
    151  1.1  jmmv 		xsnprintf(s, sizeof s, "colour%u", c & ~0x100);
    152  1.1  jmmv 		return (s);
    153  1.1  jmmv 	}
    154  1.1  jmmv 
    155  1.1  jmmv 	switch (c) {
    156  1.1  jmmv 	case 0:
    157  1.1  jmmv 		return ("black");
    158  1.1  jmmv 	case 1:
    159  1.1  jmmv 		return ("red");
    160  1.1  jmmv 	case 2:
    161  1.1  jmmv 		return ("green");
    162  1.1  jmmv 	case 3:
    163  1.1  jmmv 		return ("yellow");
    164  1.1  jmmv 	case 4:
    165  1.1  jmmv 		return ("blue");
    166  1.1  jmmv 	case 5:
    167  1.1  jmmv 		return ("magenta");
    168  1.1  jmmv 	case 6:
    169  1.1  jmmv 		return ("cyan");
    170  1.1  jmmv 	case 7:
    171  1.1  jmmv 		return ("white");
    172  1.1  jmmv 	case 8:
    173  1.1  jmmv 		return ("default");
    174  1.1  jmmv 	}
    175  1.1  jmmv 	return (NULL);
    176  1.1  jmmv }
    177  1.1  jmmv 
    178  1.2    he /* Convert colour from string. */
    179  1.1  jmmv int
    180  1.1  jmmv colour_fromstring(const char *s)
    181  1.1  jmmv {
    182  1.2    he 	const char		*errstr;
    183  1.2    he 	const char		*cp;
    184  1.2    he 	struct colour_rgb	 rgb;
    185  1.2    he 	int			 n;
    186  1.2    he 
    187  1.2    he 	if (*s == '#' && strlen(s) == 7) {
    188  1.2    he 		for (cp = s + 1; isxdigit((u_char) *cp); cp++)
    189  1.2    he 			;
    190  1.2    he 		if (*cp != '\0')
    191  1.2    he 			return (-1);
    192  1.2    he 		n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &rgb.r, &rgb.g, &rgb.b);
    193  1.2    he 		if (n != 3)
    194  1.2    he 			return (-1);
    195  1.2    he 		return (colour_rgb_find(&rgb) | 0x100);
    196  1.2    he 	}
    197  1.1  jmmv 
    198  1.1  jmmv 	if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) {
    199  1.1  jmmv 		n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr);
    200  1.1  jmmv 		if (errstr != NULL)
    201  1.1  jmmv 			return (-1);
    202  1.1  jmmv 		return (n | 0x100);
    203  1.1  jmmv 	}
    204  1.1  jmmv 
    205  1.1  jmmv 	if (strcasecmp(s, "black") == 0 || (s[0] == '0' && s[1] == '\0'))
    206  1.1  jmmv 		return (0);
    207  1.1  jmmv 	if (strcasecmp(s, "red") == 0 || (s[0] == '1' && s[1] == '\0'))
    208  1.1  jmmv 		return (1);
    209  1.1  jmmv 	if (strcasecmp(s, "green") == 0 || (s[0] == '2' && s[1] == '\0'))
    210  1.1  jmmv 		return (2);
    211  1.1  jmmv 	if (strcasecmp(s, "yellow") == 0 || (s[0] == '3' && s[1] == '\0'))
    212  1.1  jmmv 		return (3);
    213  1.1  jmmv 	if (strcasecmp(s, "blue") == 0 || (s[0] == '4' && s[1] == '\0'))
    214  1.1  jmmv 		return (4);
    215  1.1  jmmv 	if (strcasecmp(s, "magenta") == 0 || (s[0] == '5' && s[1] == '\0'))
    216  1.1  jmmv 		return (5);
    217  1.1  jmmv 	if (strcasecmp(s, "cyan") == 0 || (s[0] == '6' && s[1] == '\0'))
    218  1.1  jmmv 		return (6);
    219  1.1  jmmv 	if (strcasecmp(s, "white") == 0 || (s[0] == '7' && s[1] == '\0'))
    220  1.1  jmmv 		return (7);
    221  1.1  jmmv 	if (strcasecmp(s, "default") == 0 || (s[0] == '8' && s[1] == '\0'))
    222  1.1  jmmv 		return (8);
    223  1.1  jmmv 	return (-1);
    224  1.1  jmmv }
    225  1.1  jmmv 
    226  1.2    he /* Convert 256 colour palette to 16. */
    227  1.1  jmmv u_char
    228  1.1  jmmv colour_256to16(u_char c)
    229  1.1  jmmv {
    230  1.1  jmmv 	static const u_char table[256] = {
    231  1.1  jmmv 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
    232  1.1  jmmv 		 0,  4,  4,  4, 12, 12,  2,  6,  4,  4, 12, 12,  2,  2,  6,  4,
    233  1.1  jmmv 		12, 12,  2,  2,  2,  6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10,
    234  1.1  jmmv 		10, 10, 10, 14,  1,  5,  4,  4, 12, 12,  3,  8,  4,  4, 12, 12,
    235  1.1  jmmv 		 2,  2,  6,  4, 12, 12,  2,  2,  2,  6, 12, 12, 10, 10, 10, 10,
    236  1.1  jmmv 		14, 12, 10, 10, 10, 10, 10, 14,  1,  1,  5,  4, 12, 12,  1,  1,
    237  1.1  jmmv 		 5,  4, 12, 12,  3,  3,  8,  4, 12, 12,  2,  2,  2,  6, 12, 12,
    238  1.1  jmmv 		10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,  1,  1,  1,  5,
    239  1.1  jmmv 		12, 12,  1,  1,  1,  5, 12, 12,  1,  1,  1,  5, 12, 12,  3,  3,
    240  1.1  jmmv 		 3,  7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,
    241  1.1  jmmv 		 9,  9,  9,  9, 13, 12,  9,  9,  9,  9, 13, 12,  9,  9,  9,  9,
    242  1.1  jmmv 		13, 12,  9,  9,  9,  9, 13, 12, 11, 11, 11, 11,  7, 12, 10, 10,
    243  1.1  jmmv 		10, 10, 10, 14,  9,  9,  9,  9,  9, 13,  9,  9,  9,  9,  9, 13,
    244  1.1  jmmv 		 9,  9,  9,  9,  9, 13,  9,  9,  9,  9,  9, 13,  9,  9,  9,  9,
    245  1.1  jmmv 		 9, 13, 11, 11, 11, 11, 11, 15,  0,  0,  0,  0,  0,  0,  8,  8,
    246  1.1  jmmv 		 8,  8,  8,  8,  7,  7,  7,  7,  7,  7, 15, 15, 15, 15, 15, 15
    247  1.1  jmmv 	};
    248  1.1  jmmv 
    249  1.1  jmmv 	return (table[c]);
    250  1.1  jmmv }
    251  1.1  jmmv 
    252  1.2    he /* Convert 256 colour palette to 88. */
    253  1.1  jmmv u_char
    254  1.1  jmmv colour_256to88(u_char c)
    255  1.1  jmmv {
    256  1.1  jmmv 	static const u_char table[256] = {
    257  1.1  jmmv 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
    258  1.1  jmmv 		16, 17, 17, 18, 18, 19, 20, 21, 21, 22, 22, 23, 20, 21, 21, 22,
    259  1.1  jmmv 		22, 23, 24, 25, 25, 26, 26, 27, 24, 25, 25, 26, 26, 27, 28, 29,
    260  1.1  jmmv 		29, 30, 30, 31, 32, 33, 33, 34, 34, 35, 36, 37, 37, 38, 38, 39,
    261  1.1  jmmv 		36, 37, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43, 40, 41, 41, 42,
    262  1.1  jmmv 		42, 43, 44, 45, 45, 46, 46, 47, 32, 33, 33, 34, 34, 35, 36, 37,
    263  1.1  jmmv 		37, 38, 38, 39, 36, 37, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43,
    264  1.1  jmmv 		40, 41, 41, 42, 42, 43, 44, 45, 45, 46, 46, 47, 48, 49, 49, 50,
    265  1.1  jmmv 		50, 51, 52, 53, 53, 54, 54, 55, 52, 53, 53, 54, 54, 55, 56, 57,
    266  1.1  jmmv 		57, 58, 58, 59, 56, 57, 57, 58, 58, 59, 60, 61, 61, 62, 62, 63,
    267  1.1  jmmv 		48, 49, 49, 50, 50, 51, 52, 53, 53, 54, 54, 55, 52, 53, 53, 54,
    268  1.1  jmmv 		54, 55, 56, 57, 57, 58, 58, 59, 56, 57, 57, 58, 58, 59, 60, 61,
    269  1.1  jmmv 		61, 62, 62, 63, 64, 65, 65, 66, 66, 67, 68, 69, 69, 70, 70, 71,
    270  1.1  jmmv 		68, 69, 69, 70, 70, 71, 72, 73, 73, 74, 74, 75, 72, 73, 73, 74,
    271  1.1  jmmv 		74, 75, 76, 77, 77, 78, 78, 79,  0,  0, 80, 80, 80, 81, 81, 81,
    272  1.1  jmmv 		82, 82, 82, 83, 83, 83, 84, 84, 84, 85, 85, 85, 86, 86, 86, 87
    273  1.1  jmmv 	};
    274  1.1  jmmv 
    275  1.1  jmmv 	return (table[c]);
    276  1.1  jmmv }
    277