require('strict')

local p = {}

--[=[
Input list of colors and weights
	{{color = {r, g, b},	weight = n}, ...}
Output color
	{r, g, b}
Also supports RGBA
]=]
function p.weighted_color_blend(args)
	local total_weight = 0
	local max_len = 0
	for k, v in pairs(args) do
		total_weight = total_weight + v.weight
		
		if max_len < #(v['color']) then
			max_len = #(v['color'])
		end
	end
	if total_weight == 0 then
		total_weight = 1
	end
	max_len = math.max(max_len, 3)
	
	local blend = {}
	for i = 1, max_len do
		blend[i] = 0
		for k, v in pairs(args) do
			blend[i] = blend[i] + v['color'][i] * v['weight']/total_weight
		end
	end
	
	return blend
end

p.colorData = {
	-- base colors
	[1] = {
		name = 'red',
		rgb = {226, 81, 52}
	},
	[2] = {
		name = 'yellow',
		rgb = {243, 205, 70}
	},
	[3] = {
		name = 'blue',
		rgb = {60, 124, 160}
	},
	[4] = {
		name = 'orange',
		rgb = {254, 166, 58}
	},
	[5] = {
		name = 'green',
		rgb = {123, 180, 135}
	},
	[6] = {
		name = 'purple',
		rgb = {183,97, 142}
	},
	[7] = {
		name = 'deep blue',
		rgb = {15, 87, 127}
	},
	[8] = {
		name = 'rose lake',
		rgb = {231, 86, 115}
	},
	[9] = {
		name = 'lemon yellow',
		rgb = {236, 214, 104}
	},
	[10] = {
		name = 'vermilion',
		rgb = {252, 120, 90}
	},
	[11] = {
		name = 'gray',
		rgb = {142, 136, 112}
	},
	[12] = {
		name = 'black',
		rgb = {0, 0, 0}
	},
	['w'] = {
		name = 'white',
		rgb = {235, 238, 238} -- work says the white used was zinc white - Approximated here --
	}
}

local derived_colors = {
    [13]    = {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][2]['rgb'],	weight = 3}},
	[14]	= {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][2]['rgb'],	weight = 15}},
	[15]	= {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][2]['rgb'],	weight = 3}},
	[16]	= {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][3]['rgb'],	weight = 1}},
	[17]	= {{color = p['colorData'][1]['rgb'],	weight = 5},	{color = p['colorData'][3]['rgb'],	weight = 1}},
	[18]	= {{color = p['colorData'][1]['rgb'],	weight = 15},	{color = p['colorData'][3]['rgb'],	weight = 1}},
	[19]	= {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][4]['rgb'],	weight = 1}},
	[20]	= {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][3]['rgb'],	weight = 5}},
	[24]	= {{color = p['colorData'][1]['rgb'],	weight = 1},	{color = p['colorData'][7]['rgb'],	weight = 1}},
	[38]	= {{color = p['colorData'][2]['rgb'],	weight = 1},	{color = p['colorData'][3]['rgb'],	weight = 2}},
	[40]	= {{color = p['colorData'][2]['rgb'],	weight = 1},	{color = p['colorData'][5]['rgb'],	weight = 1}},
	[45]	= {{color = p['colorData'][2]['rgb'],	weight = 1},	{color = p['colorData'][7]['rgb'],	weight = 1}},
	[46]	= {{color = p['colorData'][2]['rgb'],	weight = 30},	{color = p['colorData'][7]['rgb'],	weight = 1}},
	[47]	= {{color = p['colorData'][2]['rgb'],	weight = 100},	{color = p['colorData'][7]['rgb'],	weight = 1}},
	[48]	= {{color = p['colorData'][2]['rgb'],	weight = 1},	{color = p['colorData'][10]['rgb'],	weight = 1}},
	[51]	= {{color = p['colorData'][2]['rgb'],	weight = 3},	{color = p['colorData'][12]['rgb'],	weight = 1}},
	[55]	= {{color = p['colorData'][3]['rgb'],	weight = 1},	{color = p['colorData'][4]['rgb'],	weight = 3}},
	[59]	= {{color = p['colorData'][3]['rgb'],	weight = 1},	{color = p['colorData'][6]['rgb'],	weight = 2}},
	[65]	= {{color = p['colorData'][3]['rgb'],	weight = 1},	{color = p['colorData'][10]['rgb'],	weight = 1}},
	[68]	= {{color = p['colorData'][3]['rgb'],	weight = 1},	{color = p['colorData'][11]['rgb'],	weight = 3}},
	[71]	= {{color = p['colorData'][4]['rgb'],	weight = 2},	{color = p['colorData'][5]['rgb'],	weight = 1}},
	[75]	= {{color = p['colorData'][4]['rgb'],	weight = 5},	{color = p['colorData'][7]['rgb'],	weight = 2}},
	[76]	= {{color = p['colorData'][4]['rgb'],	weight = 1},	{color = p['colorData'][8]['rgb'],	weight = 1}},
	[83]	= {{color = p['colorData'][5]['rgb'],	weight = 3},	{color = p['colorData'][7]['rgb'],	weight = 1}},
	[84]	= {{color = p['colorData'][5]['rgb'],	weight = 1},	{color = p['colorData'][8]['rgb'],	weight = 1}},
	[85]	= {{color = p['colorData'][5]['rgb'],	weight = 1},	{color = p['colorData'][9]['rgb'],	weight = 3}},
	[88]	= {{color = p['colorData'][5]['rgb'],	weight = 1},	{color = p['colorData'][10]['rgb'],	weight = 1}},
	[89]	= {{color = p['colorData'][5]['rgb'],	weight = 1},	{color = p['colorData'][10]['rgb'],	weight = 3}},
	[93]	= {{color = p['colorData'][6]['rgb'],	weight = 3},	{color = p['colorData'][7]['rgb'],	weight = 1}},
	[94]	= {{color = p['colorData'][6]['rgb'],	weight = 1},	{color = p['colorData'][8]['rgb'],	weight = 1}},
	[101]	= {{color = p['colorData'][7]['rgb'],	weight = 1},	{color = p['colorData'][9]['rgb'],	weight = 1}},
	[105]	= {{color = p['colorData'][7]['rgb'],	weight = 1},	{color = p['colorData'][10]['rgb'],	weight = 3}},
	[110]	= {{color = p['colorData'][7]['rgb'],	weight = 5},	{color = p['colorData'][12]['rgb'],	weight = 1}},
	[118]	= {{color = p['colorData'][9]['rgb'],	weight = 2},	{color = p['colorData'][12]['rgb'],	weight = 1}},
	[122]	= {{color = p['colorData'][10]['rgb'],	weight = 1},	{color = p['colorData'][12]['rgb'],	weight = 1}},
	[149]	= {{color = p['colorData'][10]['rgb'],	weight = 1},	{color = p['colorData'][12]['rgb'],	weight = 40}},
}

for color_key, color_components in pairs(derived_colors) do
	p['colorData'][color_key] = {rgb = p.weighted_color_blend(color_components)}
end

return p