1
0
This commit is contained in:
Jon Michael Aanes 2019-06-09 16:29:08 +02:00
commit 37b9141c77
2 changed files with 29 additions and 6 deletions

View File

@ -15,6 +15,10 @@ local colors = {}
--------------------------------------------------------------------------------
-- Conversion
colors.rgb255_to_rgb1 = function (color)
return { color[1]/255, color[2]/255, color[3]/255, (color[4] or 255)/255 }
end
colors.rgb_to_hsl = function (color)
-- Error check
assert(type(color) == 'table' and type(color[1]) == 'number' and type(color[2]) == 'number' and type(color[3]) == 'number')
@ -25,10 +29,9 @@ colors.rgb_to_hsl = function (color)
local b = (color[3] == 255 and 1 or color[3]/256)
local max, min = math.max(r, g, b), math.min(r, g, b)
if min == max then return { 0, 0, (max+min)/2 } end
local h, s, l = (max+min)/2, (max+min)/2, (max+min)/2
if min == max then
return {0, 0, l}
end
local d = max - min
s = l > 0.5 and d / (2 - max - min) or d / (max + min)
@ -82,14 +85,33 @@ colors.hsl_to_rgb = function (color)
return { r, g, b }
end
colors.hsl_to_rgb1 = function (...)
return colors.rgb255_to_rgb1(colors.hsl_to_rgb(...))
end
--------------------------------------------------------------------------------
-- Interpolation
colors.interpolate_rgb = function (c1, c2, t)
return { c1[1]+(c2[1]-c1[1])*t, c1[2]+(c2[2]-c1[2])*t, c1[3]+(c2[3]-c1[3])*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 }
end
colors.interpolate_rgb_gamma_adjusted = function (c1, c2, t)
colors.interpolate_rgb_gamma_adjusted = function (c1, c2, t, gamma)
local gamma_inv = nil
if not gamma then
gamma, gamma_inv = GAMMA, GAMMA_INV
else
assert(type(gamma) == 'number' and gamma == gamma and gamma ~= 0)
gamma_inv = 1 / gamma
end
----
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

View File

@ -1 +1,2 @@
return require 'colors.colors'
return require ((...) .. '.colors')