vt_termcolors.c revision 270262
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 * $FreeBSD: stable/10/sys/dev/vt/colors/vt_termcolors.c 270262 2014-08-21 10:18:42Z dumbbell $
30 */
31
32#include <sys/param.h>
33
34#include <dev/vt/colors/vt_termcolors.h>
35
36static struct {
37	unsigned char r;	/* Red percentage value. */
38	unsigned char g;	/* Green percentage value. */
39	unsigned char b;	/* Blue percentage value. */
40} color_def[16] = {
41	{0,	0,	0},	/* black */
42	{50,	0,	0},	/* dark red */
43	{0,	50,	0},	/* dark green */
44	{77,	63,	0},	/* dark yellow */
45	{20,	40,	64},	/* dark blue */
46	{50,	0,	50},	/* dark magenta */
47	{0,	50,	50},	/* dark cyan */
48	{75,	75,	75},	/* light gray */
49
50	{18,	20,	21},	/* dark gray */
51	{100,	0,	0},	/* light red */
52	{0,	100,	0},	/* light green */
53	{100,	100,	0},	/* light yellow */
54	{45,	62,	81},	/* light blue */
55	{100,	0,	100},	/* light magenta */
56	{0,	100,	100},	/* light cyan */
57	{100,	100,	100},	/* white */
58};
59
60/*
61 * Between console's palette and VGA's one:
62 *   - blue and red are swapped (1 <-> 4)
63 *   - yellow ad cyan are swapped (3 <-> 6)
64 */
65static const int cons_to_vga_colors[16] = {
66	0, 4, 2, 6, 1, 5, 3, 7,
67	0, 4, 2, 6, 1, 5, 3, 7
68};
69
70int
71vt_generate_cons_palette(uint32_t *palette, int format, uint32_t rmax, int roffset,
72    uint32_t gmax, int goffset, uint32_t bmax, int boffset)
73{
74	int i;
75
76#define	CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset)
77	for (i = 0; i < 16; i++) {
78		switch (format) {
79		case COLOR_FORMAT_VGA:
80			palette[i] = cons_to_vga_colors[i];
81			break;
82		case COLOR_FORMAT_RGB:
83			palette[i] = CF(r, i) | CF(g, i) | CF(b, i);
84			break;
85		default:
86			return (ENODEV);
87		}
88	}
89#undef	CF
90	return (0);
91}
92