/*
* 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 .
*/
#include "sgr.h"
#include
#include
#include
#include
#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;
}
}
}