ref: df5ab4df0f996f4d1894c9cbe0f3edc10aca848a
parent: fc6a696cb43ffd4dd373118bdae89a4053341414
author: phil9 <telephil9@gmail.com>
date: Wed Nov 30 11:14:49 EST 2022
change color handling all color related functions (background, fill and stroke) can now take 1 or 3 arguments. In the first case, the argument correspond to a shade of gray (between 0 and 255). In the second case, the 3 arguments correspond to the RGB components of the color.
--- a/a.h
+++ b/a.h
@@ -8,14 +8,6 @@
#include <lauxlib.h>
#include <lualib.h>
-typedef struct Color Color;
-
-struct Color
-{
- ulong color;
- Image *i;
-};
-
void resize(lua_State*, int, int);
void drawcanvas(void);
@@ -22,7 +14,8 @@
void initstate(void);
void registerfuncs(lua_State*);
-Image* getcolor(int);
+Image* color(int, int, int);
+Image* grayscale(int);
extern int drawing;
extern Image* canvas;
--- a/api.c
+++ b/api.c
@@ -47,14 +47,36 @@
return 0;
}
+Image*
+getcolor(lua_State *L)
+{
+ Image *i;
+ int c, r, g, b;
+
+ c = lua_gettop(L);
+ if(c == 1){
+ g = luaL_checkinteger(L, 1);
+ i = grayscale(g);
+ }else if(c == 3){
+ r = luaL_checkinteger(L, 1);
+ g = luaL_checkinteger(L, 2);
+ b = luaL_checkinteger(L, 3);
+ i = color(r, g, b);
+ }else{
+ fprint(2, "invalid color request\n");
+ return nil;
+ }
+ return i;
+}
+
int
cbackground(lua_State *L)
{
Image *i;
- int n;
- n = luaL_checkinteger(L, 1);
- i = getcolor(n);
+ i = getcolor(L);
+ if(i == nil)
+ return 1;
draw(canvas, canvas->r, i, nil, ZP);
return 0;
}
@@ -69,10 +91,12 @@
int
cstroke(lua_State *L)
{
- int n;
+ Image *i;
- n = luaL_checkinteger(L, 1);
- stroke = getcolor(n);
+ i = getcolor(L);
+ if(i == nil)
+ return 1;
+ stroke = i;
nostroke = 0;
return 0;
}
@@ -87,10 +111,12 @@
int
cfill(lua_State *L)
{
- int n;
+ Image *i;
- n = luaL_checkinteger(L, 1);
- fill = getcolor(n);
+ i = getcolor(L);
+ if(i == nil)
+ return 1;
+ fill = i;
nofill = 0;
return 0;
}
--- /dev/null
+++ b/color.c
@@ -1,0 +1,54 @@
+#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 < ncolors; 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++;
+ }
+ return c;
+}
+
+Image*
+grayscale(int n)
+{
+ return color(n, n, n);
+}
+
--- a/mkfile
+++ b/mkfile
@@ -4,7 +4,7 @@
TARG=slug
LIB=lua/liblua.a.$O
HFILES=a.h
-OFILES=slug.$O api.$O
+OFILES=slug.$O api.$O color.$O
</sys/src/cmd/mkone
--- a/slug.c
+++ b/slug.c
@@ -1,5 +1,4 @@
#include "a.h"
-#include "colors.h"
Mousectl *mc;
Keyboardctl *kc;
@@ -23,18 +22,6 @@
return;
lua_call(L, 0, 0);
drawcanvas();
-}
-
-Image*
-getcolor(int n)
-{
- if(n < 0)
- n = 0;
- if(n >= nelem(palette))
- n = nelem(palette) - 1;
- if(palette[n].i == nil)
- palette[n].i = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, palette[n].color);
- return palette[n].i;
}
void