xref: /freebsd/sys/dev/vt/colors/vt_termcolors.c (revision a0ee8cc6)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Aleksandr Rybalko under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 
35 #include <dev/vt/colors/vt_termcolors.h>
36 
37 static const struct {
38 	unsigned char r;	/* Red percentage value. */
39 	unsigned char g;	/* Green percentage value. */
40 	unsigned char b;	/* Blue percentage value. */
41 } color_def[16] = {
42 	{0,	0,	0},	/* black */
43 	{50,	0,	0},	/* dark red */
44 	{0,	50,	0},	/* dark green */
45 	{77,	63,	0},	/* dark yellow */
46 	{20,	40,	64},	/* dark blue */
47 	{50,	0,	50},	/* dark magenta */
48 	{0,	50,	50},	/* dark cyan */
49 	{75,	75,	75},	/* light gray */
50 
51 	{18,	20,	21},	/* dark gray */
52 	{100,	0,	0},	/* light red */
53 	{0,	100,	0},	/* light green */
54 	{100,	100,	0},	/* light yellow */
55 	{45,	62,	81},	/* light blue */
56 	{100,	0,	100},	/* light magenta */
57 	{0,	100,	100},	/* light cyan */
58 	{100,	100,	100},	/* white */
59 };
60 
61 /*
62  * Between console's palette and VGA's one:
63  *   - blue and red are swapped (1 <-> 4)
64  *   - yellow ad cyan are swapped (3 <-> 6)
65  */
66 static const int cons_to_vga_colors[16] = {
67 	0, 4, 2, 6, 1, 5, 3, 7,
68 	0, 4, 2, 6, 1, 5, 3, 7
69 };
70 
71 int
72 vt_generate_cons_palette(uint32_t *palette, int format, uint32_t rmax,
73     int roffset, uint32_t gmax, int goffset, uint32_t bmax, int boffset)
74 {
75 	int i;
76 
77 #define	CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset)
78 	for (i = 0; i < 16; i++) {
79 		switch (format) {
80 		case COLOR_FORMAT_VGA:
81 			palette[i] = cons_to_vga_colors[i];
82 			break;
83 		case COLOR_FORMAT_RGB:
84 			palette[i] = CF(r, i) | CF(g, i) | CF(b, i);
85 			break;
86 		default:
87 			return (ENODEV);
88 		}
89 	}
90 #undef	CF
91 	return (0);
92 }
93