summaryrefslogtreecommitdiffstats
path: root/src/sgr.c
blob: b768e96890528afca4ccf53e474f8e213ced3efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Select Graphic Rendition color parameters function
 *
 * Copyright (C) 2019  Patrick McDermott
 *
 * This file is part of fbcon2png.
 *
 * fbcon2png is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * fbcon2png is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with fbcon2png.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "sgr.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <png.h>

#include "i18n.h"
#include "output.h"

static png_color _4bit[] = {
	{  0,   0,   0},  /* Black */
	{170,   0,   0},  /* Red */
	{  0, 170,   0},  /* Green */
	{170,  85,   0},  /* Yellow */
	{  0,   0, 170},  /* Blue */
	{170,   0, 170},  /* Magenta */
	{  0, 170, 170},  /* Cyan */
	{170, 170, 170},  /* White */
	{ 85,  85,  85},  /* Bright Black */
	{255,  85,  85},  /* Bright Red */
	{ 85, 255,  85},  /* Bright Green */
	{255, 255,  85},  /* Bright Yellow */
	{ 85,  85, 255},  /* Bright Blue */
	{255,  85, 255},  /* Bright Magenta */
	{ 85, 255, 255},  /* Bright Cyan */
	{255, 255, 255},  /* Bright White */
};

/* TODO: Add 8-bit and 24-bit modes. */

void
sgr(unsigned char *param, png_colorp fg, png_colorp bg)
{
	int            end;
	unsigned char *p;
	int            i;

	assert(param);
	assert(fg);
	assert(bg);

	end = 0;
	for (p = param; param; ++param) {
		if (*param == ';' || *param == ':' || *param == '\0') {
			if (*param == '\0') {
				end = 1;
			}
			*param = '\0';
			if (*p) {
				i = atoi((char *) p);
			} else {
				i = 0;
			}
			if (i == 0) {  /* Reset */
				memcpy(fg, &_4bit[7], sizeof(*fg));
				memcpy(bg, &_4bit[0], sizeof(*bg));
			}
			if (i >= 30 && i <= 37) {  /* Set foreground color */
				memcpy(fg, &_4bit[i - 30], sizeof(*fg));
			}
			if (i >= 40 && i <= 47) {  /* Set background color */
				memcpy(bg, &_4bit[i - 40], sizeof(*bg));
			}
			if (i >= 90 && i <= 97) {  /* Set foreground color */
				memcpy(fg, &_4bit[i - 90 + 8], sizeof(*fg));
			}
			if (i >= 100 && i <= 107) {  /* Set background color */
				memcpy(bg, &_4bit[i - 100 + 8], sizeof(*bg));
			}
			if (end) {
				break;
			}
			p = param + 1;
		} else if (*param < '0' || *param > '9') {
			error(_("Invalid SGR parameter \"%s\""), p);
			break;
		}
	}
}