BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
860e91b
Branches
Tags
master
v0.1
v0.2
v0.3
v0.3.1
v0.4
v0.4.1
v0.5
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.7.0
v0.7.1
v0.7.10
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.8.0
v0.8.1
BlinkenLib
src
BlinkenColorizer.c
format
Stefan Schuermans
commited
860e91b
at 2023-08-18 09:55:23
BlinkenColorizer.c
Blame
History
Raw
/* BlinkenLib Copyright 2004-2014 Stefan Schuermans <stefan@schuermans.info> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <string.h> #include <BlinkenLib/BlinkenColorizer.h> #include "Tools.h" unsigned char BlinkenColorizerSolid(int step, int channels, int y, int x, int c) { int substep, updown, chan; step %= channels * 2 * 254; substep = step % 254; updown = (step / 254) % 2; chan = (step / 254) / 2; if (updown == 0) { if (c == chan) return 255; else if (c == (chan + 1) % channels) return substep; else return 0; } else { if (c == chan) return 255 - substep; else if (c == (chan + 1) % channels) return 255; else return 0; } (void)x; // keep compiler quiet (void)y; // keep compiler quiet } unsigned char BlinkenColorizerRainbow(int step, int channels, int y, int x, int c) { return BlinkenColorizerSolid(step + ((x + y) * 64), channels, y, x, c); } // type describing a colorizing modes typedef struct _BlinkenColorizingModeDesc { const char *name; unsigned char (*func)(int step, int channels, int y, int x, int c); } BlinkenColorizingModeDesc; // supported colorizing modes static const BlinkenColorizingModeDesc BlinkenColorizingModeDescs[] = { {"solid", BlinkenColorizerSolid}, {"rainbow", BlinkenColorizerRainbow}, }; // convert string to colorizing mode // returns colorizing mode (>= 0) or -1 on error int BlinkenColorizerStr2Mode(const char *str) { unsigned int i; for (i = 0; i < arr_cnt(BlinkenColorizingModeDescs); i++) if (strcmp(str, BlinkenColorizingModeDescs[i].name) == 0) break; if (i < arr_cnt(BlinkenColorizingModeDescs)) return i; return -1; } // convert colorizing mode to string const char *BlinkenColorizerMode2Str(int mode) { if (mode < 0 || mode >= (int)arr_cnt(BlinkenColorizingModeDescs)) return "(unknown)"; return BlinkenColorizingModeDescs[mode].name; } // get color for colorizing unsigned char BlinkenColorizerGetColor(int channels, int mode, int step, int y, int x, int c) { if (mode < 0 || mode >= (int)arr_cnt(BlinkenColorizingModeDescs)) return 255; return BlinkenColorizingModeDescs[mode].func(step, channels, y, x, c); }