From: Rachael Date: Sun, 30 Apr 2017 21:32:54 +0000 (-0700) Subject: tries to implement rgb --> hsl -> rgb, but fails compiling for an index out of range... X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=aecaf870aa145bf004bba250b1607106bbe4b057;p=acmetensortoys-esp-lua_lamp tries to implement rgb --> hsl -> rgb, but fails compiling for an index out of range exception --- diff --git a/lamp-touch.lua b/lamp-touch.lua index fa0c0f2..128403a 100644 --- a/lamp-touch.lua +++ b/lamp-touch.lua @@ -10,6 +10,7 @@ local touch_db_blackout = nil local touch_db_fn = nil local touchfns = { } local touchfnix = 1 +local dimfactor = 1 if touchcolor == nil then touchcolor = 40 end if touchlastfn == nil then touchlastfn = "fill" end @@ -51,6 +52,11 @@ local function setblackout(nb) end local function toggleblackout() setblackout(not isblackout) end +local function dimdisplay() + dimfactor = dimfactor + 10 + dimfactor = dimfactor % 100 +end + local function touchcolorvec(c) local r, g, b local cm = c % 16 @@ -58,6 +64,71 @@ local function touchcolorvec(c) elseif c < 32 then r = 0; g = 15 - cm; b = cm else r = cm; g = 0; b = 15 - cm end + if (dimfactor > 0) then + print("before green is ", g) + print("before blue is ", b) + local h = 0 + local s = 0 + local l = 0 + local maxcolor = math.max(r,g,b) + local mincolor = math.min(r,g,b) + local delta = maxcolor - mincolor + l = math.floor((maxcolor + mincolor)/2) + + if (maxcolor == r) then + h = (math.floor(((g)-(b))/delta) % 6) + else if (maxcolor == g) then + h = (math.floor(((b)- (r))/delta) + 2) + else if (maxcolor == b) then + h = (math.floor(((r)- (g))/delta) + 4) + end + if (delta == 0) then + h = 0 + s = 0 + else + s = math.floor((maxcolor - mincolor)/(1-math.abs(2*l -1))) + end + l = math.floor(l/dimfactor) + + local c, x, m + c = (1- math.abs(2*l - 1)) * s + x = c * (1-math.abs((h) % 2 - 1)) + m = l-math.floor(c/2) + + local rprime, gprime, bprime + + if ( h < 1) then + rprime = c + gprime = x + bprime = 0 + else if (h < 2) then + rprime = x + gprime = c + bprime = 0 + else if (h < 3) then + rprime = 0 + gprime = c + bprime = x + else if (h < 4) then + rprime = 0 + gprime = x + bprime = c + else if (h < 5) then + rprime = x + gprime = 0 + bprime = c + else if (h < 6) then + rprime = c + gprime = 0 + bprime = x + end + + g = math.abs(math.floor((gprime + m))) + b = math.abs(math.floor((bprime + m))) + r = math.abs(math.floor((rprime + m))) + print("green is ", g) + print("blue is ", b) + end return g,r,b end @@ -138,7 +209,15 @@ local function ontouch() end -- XXX front left: no function assigned, maybe device select or something? - -- if bit.isset(down,4) then end + if bit.isset(down,4) then + dimdisplay() + claimfb() + end + + if bit.isset(down,5) then + -- I could have sworn I had already started this :\ + + end -- draw if we've claimed it! if ledfb == touchfb then