ref: 05fa071dc3ea7a5208f3747ac9d93f4d3e256b2f
dir: /colors.c/
#include <u.h> #include <libc.h> #include <draw.h> #include <thread.h> #include <cursor.h> #include <mouse.h> #include <keyboard.h> #include <frame.h> #include <fcall.h> #include "dat.h" #include "fns.h" Image* gethexcolor(Window *w, ulong val) { int i; Image *img; for(i=0; i<w->nhexcols; i++) if(w->hexcols[i].val == val) return w->hexcols[i].img; if(w->nhexcols >= w->maxhexcols) { w->maxhexcols += 32; w->hexcols = erealloc(w->hexcols, w->maxhexcols*sizeof(Hexcolor)); } img = allocimage(display, Rect(0,0,1,1), RGBA32, 1, val); if(img == nil) return nil; w->hexcols[w->nhexcols].val = val; w->hexcols[w->nhexcols].img = img; w->nhexcols++; return img; } static ulong parsecolorvalue(char **s) { char *p, *e; ulong c; p = *s; if(strncmp(p, "0x", 2) == 0) { c = strtoul(p, &e, 16); if(e > p) { *s = e; return c; } return DEFAULT; } if(strncmp(p, "red", 3) == 0) { *s = p + 3; return RED; } if(strncmp(p, "green", 5) == 0) { *s = p + 5; return GREEN; } if(strncmp(p, "blue", 4) == 0) { *s = p + 4; return BLUE; } if(strncmp(p, "cyan", 4) == 0) { *s = p + 4; return CYAN; } if(strncmp(p, "magenta", 7) == 0) { *s = p + 7; return MAGENTA; } if(strncmp(p, "yellow", 6) == 0) { *s = p + 6; return YELLOW; } if(strncmp(p, "black", 5) == 0) { *s = p + 5; return BLACK; } if(strncmp(p, "white", 5) == 0) { *s = p + 5; return WHITE; } return DEFAULT; } int parsecolor(char *cmd, int *fg, int *bg, char **text) { *fg = DEFAULT; *bg = DEFAULT; /* Skip whitespace */ while(*cmd == ' ' || *cmd == '\t') cmd++; /* If no color indicators, just return the text as-is */ if(*cmd != '+' && *cmd != '-') { *text = cmd; return 0; } /* Check for foreground */ if(*cmd == '+') { cmd++; *fg = parsecolorvalue(&cmd); /* Only check for background if explicitly marked */ if(*cmd == '-') { cmd++; *bg = parsecolorvalue(&cmd); } } /* Check for background only */ else if(*cmd == '-') { cmd++; *bg = parsecolorvalue(&cmd); } while(*cmd == ' ' || *cmd == '\t') cmd++; *text = cmd; return 0; }