shithub: slug

Download patch

ref: fdf4c12f78354d0886bc1ff29f7605bb671f4a91
parent: 20462b4b4afbc56235201903221a1fb15f14d605
author: phil9 <telephil9@gmail.com>
date: Mon Dec 5 14:38:23 EST 2022

implement lerpColor()

--- a/api.c
+++ b/api.c
@@ -143,6 +143,32 @@
 	return 0;
 }
 
+void
+checkcolor(lua_State *L, int index, int *r, int *g, int *b)
+{
+	lua_pushstring(L, "r");
+	lua_gettable(L, index);
+	*r = luaL_checkinteger(L, -1);
+	lua_pushstring(L, "g");
+	lua_gettable(L, index);
+	*g = luaL_checkinteger(L, -1);
+	lua_pushstring(L, "b");
+	lua_gettable(L, index);
+	*b = luaL_checkinteger(L, -1);
+}
+
+void
+pushcolor(lua_State *L, int r, int g, int b)
+{
+	lua_newtable(L);
+	lua_pushinteger(L, r);
+	lua_setfield(L, -2, "r");
+	lua_pushinteger(L, g);
+	lua_setfield(L, -2, "g");
+	lua_pushinteger(L, b);
+	lua_setfield(L, -2, "b");
+}
+
 Image*
 getcolor(lua_State *L)
 {
@@ -151,21 +177,10 @@
 
 	c = lua_gettop(L);
 	if(c == 1){
-		if(lua_istable(L, 1)){
-			lua_pushstring(L, "r");
-			lua_gettable(L, 1);
-			r = luaL_checkinteger(L, -1);
-			lua_pushstring(L, "g");
-			lua_gettable(L, 1);
-			g = luaL_checkinteger(L, -1);
-			lua_pushstring(L, "b");
-			lua_gettable(L, 1);
-			b = luaL_checkinteger(L, -1);
-		}else{
-			r = luaL_checkinteger(L, 1);
-			g = r;
-			b = r;
-		}
+		if(lua_istable(L, 1))
+			checkcolor(L, 1, &r, &g, &b);
+		else
+			r = g = b = luaL_checkinteger(L, 1);
 		i = color(r, g, b, 0);
 	}else if(c == 3){
 		r = luaL_checkinteger(L, 1);
@@ -571,17 +586,30 @@
 	b = luaL_checkinteger(L, 3);
 	if(colormode == Chsv)
 		hsvtorgb(r, g, b, &r, &g, &b);
-	lua_newtable(L);
-	lua_pushinteger(L, r);
-	lua_setfield(L, -2, "r");
-	lua_pushinteger(L, g);
-	lua_setfield(L, -2, "g");
-	lua_pushinteger(L, b);
-	lua_setfield(L, -2, "b");
+	pushcolor(L, r, g, b);
 	return 1;
 }
 
+int
+clerpcolor(lua_State *L)
+{
+	int r0, g0, b0, r1, g1, b1, r, g, b;
+	double f;
 
+	checkcolor(L, 1, &r0, &g0, &b0);
+	checkcolor(L, 2, &r1, &g1, &b1);
+	f = luaL_checknumber(L, 3);
+	if(f < 0.0)
+		f = 0.0;
+	else if(f > 1.0)
+		f = 1.0;
+	r = f * (r1 - r0) + r0;
+	g = f * (g1 - g0) + g0;
+	b = f * (b1 - b0) + b0;
+	pushcolor(L, r, g, b);
+	return 1;
+}
+
 int
 cloadpixels(lua_State *L)
 {
@@ -678,6 +706,7 @@
 	registerfunc(L, "push", cpush);
 	registerfunc(L, "pop", cpop);
 	registerfunc(L, "color", ccolor);
+	registerfunc(L, "lerpColor", clerpcolor);
 	registerfunc(L, "loadPixels", cloadpixels);
 	registerfunc(L, "updatePixels", cupdatepixels);
 	registerfunc(L, "randomGaussian", crandomgaussian);
--- /dev/null
+++ b/samples/lerpcolor.lua
@@ -1,0 +1,23 @@
+#!/bin/slug
+
+function setup()
+	size(400,400)
+	background(51)
+	stroke(255)
+	from = color(204, 102, 0)
+	to = color(0, 102, 153)
+	interA = lerpColor(from, to, .33)
+	interB = lerpColor(from, to, .66)
+	fill(from)
+	rect(40, 80, 80, 240)
+	fill(interA)
+	rect(120, 80, 80, 240)
+	fill(interB)
+	rect(200, 80, 80, 240)
+	fill(to)
+	rect(280, 80, 80, 240)
+end
+
+function draw()
+end
+
--- a/slug.man
+++ b/slug.man
@@ -96,6 +96,17 @@
 \f5color(\f2v1\fP, \f2v2\fP, \f2v3\fP)
 Creates a color object which is an lua table with r, g, and b fields.
 The parameters are affected by the current color mode, meaning they will be interpreted as either RGB or HSB.
+.TP
+\f5lerpColor(\f2c1\fP, \f2c2\fP, \f2amt\fP)
+Compute a
+.I color
+by interpolating colors
+.I c1
+and
+.I c2
+linearly by 
+.I amt
+\&.
 .SS Image
 .TP
 \f5pixels[]