shithub: slug

ref: 6e8d03dbe4e3f84c5aff74e8f72c8bd69e06009e
dir: /color.c/

View raw version
#include "a.h"

typedef struct Color Color;

struct Color
{
	ulong n;
	Image *i;
};

Color cache[255] = {0};
int ncolors = 0;

Image*
ealloccolor(ulong n)
{
	Image *i;

	i = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, n);
	if(i == nil)
		sysfatal("allocimage: %r");
	return i;
}

Image*
color(int r, int g, int b)
{
	ulong n;
	Image *c;
	int i;

	c = nil;
	n = (r << 24) | (g << 16) | (b << 8) | 0xFF;
	for(i = 0; i < nelem(cache); i++){
		if(cache[i].n == n){
			c = cache[i].i;
			break;
		}
	}
	if(c == nil){
		c = ealloccolor(n);
		cache[ncolors].n = n;
		cache[ncolors].i = c;
		ncolors++;
		if(ncolors == nelem(cache))
			ncolors = 0;
	}
	return c;
}

Image*
grayscale(int n)
{
	return color(n, n, n);
}