Annotation of src/usr.bin/tmux/colour.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 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 <string.h>
! 22:
! 23: #include "tmux.h"
! 24:
! 25: const char *
! 26: colour_tostring(u_char c)
! 27: {
! 28: switch (c) {
! 29: case 0:
! 30: return ("black");
! 31: case 1:
! 32: return ("red");
! 33: case 2:
! 34: return ("green");
! 35: case 3:
! 36: return ("yellow");
! 37: case 4:
! 38: return ("blue");
! 39: case 5:
! 40: return ("magenta");
! 41: case 6:
! 42: return ("cyan");
! 43: case 7:
! 44: return ("white");
! 45: case 8:
! 46: return ("default");
! 47: }
! 48: return (NULL);
! 49: }
! 50:
! 51: int
! 52: colour_fromstring(const char *s)
! 53: {
! 54: if (strcasecmp(s, "black") == 0 || (s[0] == '0' && s[1] == '\0'))
! 55: return (0);
! 56: if (strcasecmp(s, "red") == 0 || (s[0] == '1' && s[1] == '\0'))
! 57: return (1);
! 58: if (strcasecmp(s, "green") == 0 || (s[0] == '2' && s[1] == '\0'))
! 59: return (2);
! 60: if (strcasecmp(s, "yellow") == 0 || (s[0] == '3' && s[1] == '\0'))
! 61: return (3);
! 62: if (strcasecmp(s, "blue") == 0 || (s[0] == '4' && s[1] == '\0'))
! 63: return (4);
! 64: if (strcasecmp(s, "magenta") == 0 || (s[0] == '5' && s[1] == '\0'))
! 65: return (5);
! 66: if (strcasecmp(s, "cyan") == 0 || (s[0] == '6' && s[1] == '\0'))
! 67: return (6);
! 68: if (strcasecmp(s, "white") == 0 || (s[0] == '7' && s[1] == '\0'))
! 69: return (7);
! 70: if (strcasecmp(s, "default") == 0 || (s[0] == '8' && s[1] == '\0'))
! 71: return (8);
! 72: return (-1);
! 73: }
! 74:
! 75: u_char
! 76: colour_256to16(u_char c)
! 77: {
! 78: static const u_char table[256] = {
! 79: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
! 80: 0, 4, 4, 4, 12, 12, 2, 6, 4, 4, 12, 12, 2, 2, 6, 4,
! 81: 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10,
! 82: 10, 10, 10, 14, 1, 5, 4, 4, 12, 12, 3, 8, 4, 4, 12, 12,
! 83: 2, 2, 6, 4, 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10,
! 84: 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 5, 4, 12, 12, 1, 1,
! 85: 5, 4, 12, 12, 3, 3, 8, 4, 12, 12, 2, 2, 2, 6, 12, 12,
! 86: 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 1, 5,
! 87: 12, 12, 1, 1, 1, 5, 12, 12, 1, 1, 1, 5, 12, 12, 3, 3,
! 88: 3, 7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,
! 89: 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 13, 12, 9, 9, 9, 9,
! 90: 13, 12, 9, 9, 9, 9, 13, 12, 11, 11, 11, 11, 7, 12, 10, 10,
! 91: 10, 10, 10, 14, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13,
! 92: 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9,
! 93: 9, 13, 11, 11, 11, 11, 11, 15, 0, 0, 0, 0, 0, 0, 8, 8,
! 94: 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15
! 95: };
! 96:
! 97: return (table[c]);
! 98: }
! 99:
! 100: u_char
! 101: colour_256to88(u_char c)
! 102: {
! 103: static const u_char table[256] = {
! 104: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
! 105: 16, 17, 17, 18, 18, 19, 20, 21, 21, 22, 22, 23, 20, 21, 21, 22,
! 106: 22, 23, 24, 25, 25, 26, 26, 27, 24, 25, 25, 26, 26, 27, 28, 29,
! 107: 29, 30, 30, 31, 32, 33, 33, 34, 34, 35, 36, 37, 37, 38, 38, 39,
! 108: 36, 37, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43, 40, 41, 41, 42,
! 109: 42, 43, 44, 45, 45, 46, 46, 47, 32, 33, 33, 34, 34, 35, 36, 37,
! 110: 37, 38, 38, 39, 36, 37, 37, 38, 38, 39, 40, 41, 41, 42, 42, 43,
! 111: 40, 41, 41, 42, 42, 43, 44, 45, 45, 46, 46, 47, 48, 49, 49, 50,
! 112: 50, 51, 52, 53, 53, 54, 54, 55, 52, 53, 53, 54, 54, 55, 56, 57,
! 113: 57, 58, 58, 59, 56, 57, 57, 58, 58, 59, 60, 61, 61, 62, 62, 63,
! 114: 48, 49, 49, 50, 50, 51, 52, 53, 53, 54, 54, 55, 52, 53, 53, 54,
! 115: 54, 55, 56, 57, 57, 58, 58, 59, 56, 57, 57, 58, 58, 59, 60, 61,
! 116: 61, 62, 62, 63, 64, 65, 65, 66, 66, 67, 68, 69, 69, 70, 70, 71,
! 117: 68, 69, 69, 70, 70, 71, 72, 73, 73, 74, 74, 75, 72, 73, 73, 74,
! 118: 74, 75, 76, 77, 77, 78, 78, 79, 0, 0, 80, 80, 80, 81, 81, 81,
! 119: 82, 82, 82, 83, 83, 83, 84, 84, 84, 85, 85, 85, 86, 86, 86, 87
! 120: };
! 121:
! 122: return (table[c]);
! 123: }