vt_termcolors.c revision 256530
10Sstevel@tonic-gate/*-
20Sstevel@tonic-gate * Copyright (c) 2013 The FreeBSD Foundation
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This software was developed by Aleksandr Rybalko under sponsorship from the
60Sstevel@tonic-gate * FreeBSD Foundation.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate *    notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
140Sstevel@tonic-gate *    notice, this list of conditions and the following disclaimer in the
150Sstevel@tonic-gate *    documentation and/or other materials provided with the distribution.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
180Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200Sstevel@tonic-gate * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
210Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
230Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
250Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
270Sstevel@tonic-gate * SUCH DAMAGE.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * $FreeBSD: user/ed/newcons/sys/dev/vt/colors/vt_termcolors.c 256530 2013-10-15 12:54:47Z ray $
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate#include <sys/param.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate#include <dev/vt/colors/vt_termcolors.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gatestatic struct {
370Sstevel@tonic-gate	unsigned char r;	/* Red percentage value. */
380Sstevel@tonic-gate	unsigned char g;	/* Green percentage value. */
390Sstevel@tonic-gate	unsigned char b;	/* Blue percentage value. */
400Sstevel@tonic-gate} color_def[16] = {
410Sstevel@tonic-gate	{0,	0,	0},	/* black */
420Sstevel@tonic-gate	{0,	0,	50},	/* dark blue */
430Sstevel@tonic-gate	{0,	50,	0},	/* dark green */
440Sstevel@tonic-gate	{0,	50,	50},	/* dark cyan */
450Sstevel@tonic-gate	{50,	0,	0},	/* dark red */
460Sstevel@tonic-gate	{50,	0,	50},	/* dark magenta */
470Sstevel@tonic-gate	{50,	50,	0},	/* brown */
480Sstevel@tonic-gate	{75,	75,	75},	/* light gray */
490Sstevel@tonic-gate	{50,	50,	50},	/* dark gray */
500Sstevel@tonic-gate	{0,	0,	100},	/* light blue */
510Sstevel@tonic-gate	{0,	100,	0},	/* light green */
520Sstevel@tonic-gate	{0,	100,	100},	/* light cyan */
530Sstevel@tonic-gate	{100,	0,	0},	/* light red */
540Sstevel@tonic-gate	{100,	0,	100},	/* light magenta */
550Sstevel@tonic-gate	{100,	100,	0},	/* yellow */
560Sstevel@tonic-gate	{100,	100,	100},	/* white */
570Sstevel@tonic-gate};
580Sstevel@tonic-gate
590Sstevel@tonic-gateint
600Sstevel@tonic-gatevt_generate_vga_palette(uint32_t *palette, int format, uint32_t rmax, int roffset,
610Sstevel@tonic-gate    uint32_t gmax, int goffset, uint32_t bmax, int boffset)
620Sstevel@tonic-gate{
630Sstevel@tonic-gate	int i;
640Sstevel@tonic-gate
650Sstevel@tonic-gate#define	CF(_f, _i) ((_f ## max * color_def[(_i)]._f / 100) << _f ## offset)
660Sstevel@tonic-gate	for (i = 0; i < 16; i++) {
670Sstevel@tonic-gate		switch (format) {
680Sstevel@tonic-gate		case COLOR_FORMAT_VGA:
690Sstevel@tonic-gate			palette[i] = i;
700Sstevel@tonic-gate			break;
710Sstevel@tonic-gate		case COLOR_FORMAT_RGB:
720Sstevel@tonic-gate			palette[i] = CF(r, i) | CF(g, i) | CF(b, i);
730Sstevel@tonic-gate			break;
740Sstevel@tonic-gate		default:
750Sstevel@tonic-gate			return (ENODEV);
760Sstevel@tonic-gate		}
770Sstevel@tonic-gate	}
780Sstevel@tonic-gate#undef	CF
790Sstevel@tonic-gate	return (0);
800Sstevel@tonic-gate}
810Sstevel@tonic-gate