local intramodcom = dwarf_characters.intramodcom





--turns red, green and blue number value into a multiply tag with
--rgb as a ColorString.
local function get_color_mod(r, g, b)
	local hex = string.format("%.2X%.2X%.2X", r, g, b)
	return "^[multiply:#" .. hex
end




local function get_skin_color_rgb(brightness)
	local red = 255 * brightness
	local green = red * 0.86
	local blue = green * 0.83
	return red, green, blue
end




local function get_textures_naked(skin_color)
	local skin_color_mod = get_color_mod(get_skin_color_rgb(skin_color))
	--local skin = "(dwarf_characters_skin.png"..skin_color_mod .. ")"
	local blank = "dwarf_characters_blank.png"

	local skin = {}
	for i = 1, 15
	do
		skin[i] = blank
	end

	dwarf_characters.apply_skin(skin, "normal", skin_color_mod)
	return skin
end


local function get_hair_color_rgb(brightness, redness)
	local r = 255
	local g = 255 - redness * 55
	local b = 83

	local a1 = g / r
	r = r * brightness
	local a2 = b / g
	g = g * a1 * brightness
	b = b * a2 * brightness

	return r, g, b
end


local function apply_beard_and_hair(base, appearance)
	local color_mod =
		get_color_mod(get_hair_color_rgb(appearance.hair_brightness,
			appearance.hair_redness))


	local beard = {}
	dwarf_characters.apply_beard(beard, appearance.beard)
	local hair = {}
	dwarf_characters.apply_hair(hair, appearance.hair)

	for k, v in pairs(beard)
	do
		base[k] = hair[k] .. "^" .. v .. color_mod
	end


end

local function get_textures(appearance)
	local textures = get_textures_naked(appearance.skin_color)

	apply_beard_and_hair(textures, appearance)


	--armor and helmet mods can do anything, depending on the
	--processing function passed to the definition.

	dwarf_characters.apply_armor(textures, appearance.armor, appearance.armor_mod)

	dwarf_characters.apply_helmet(textures, appearance.helmet, appearance.helmet_mod)
	return textures
end


intramodcom.get_textures = get_textures
