From 66d3a7f0542309cab61ebd7680b033bff105a892 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 8 Oct 2024 07:25:56 +0200 Subject: [PATCH] checl color number avaliable --- color.c | 41 ++++++++++++++++++++++++++--------------- color.h | 2 ++ ndisplay.c | 20 ++++++++++++-------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/color.c b/color.c index 16b242c..4e3dc48 100644 --- a/color.c +++ b/color.c @@ -305,10 +305,28 @@ color_t colors3[nbcolors3] = { { 0, 0.0, 0.0, 0.0 }, }; +color_t *_findcolor (float red, float green, float blue, color_t *colors, int nbcolors) +{ + float dist = 3; + color_t *color = NULL; + int i; + for (i = 0; i < nbcolors; i++) { + float r = (colors + i)->red - red; + float g = (colors + i)->green - green; + float b = (colors + i)->blue - blue; + float d = r * r + g * g + b * b; + if (d < dist) { + dist = d; + color = colors + i; + } + } + return color; +} + color_t *findcolor (float red, float green, float blue, int colormap) { - int nbcolors; - color_t *colors; + int nbcolors = 0; + color_t *colors = NULL; switch (colormap) { case 0: nbcolors = nbcolors0; @@ -325,23 +343,11 @@ color_t *findcolor (float red, float green, float blue, int colormap) } color_t *color = NULL; - int i; - float dist; switch (colormap) { case 0: case 1: case 2: - dist = 3; - for (i = 0; i < nbcolors; i++) { - float r = (colors + i)->red - red; - float g = (colors + i)->green - green; - float b = (colors + i)->blue - blue; - float d = r * r + g * g + b * b; - if (d < dist) { - dist = d; - color = colors + i; - } - } + color = _findcolor (red, green, blue, colors, nbcolors); break; default: case 3: @@ -370,4 +376,9 @@ void cprint24 (color_t *color, char *str) printf ("\e[48;2;%d;%d;%dm%s\e[0m", (color + 0)->code, (color + 1)->code, (color + 2)->code, str); } +color_t *findncolor (float red, float green, float blue, int nbcolors) +{ + return _findcolor (red, green, blue, colors2, nbcolors); +} + /* vim: set ts=4 sw=4 et: */ diff --git a/color.h b/color.h index 404d201..a96ef56 100644 --- a/color.h +++ b/color.h @@ -16,6 +16,8 @@ void cprint8 (color_t *color, char *str); void cprint24 (color_t *color, char *str); +color_t *findncolor (float red, float green, float blue, int nbcolors); + #endif /* __COLOR_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/ndisplay.c b/ndisplay.c index f6ed0b0..572e9cb 100644 --- a/ndisplay.c +++ b/ndisplay.c @@ -22,12 +22,13 @@ char *progname = NULL; char *version = "0.1"; -int colormap = 2; float gf[3] = { 1.0f, 1.0f, 1.0f }; int mode = 1; int scale = 1; #define MAXSCALE 4 +#define MAXCOLORS 256 + char *help = " Move up\n" " Move left\n" @@ -115,10 +116,6 @@ int main (int argc, char *argv[]) } /* check */ - if ((colormap < 0) || (colormap > 3)) { - VERBOSE (ERROR, fprintf (stderr, "incorrect colormap (%d)\n", colormap)); - return 1; - } if ((gf[0] <= 0) || (gf[1] <= 0) || (gf[2] <= 0)) { VERBOSE (ERROR, fprintf (stderr, "incorrect gamma (%.1f:%.1f;%.1f)\n", gf[0], gf[1], gf[2])); return 1; @@ -151,13 +148,19 @@ int main (int argc, char *argv[]) curs_set (0); start_color (); + /* check */ + if (COLORS == 0) { + VERBOSE (ERROR, fprintf (stderr, "no color avaliable\n")); + return 1; + } + /* cursor definition */ int xcursor = 0; int ycursor = 0; /* init colormap */ int c; - for (c = 0; c < 256; c++) { + for (c = 0; c < MAXCOLORS; c++) { init_pair (c, 0, c); } @@ -188,9 +191,9 @@ int main (int argc, char *argv[]) if (ind >= images[scale]->width * images[scale]->height) { continue; } - color_t *color = findcolor (correction (images[scale]->red[ind], gf[0]), + color_t *color = findncolor (correction (images[scale]->red[ind], gf[0]), correction (images[scale]->green[ind], gf[1]), - correction (images[scale]->blue[ind], gf[2]), colormap); + correction (images[scale]->blue[ind], gf[2]), COLORS); attron (COLOR_PAIR (color->code)); move (y, x * mode); @@ -276,6 +279,7 @@ int main (int argc, char *argv[]) } /* test: ndisplay.exe 2>&1 | grep 'no file specified' */ +/* test: TERM=vt100 ndisplay.exe image/laurent4p.ppm 2>&1 | grep 'no color avaliable' */ /* test: ndisplay.exe -h | grep usage */ /* test: ndisplay.exe -v 2>&1 | grep missing */ /* test: ndisplay.exe image/bogus1.ppm 2>&1 | grep "can't read PNM file" */ -- 2.30.2