diff --git a/colors.lua b/colors.lua index 0faeb43..9405bc3 100644 --- a/colors.lua +++ b/colors.lua @@ -7,6 +7,9 @@ LICENSE is BEER-WARE. ]] +local GAMMA = 2.2 +local GAMMA_INV = 1/GAMMA + local colors = {} -------------------------------------------------------------------------------- @@ -83,13 +86,14 @@ end -- Interpolation colors.interpolate_rgb = function (c1, c2, t) - assert(type(c1) == 'table' and type(c1[1]) == 'number' and type(c1[2]) == 'number' and type(c1[3]) == 'number' and (type(c1[4]) == 'number' or c1[4] == nil)) - assert(type(c2) == 'table' and type(c2[1]) == 'number' and type(c2[2]) == 'number' and type(c2[3]) == 'number' and (type(c2[4]) == 'number' or c2[4] == nil)) - assert(type(t) == 'number') - -- - local alpha = nil - if c1[4] or c2[4] then alpha = (c1[4] or 255) + ((c2[4] or 255)-(c1[4] or 255))*t end - return { c1[1]+(c2[1]-c1[1])*t, c1[2]+(c2[2]-c1[2])*t, c1[3]+(c2[3]-c1[3])*t, alpha } + return { c1[1]+(c2[1]-c1[1])*t, c1[2]+(c2[2]-c1[2])*t, c1[3]+(c2[3]-c1[3])*t } +end + +colors.interpolate_rgb_gamma_adjusted = function (c1, c2, t) + local c1_r, c1_g, c1_b = c1[1]^GAMMA, c1[2]^GAMMA, c1[3]^GAMMA + local c2_r, c2_g, c2_b = c2[1]^GAMMA, c2[2]^GAMMA, c2[3]^GAMMA + local c3_r, c3_g, c3_b = c1_r+(c2_r-c1_r)*t, c1_g+(c2_g-c1_g)*t, c1_b+(c2_b-c1_b)*t + return c3_r^GAMMA_INV, c3_g^GAMMA_INV, c3_b^GAMMA_INV end local function angle_delta (a1, a2) @@ -151,3 +155,4 @@ end -------------------------------------------------------------------------------- return colors +