diff options
| author | auric <104602845+ihateamongus@users.noreply.github.com> | 2025-09-08 20:51:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-08 20:51:40 -0500 |
| commit | e2ec2e4acbf07da282c7ce4d4370ed1a83e11e70 (patch) | |
| tree | 4026088b8084647406f27137e456a8d591a1b5cb | |
| parent | 5f17537ca9538c8f2214be62c46823163fd8b5e5 (diff) | |
| parent | 1e78a122dc52f1c791fdcb7733a53d70f3929970 (diff) | |
Merge pull request #17 from ihateamongus/codex/add-staggering-color-effect-demo-in-tools
Add color staggering X demo
| -rw-r--r-- | tools/Makefile | 14 | ||||
| -rw-r--r-- | tools/colorstagger.c | 97 |
2 files changed, 106 insertions, 5 deletions
diff --git a/tools/Makefile b/tools/Makefile index ae33902..e42f8bf 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,16 +1,20 @@ +.RECIPEPREFIX = > CC = cc CFLAGS = -std=c99 -Wall -Wextra -pedantic -Os LDFLAGS = PREFIX ?= /usr/local -all: exofetch +all: exofetch colorstagger exofetch: exofetch.c - $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) +>$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +colorstagger: colorstagger.c +>$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) -lX11 clean: - rm -f exofetch +>rm -f exofetch colorstagger install: exofetch - mkdir -p $(DESTDIR)$(PREFIX)/bin - cp -f exofetch $(DESTDIR)$(PREFIX)/bin +>mkdir -p $(DESTDIR)$(PREFIX)/bin +>cp -f exofetch $(DESTDIR)$(PREFIX)/bin diff --git a/tools/colorstagger.c b/tools/colorstagger.c new file mode 100644 index 0000000..3e68faf --- /dev/null +++ b/tools/colorstagger.c @@ -0,0 +1,97 @@ +#define _XOPEN_SOURCE 500 +#include <X11/Xlib.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +typedef struct { unsigned char r, g, b; } Color; + +static Color +parse(const char *hex) +{ + Color c = {0}; + if (hex[0] == '#') + hex++; + if (strlen(hex) >= 6) { + char buf[3] = {0}; + buf[0] = hex[0]; buf[1] = hex[1]; c.r = strtol(buf, NULL, 16); + buf[0] = hex[2]; buf[1] = hex[3]; c.g = strtol(buf, NULL, 16); + buf[0] = hex[4]; buf[1] = hex[5]; c.b = strtol(buf, NULL, 16); + } + return c; +} + +static unsigned long +pack(Color c) +{ + return ((unsigned long)c.r << 16) | + ((unsigned long)c.g << 8) | + ((unsigned long)c.b); +} + +static Color +mix(Color a, Color b, double t) +{ + Color c; + c.r = a.r + (b.r - a.r) * t; + c.g = a.g + (b.g - a.g) * t; + c.b = a.b + (b.b - a.b) * t; + return c; +} + +int +main(void) +{ + char *colors[] = {"#ff5555", "#f1fa8c", "#50fa7b", "#8be9fd", "#bd93f9"}; + const int ncolors = sizeof(colors) / sizeof(colors[0]); + + Display *d = XOpenDisplay(NULL); + if (!d) { + fprintf(stderr, "cannot open display\n"); + return 1; + } + int s = DefaultScreen(d); + int width = 600, height = 100; + Window w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, width, height, + 1, BlackPixel(d, s), WhitePixel(d, s)); + XSelectInput(d, w, KeyPressMask); + XMapWindow(d, w); + GC gc = XCreateGC(d, w, 0, NULL); + + int grad = 60, solid = 60; + int running = 1; + while (running) { + while (XPending(d)) { + XEvent e; + XNextEvent(d, &e); + if (e.type == KeyPress) + running = 0; + } + + int x = 0; + for (int i = 0; i < ncolors; i++) { + Color a = parse(colors[i]); + Color b = parse(colors[(i + 1) % ncolors]); + for (int k = 0; k < grad; k++) { + double t = k / (double)(grad - 1); + Color c = mix(a, b, t); + XSetForeground(d, gc, pack(c)); + XDrawLine(d, w, gc, x + k, 0, x + k, height); + } + x += grad; + XSetForeground(d, gc, pack(b)); + XFillRectangle(d, w, gc, x, 0, solid, height); + x += solid; + } + XFlush(d); + usleep(500000); + char *tmp = colors[0]; + memmove(colors, colors + 1, (ncolors - 1) * sizeof(char *)); + colors[ncolors - 1] = tmp; + XClearWindow(d, w); + } + + XCloseDisplay(d); + return 0; +} |
