new file: bin/color

modified:   src/color.c
	new file:   src/color.h
This commit is contained in:
Brett Weiland 2020-07-26 12:46:01 -05:00
parent dd3d885a31
commit ee39e60edd
3 changed files with 98 additions and 36 deletions

BIN
bin/color Executable file

Binary file not shown.

View File

@ -5,19 +5,59 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <math.h> #include <math.h>
#include <signal.h>
#include "color.h" #include "color.h"
int add_color(color_info color) { int add_color(update_info *color, int key) {
XFreeColors(color->display, color->colormap, &color->background.pixel, 1, 0);
XFreeColors(color->display, color->colormap, &color->foreground.pixel, 1, 0);
printf("%i\n", color->channel_step);
switch(key){
case X_Q_KEY:
color->background.red += color->channel_step;
break;
case X_W_KEY:
color->background.green += color->channel_step;
printf("%i\n", color->background.green);
break;
case X_E_KEY:
color->background.blue += color->channel_step;
break;
case X_A_KEY:
color->background.red -= color->channel_step;
break;
case X_S_KEY:
color->background.green -= color->channel_step;
break;
case X_D_KEY:
color->background.blue -= color->channel_step;
break;
default:
printf("what the actual fuck is wrong with your program you cunt\n");
break;
}
color->foreground.red = 0xffff - color->background.red;
color->foreground.green = 0xffff - color->background.green;
color->foreground.blue = 0xffff - color->background.blue;
XAllocColor(color->display, color->colormap, &color->foreground);
XAllocColor(color->display, color->colormap, &color->background);
} }
int redraw_display(update_info *display_summary, XTextItem *text_prompt, GC *gc, unsigned int textpos_y) {
XClearWindow(display_summary->display, display_summary->window);
XSetWindowBackground(display_summary->display, display_summary->window, display_summary->background.pixel);
XSetForeground(display_summary->display, *gc, display_summary->foreground.pixel);
printf("%i\n", display_summary->foreground.red);
XDrawText(display_summary->display, display_summary->window, *gc, TEXT_CORNER_OFFSET, textpos_y, text_prompt, 1);
}
int main(void) { int main(void) {
char text_prompt_buffer[PROMPT_BUFSIZE] = ""; char text_prompt_buffer[PROMPT_BUFSIZE] = "Q/W/E increases R/G/B. A/S/D decreases R/G/B. Enter allows exact value. Press Escape to dismiss.";
Display *display; Display *display;
Window window; Window window;
@ -53,9 +93,9 @@ int main(void) {
//background colormap //background colormap
colormap = DefaultColormap(display, screen); colormap = DefaultColormap(display, screen);
background_color.red = 1000; background_color.red = 0;
background_color.green = 0; background_color.green = 0;
background_color.blue = 60000; background_color.blue = 0;
//text colormap //text colormap
foreground_color.red = 0xffff - background_color.red; foreground_color.red = 0xffff - background_color.red;
@ -71,7 +111,7 @@ int main(void) {
window = XCreateSimpleWindow(display, RootWindow(display, screen), window = XCreateSimpleWindow(display, RootWindow(display, screen),
10, 10, 10, 10,
100, 100, 1920, 1080,
0, 0,
background_color.pixel, background_color.pixel,
background_color.pixel); //last arg is color background_color.pixel); //last arg is color
@ -79,7 +119,8 @@ int main(void) {
XMapWindow(display, window); XMapWindow(display, window);
XSelectInput(display, window, KeyPressMask | ExposureMask); XSelectInput(display, window, KeyPressMask | ExposureMask);
XGetWindowAttributes(display, window, &win_info); XGetWindowAttributes(display, window, &win_info);
color_step = 0xffff / pow(2, (win_info.depth / 3)) ; //TODO: this math is techincly one off in very, very specific scenarios
color_step = 0xffff / pow(2, (win_info.depth / 3)) + 0.5;
@ -112,18 +153,26 @@ int main(void) {
XNextEvent(display, &event); XNextEvent(display, &event);
switch (event.type) { switch (event.type) {
case KeyPress: case KeyPress:
printf("%i\n", event.xkey.keycode);
if(is_prompt) { if(is_prompt) {
XClearWindow(display, window); XClearWindow(display, window);
text_buffer_space = PROMPT_BUFSIZE - text_prompt.nchars; text_buffer_space = PROMPT_BUFSIZE - text_prompt.nchars;
switch(event.xkey.keycode) {
if(event.xkey.keycode == X_BACKSPACE) { case X_BACKSPACE:
if(text_buffer_space < PROMPT_BUFSIZE) { if(text_buffer_space < PROMPT_BUFSIZE) {
text_prompt.nchars--; text_prompt.nchars--;
} }
} break;
else if(text_buffer_space > 0) { case X_ESCAPE_KEY:
text_prompt.nchars += XLookupString(&event.xkey, text_prompt_buffer + text_prompt.nchars, text_buffer_space, NULL, NULL); text_prompt.nchars = 0;
is_prompt = 0;
break;
default:
if(text_buffer_space > 0) {
text_prompt.nchars += XLookupString(&event.xkey, text_prompt_buffer + text_prompt.nchars, text_buffer_space, NULL, NULL);
}
break;
} }
XDrawText(display, window, text_prompt_gc, TEXT_CORNER_OFFSET, textpos_y, &text_prompt, 1); XDrawText(display, window, text_prompt_gc, TEXT_CORNER_OFFSET, textpos_y, &text_prompt, 1);
} }
@ -131,31 +180,20 @@ int main(void) {
switch(event.xkey.keycode) { switch(event.xkey.keycode) {
case X_ENTER_KEY: case X_ENTER_KEY:
break; break;
case X_Q_KEY: case X_Q_KEY: //if anyone is reading this, on a scale from 1-10,
printf("%i\n", channel_step); case X_W_KEY: //just how ugly is this?
XFreeColors(display, colormap, &background_color.pixel, 1, 0); case X_E_KEY: //I don't know why, but it feels gross.
XFreeColors(display, colormap, &foreground_color.pixel, 1, 0); case X_A_KEY: //to be fair, most of this code is just for me.
background_color.red += channel_step; case X_S_KEY: //otherwise, it'd be way cleaner.
foreground_color.red = 0xffff - background_color.red; //we could just sub channel_step, but eh. i donno.
printf("----%i\n", background_color.red);
XAllocColor(display, colormap, &foreground_color);
XAllocColor(display, colormap, &background_color);
XClearWindow(display, window);
XSetWindowBackground(display, window, background_color.pixel);
break;
case X_W_KEY:
break;
case X_E_KEY:
break;
case X_A_KEY:
break;
case X_S_KEY:
break;
case X_D_KEY: case X_D_KEY:
add_color(&display_summary, event.xkey.keycode);
break; break;
case X_ESCAPE_KEY:
text_prompt.nchars = 0;
default: default:
break; break;
} }
redraw_display(&display_summary, &text_prompt, &text_prompt_gc, textpos_y);
} }
@ -165,7 +203,7 @@ int main(void) {
case Expose: case Expose:
XGetWindowAttributes(display, window, &win_info); XGetWindowAttributes(display, window, &win_info);
textpos_y = win_info.height - TEXT_CORNER_OFFSET; textpos_y = win_info.height - TEXT_CORNER_OFFSET;
XDrawText(display, window, text_prompt_gc, TEXT_CORNER_OFFSET, textpos_y, &text_prompt, 1); redraw_display(&display_summary, &text_prompt, &text_prompt_gc, textpos_y);
break; break;
default: default:
break; break;

24
src/color.h Normal file
View File

@ -0,0 +1,24 @@
#define PROMPT_BUFSIZE 256
#define X_ENTER_KEY 36
#define X_BACKSPACE 22
#define X_ESCAPE_KEY 9
#define X_Q_KEY 24
#define X_W_KEY 25
#define X_E_KEY 26
#define X_A_KEY 38
#define X_S_KEY 39
#define X_D_KEY 40
#define TEXT_CORNER_OFFSET 25
typedef struct {
Window window;
Display *display;
Colormap colormap;
XColor background;
XColor foreground;
unsigned int channel_step;
} update_info;