-rw-r--r-- | agg-plot/colors.cpp | 26 | ||||
-rw-r--r-- | agg-plot/colors.h | 1 | ||||
-rw-r--r-- | agg-plot/lua-draw.cpp | 104 | ||||
-rw-r--r-- | agg-plot/lua-draw.h | 3 | ||||
-rw-r--r-- | graph-init.lua | 54 | ||||
-rw-r--r-- | pre3d/pre3d.lua | 2 |
diff --git a/agg-plot/colors.cpp b/agg-plot/colors.cpp index 39d93828..f6b414a1 100644 --- a/agg-plot/colors.cpp +++ b/agg-plot/colors.cpp @@ -4,7 +4,7 @@ #include "lua-cpp-utils.h" #include "colors.h" -agg::rgba8 +static agg::rgba8 rgba8_lookup (lua_State *L, const char *color_str) { const char *p = color_str; @@ -49,17 +49,25 @@ agg::rgba8 color_arg_lookup (lua_State *L, int index) { if (lua_isnoneornil (L, index)) + return colors::cdefault; + + if (lua_isnumber(L, index)) { - return colors::cdefault; - } - else if (lua_isstring (L, index)) - { - const char *cstr = lua_tostring (L, index); - return rgba8_lookup (L, cstr); + unsigned int col = (unsigned int) lua_tointeger (L, index); + agg::int8u r = (col & 0xff000000) >> 24; + agg::int8u g = (col & 0x00ff0000) >> 16; + agg::int8u b = (col & 0x0000ff00) >> 8; + agg::int8u a = (col & 0x000000ff); + + return agg::rgba8(r, g, b, a); } - agg::rgba8 *pc = object_check<agg::rgba8> (L, index, GS_RGBA_COLOR); - return *pc; + const char *cstr = lua_tostring (L, index); + + if (!cstr) + luaL_error (L, "invalid color specification"); + + return rgba8_lookup (L, cstr); } agg::rgba colors::white(1, 1, 1); diff --git a/agg-plot/colors.h b/agg-plot/colors.h index 16016c71..ceba512e 100644 --- a/agg-plot/colors.h +++ b/agg-plot/colors.h @@ -8,7 +8,6 @@ extern "C" { #include "defs.h" #include "agg_color_rgba.h" -extern agg::rgba8 rga8_lookup (lua_State *L, const char *color_str); extern agg::rgba8 color_arg_lookup (lua_State *L, int index); namespace colors { diff --git a/agg-plot/lua-draw.cpp b/agg-plot/lua-draw.cpp index 4b9d83ff..d533e745 100644 --- a/agg-plot/lua-draw.cpp +++ b/agg-plot/lua-draw.cpp @@ -62,11 +62,6 @@ static int agg_ellipse_new (lua_State *L); static int agg_circle_new (lua_State *L); static int agg_ellipse_free (lua_State *L); - -static int agg_rgba_add (lua_State *L); -static int agg_rgba_mul (lua_State *L); -static int agg_rgba_set_alpha (lua_State *L); - static void path_cmd (draw::path *p, int cmd, struct cmd_call_stack *stack); static struct path_cmd_reg cmd_table[] = { @@ -83,8 +78,6 @@ static const struct luaL_Reg draw_functions[] = { {"path", agg_path_new}, {"ellipse", agg_ellipse_new}, {"circle", agg_circle_new}, - {"rgba", agg_rgba_new}, - {"rgb", agg_rgb_new}, {NULL, NULL} }; @@ -100,14 +93,6 @@ static const struct luaL_Reg agg_ellipse_methods[] = { {NULL, NULL} }; -// no finalizer is needed because rgba does not allocate memory -static const struct luaL_Reg rgba_methods[] = { - {"__add", agg_rgba_add }, - {"__mul", agg_rgba_mul }, - {"alpha", agg_rgba_set_alpha }, - {NULL, NULL} -}; - int agg_path_new (lua_State *L) { @@ -267,89 +252,6 @@ agg_ellipse_free (lua_State *L) return object_free<draw::ellipse>(L, 1, GS_DRAW_ELLIPSE); } -static unsigned int double2uint8 (double x) -{ - int u = x * 255.0; - if (u > 255) - u = 255; - else if (u < 0) - u = 0; - return (unsigned int) u; -} - -agg::rgba8 * -check_agg_rgba8 (lua_State *L, int index) -{ - return (agg::rgba8 *) gs_check_userdata (L, index, GS_RGBA_COLOR); -} - -int -agg_rgba_new (lua_State *L) -{ - unsigned int r = double2uint8 (luaL_checknumber (L, 1)); - unsigned int g = double2uint8 (luaL_checknumber (L, 2)); - unsigned int b = double2uint8 (luaL_checknumber (L, 3)); - unsigned int a = double2uint8 (luaL_checknumber (L, 4)); - - new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b, a); - return 1; -} - -int -agg_rgb_new (lua_State *L) -{ - unsigned int r = double2uint8 (luaL_checknumber (L, 1)); - unsigned int g = double2uint8 (luaL_checknumber (L, 2)); - unsigned int b = double2uint8 (luaL_checknumber (L, 3)); - - new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b, 255); - return 1; -} - -int -agg_rgba_set_alpha (lua_State *L) -{ - agg::rgba8 *c = object_check<agg::rgba8> (L, 1, GS_RGBA_COLOR); - double a = luaL_checknumber (L, 2); - c->a = agg::rgba8::base_mask * a; - return 0; -} - -int -agg_rgba_add (lua_State *L) -{ - agg::rgba8 *c1 = object_check<agg::rgba8> (L, 1, GS_RGBA_COLOR); - agg::rgba8 *c2 = object_check<agg::rgba8> (L, 2, GS_RGBA_COLOR); - - unsigned int r = c1->r + c2->r; - unsigned int g = c1->g + c2->g; - unsigned int b = c1->b + c2->b; - - new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b); - - return 1; -} - -int -agg_rgba_mul (lua_State *L) -{ - int is = 1, ic = 2; - - if (gs_is_userdata (L, 1, GS_RGBA_COLOR)) - { - ic = 1; - is = 2; - } - - double f = luaL_checknumber (L, is); - agg::rgba8 *c = object_check<agg::rgba8> (L, ic, GS_RGBA_COLOR); - - unsigned int r = f * c->r, g = f * c->g, b = f * c->b; - - new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b); - return 1; -} - void draw_register (lua_State *L) { @@ -363,12 +265,6 @@ draw_register (lua_State *L) luaL_register (L, NULL, agg_ellipse_methods); lua_pop (L, 1); - luaL_newmetatable (L, GS_METATABLE(GS_RGBA_COLOR)); - lua_pushvalue (L, -1); - lua_setfield (L, -2, "__index"); - luaL_register (L, NULL, rgba_methods); - lua_pop (L, 1); - /* gsl module registration */ luaL_register (L, NULL, draw_functions); } diff --git a/agg-plot/lua-draw.h b/agg-plot/lua-draw.h index 55509946..ba2cb500 100644 --- a/agg-plot/lua-draw.h +++ b/agg-plot/lua-draw.h @@ -19,11 +19,8 @@ __END_DECLS extern int agg_text_new (lua_State *L); extern int agg_path_new (lua_State *L); -extern int agg_rgb_new (lua_State *L); -extern int agg_rgba_new (lua_State *L); extern draw::path* check_agg_path (lua_State *L, int index); -extern agg::rgba8* check_agg_rgba8 (lua_State *L, int index); #endif diff --git a/graph-init.lua b/graph-init.lua index e0933e99..1d173a6b 100644 --- a/graph-init.lua +++ b/graph-init.lua @@ -1,6 +1,10 @@ +local bit = require 'bit' + local floor = math.floor +local bor, band, lshift, rshift = bit.bor, bit.band, bit.lshift, bit.rshift + local lua_index_style = gslsh.lua_index_style function graph.ipath(f) @@ -30,7 +34,7 @@ function graph.ipathp(f) end function graph.fxline(f, xi, xs, n) - n = n and n or 512 + n = n and math.min(n, 8192) or 512 return graph.ipath(iter.sample(f, xi, xs, n)) end @@ -108,13 +112,55 @@ function graph.rect(x1, y1, x2, y2) return p end +local function rgba(r, g, b, a) + local rb = band(lshift(r*255, 24), 0xff000000) + local gb = band(lshift(g*255, 16), 0xff0000 ) + local bb = band(lshift(b*255, 8 ), 0xff00 ) + return bor(rb, gb, bb, a and band(a*255, 0xff) or 0xff) +end + +local function rgba_decode(col) + local r = rshift(band(col, 0xff000000), 24) / 255 + local g = rshift(band(col, 0xff0000), 16) / 255 + local b = rshift(band(col, 0xff00), 8) / 255 + local a = band(col, 0xff) / 255 + return r, g, b, a +end + +graph.rgba = rgba +graph.rgb = function(r, g, b) return rgba(r, g, b, 1) end + +local lum_std = 0.75 + +graph.color = { + red = rgba(lum_std, 0, 0), + green = rgba(0, lum_std, 0), + blue = rgba(0, 0, lum_std), + magenta = rgba(lum_std, 0, lum_std), + cyan = rgba(0, lum_std, lum_std), + yellow = rgba(lum_std, lum_std, 0), + + black = rgba(0, 0, 0), + white = rgba(1, 1, 1), + + decode = rgba_decode, + + combine = function(f1, c1, f2, c2) + local r1, g1, b1 = rgba_decode(c1) + if f2 and c2 then + local r2, g2, b2 = rgba_decode(c2) + return rgba(f1*r1+f2*r2, f1*g1+f2*g2, f1*b1+f2*b2) + else + return rgba(f1*r1, f1*g1, f1*b1) + end + end +} + local bcolors = {'red', 'blue', 'green', 'magenta', 'cyan', 'yellow'} -local mcolors = {'', 'dark', 'light'} function graph.rainbow(n) local p = #bcolors - local q = floor((n-1)/p) % #mcolors - return mcolors[q+1] .. bcolors[(n-1) % p + 1] + return graph.color[bcolors[(n-1) % p + 1]] end local color_schema = { diff --git a/pre3d/pre3d.lua b/pre3d/pre3d.lua index 014aa923..12faa530 100644 --- a/pre3d/pre3d.lua +++ b/pre3d/pre3d.lua @@ -773,7 +773,7 @@ function RendererMT.bufferShape(this, shape) if this.set_light_intensity then local r1 = 0.2 local ci = intensity * (1-r1) - fill_rgba = r1 * fill_rgba + ci * fill_rgba + fill_rgba = color.combine(r1, fill_rgba, ci, fill_rgba) end if this.fill_rgba_alpha then |