summaryrefslogtreecommitdiffstats
path: root/src/sgr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sgr.c')
-rw-r--r--src/sgr.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/sgr.c b/src/sgr.c
new file mode 100644
index 0000000..baec803
--- /dev/null
+++ b/src/sgr.c
@@ -0,0 +1,85 @@
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+
+#include <png.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(char *param, png_colorp fg, png_colorp bg)
+{
+ int end;
+ char *p;
+ int i;
+
+ end = 0;
+ for (p = param; param; ++param) {
+ if (*param == ';' || *param == ':' || *param == '\0') {
+ if (*param == '\0') {
+ end = 1;
+ }
+ *param = '\0';
+ i = atoi(p);
+ 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') {
+ break;
+ }
+ }
+}