diff --git a/init.lua b/init.lua index 7fe60d8..6639407 100644 --- a/init.lua +++ b/init.lua @@ -77,9 +77,33 @@ end palette_swap.get_palette_from_image_data = get_palette_of_image_data -function palette_swap.apply_color_map_to_image_data (img_data, map_fn) +local function concat_functions (first_fn, ...) + -- + local funcs = {first_fn, ...} + + -- Identity function. + if #funcs == 0 then + return function (...) return ... end + + -- Only one function + elseif #funcs <= 1 then + return first_fn + end + + -- Generic + return function (...) + local val = {...} + for i = 1, #funcs do + val = {funcs[i](unpack(val))} + end + return unpack(val) + end +end + +function palette_swap.apply_color_map_to_image_data (img_data, ...) assert(img_data) - assert(type(map_fn) == 'function') + -- + local map_fn = concat_functions(...) -- return apply_change_palette(img_data, change_palette_from_map(map_fn, get_palette_of_image_data(img_data))) end @@ -106,26 +130,43 @@ function palette_swap.color_map.switch_channel (mode) return loadstring (func_s)() end -function palette_swap.color_map.rotate_hue (rad) +local function load_colors () -- Only need to import colors when needed local colors = require 'colors' assert(type(colors) == 'table') assert(type(colors.rgb1_to_rgb255) == 'function') - assert(type(colors.rgb_to_hsl) == 'function') - assert(type(colors.hsl_to_rgb1) == 'function') + assert(type(colors.rgb_to_hsl) == 'function') + assert(type(colors.hsl_to_rgb1) == 'function') - -- Create function - return function (r, g, b) - local hsl = colors.rgb_to_hsl(colors.rgb1_to_rgb255(r, g, b)) - -- - hsl[1] = (hsl[1] + rad) % 1 - -- - local rgb1 = colors.hsl_to_rgb1(hsl) - return unpack(rgb1) + function palette_swap.color_map.increase_saturation (amount) + -- Create function + return function (r, g, b) + local hsl = colors.rgb_to_hsl(colors.rgb1_to_rgb255(r, g, b)) + -- + hsl[2] = math.min(1, math.max(0, hsl[2] + amount)) + -- + return unpack(colors.hsl_to_rgb1(hsl)) + end + end + + function palette_swap.color_map.rotate_hue (rad) + return function (r, g, b) + local hsl = colors.rgb_to_hsl(colors.rgb1_to_rgb255(r, g, b)) + -- + hsl[1] = (hsl[1] + rad) % 1 + -- + return unpack(colors.hsl_to_rgb1(hsl)) + end end end +for _, key in ipairs {'rotate_hue', 'increase_saturation'} do + palette_swap.color_map[key] = function (...) + load_colors() + return palette_swap.color_map[key](...) + end +end -------------------------------------------------------------------------------- -- Return