]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/vt/colors/vt_termcolors.c
MFC 219886, 226100, 226111, 226341, 242529, 259015, 259016, 259019, 259049,
[FreeBSD/stable/9.git] / sys / dev / vt / colors / vt_termcolors.c
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$
30  */
31
32 #include <sys/param.h>
33
34 #include <dev/vt/colors/vt_termcolors.h>
35
36 static 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         {0,     0,      50},    /* dark blue */
43         {0,     50,     0},     /* dark green */
44         {0,     50,     50},    /* dark cyan */
45         {50,    0,      0},     /* dark red */
46         {50,    0,      50},    /* dark magenta */
47         {50,    50,     0},     /* brown */
48         {75,    75,     75},    /* light gray */
49         {50,    50,     50},    /* dark gray */
50         {0,     0,      100},   /* light blue */
51         {0,     100,    0},     /* light green */
52         {0,     100,    100},   /* light cyan */
53         {100,   0,      0},     /* light red */
54         {100,   0,      100},   /* light magenta */
55         {100,   100,    0},     /* yellow */
56         {100,   100,    100},   /* white */
57 };
58
59 int
60 vt_generate_vga_palette(uint32_t *palette, int format, uint32_t rmax, int roffset,
61     uint32_t gmax, int goffset, uint32_t bmax, int boffset)
62 {
63         int i;
64
65 #define CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset)
66         for (i = 0; i < 16; i++) {
67                 switch (format) {
68                 case COLOR_FORMAT_VGA:
69                         palette[i] = i;
70                         break;
71                 case COLOR_FORMAT_RGB:
72                         palette[i] = CF(r, i) | CF(g, i) | CF(b, i);
73                         break;
74                 default:
75                         return (ENODEV);
76                 }
77         }
78 #undef  CF
79         return (0);
80 }