author | Francesco Abbate <francesco.bbt@gmail.com> | 2011年11月06日 18:04:21 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2011年11月06日 18:04:21 +0100 |
commit | 65ceb0cae23f9ade2f5d7b51e94286e10b525852 (patch) | |
tree | 104af7f29cefac8c460e2d3df34c13f20eb2a85d /graph-init.lua | |
parent | 394be1a7f6b20470ff89ef1c2988146fcac6957c (diff) | |
download | gsl-shell-65ceb0cae23f9ade2f5d7b51e94286e10b525852.tar.gz |
-rw-r--r-- | graph-init.lua | 54 |
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 = { |