-- dye/init.lua

dye = {}

-- Make dye names and descriptions available globally

dye.dyes = {
   {'grey',       'Grey',       '#808080'},
   {'dark_grey',  'Dark Grey',  '#404040'},
   {'black',      'Black',      '#1a1a1a'},
   {'violet',     'Violet',     '#480680'},
   {'blue',       'Blue',       '#00519d'},
   {'cyan',       'Cyan',       '#00959d'},
   {'dark_green', 'Dark Green', '#004d00'},
   {'green',      'Green',      '#008000'},
   {'yellow',     'Yellow',     '#ffff00'},
   {'brown',      'Brown',      '#40230f'},
   {'orange',     'Orange',     '#ff5600'},
   {'red',        'Red',        '#c91818'},
   {'magenta',    'Magenta',    '#d80481'},
   {'pink',       'Pink',       '#ff7dc8'},
   {'white',      'White',      '#ffffff'},
}

-- Define items

for _, row in ipairs(dye.dyes) do
   local name = row[1]
   local description = row[2]
   local hex = row[3]
   local groups = {dye = 1}
   groups['color_' .. name] = 1

   minetest.register_craftitem('dye:' .. name, {
   inventory_image = '(dye.png^[multiply:'..hex..')^dye_mask.png',
   description = description .. ' Dye',
   groups = groups
   })

   minetest.register_craft({
   output = 'dye:' .. name .. ' 4',
   recipe = {
   {'group:flower,color_' .. name}
   },
   })
end

-- Manually add coal -> black dye

minetest.register_craft({
   output = 'dye:black 4',
   recipe = {
   {'group:coal'}
   },
})

-- Manually add blueberries->violet dye

minetest.register_craft({
   output = 'dye:violet 2',
   recipe = {
   {'default:blueberries'}
   },
})

-- Mix recipes

local dye_recipes = {
   -- src1, src2, dst
   -- RYB mixes
   {'red', 'blue', 'violet'}, -- 'purple'
   {'yellow', 'red', 'orange'},
   {'yellow', 'blue', 'green'},
   -- RYB complementary mixes
   {'yellow', 'violet', 'dark_grey'},
   {'blue', 'orange', 'dark_grey'},
   -- CMY mixes - approximation
   {'cyan', 'yellow', 'green'},
   {'cyan', 'magenta', 'blue'},
   {'yellow', 'magenta', 'red'},
   -- other mixes that result in a color we have
   {'red', 'green', 'brown'},
   {'magenta', 'blue', 'violet'},
   {'green', 'blue', 'cyan'},
   {'pink', 'violet', 'magenta'},
   -- mixes with black
   {'white', 'black', 'grey'},
   {'grey', 'black', 'dark_grey'},
   {'green', 'black', 'dark_green'},
   {'orange', 'black', 'brown'},
   -- mixes with white
   {'white', 'red', 'pink'},
   {'white', 'dark_grey', 'grey'},
   {'white', 'dark_green', 'green'},
}

for _, mix in pairs(dye_recipes) do
   minetest.register_craft({
   type = 'shapeless',
   output = 'dye:' .. mix[3] .. ' 2',
   recipe = {'dye:' .. mix[1], 'dye:' .. mix[2]},
   })
end
