# Scripts for blending images
# Included by a style file in its init script


########################################################################
# Filenames and other defaults
########################################################################

mask_hybrid_with_land	:= { false }
mask_multi_land_with_color	:= { false }
template		:= { input + "{type}.jpg" }
land_template		:= { (if input == "a" then "c" else input) + "l{type}.jpg" }
# For what value should thumbnails of hybrids be made?
hybrid_previews         := "hybrid"
# Are there templates for colored lands, land_template(...)?
colored_lands           := true

########################################################################
# Combining multiple colors (hybrids)
########################################################################

card_hybrid_2 := {
	linear_blend(
		image1: template(colors[0])
		image2: template(colors[1])
		x1: 0.4,  y1: 0
		x2: 0.6,  y2: 0
	)
}

overlay_hybrid := [
	1: { template(colors[0]) }
	2: { combine_blend(
		image1: template(colors[0]),
		image2: template(colors[1]),
		combine: "symmetric overlay"
	   )}
]

# vertical version of a horizontal hybrid
vertical_card_hybrid := {
	do_linear_blend := linear_blend
	linear_blend := { do_linear_blend(x1: 0, x2: 0, y1: x1, y2: x2) }
	card_hybrid.horizontal[color_count]()
}

horizontal_card_hybrid := [
	1: { template(colors[0]) }
	2: card_hybrid_2
	3: {linear_blend(
		image1: template(colors[0])
		x1: 0.22,  y1: 0
		x2: 0.4,   y2: 0
		image2: linear_blend(
		  image1: template(colors[1])
		  x1: 0.6,   y1: 0
		  x2: 0.78,  y2: 0
		  image2: template(colors[2])
	   ))}
	4: {linear_blend(
		# blend = 0.16
		image1: template(colors[0])
		x1: 0.15,  y1: 0
		x2: 0.31,  y2: 0
		image2: linear_blend(
		  image1: template(colors[1])
		  x1: 0.42,  y1: 0
		  x2: 0.58,  y2: 0
		  image2: linear_blend(
		    image1: template(colors[2])
		    x1: 0.69, y1: 0
		    x2: 0.85, y2: 0
		    image2: template(colors[3])
	   )))}
	5: {linear_blend(
		image1: template(colors[0])
		x1: 2.0 / 15,  y1: 0
		x2: 4.0 / 15,  y2: 0
		image2: linear_blend(
		  image1: template(colors[1])
		  x1: 5.0 / 15,  y1: 0
		  x2: 7.0 / 15,  y2: 0
		  image2: linear_blend(
		    image1: template(colors[2])
		    x1:  8.0 / 15,  y1: 0
		    x2: 10.0 / 15,  y2: 0
		    image2: linear_blend(
		      image1: template(colors[3])
		      x1: 11.0 / 15,  y1: 0
		      x2: 13.0 / 15,  y2: 0
		      image2: template(colors[4])
	   ))))}
	6: {linear_blend(
		image1: template(colors[0])
		x1: 1.5 / 15,  y1: 0
		x2: 3.1 / 15,  y2: 0
		image2: linear_blend(
		  image1: template(colors[1])
		  x1: 4.1 / 15,  y1: 0
		  x2: 5.7 / 15,  y2: 0
		  image2: linear_blend(
		    image1: template(colors[2])
		    x1: 6.7 / 15,  y1: 0
		    x2: 8.3 / 15,  y2: 0
		    image2: linear_blend(
		      image1: template(colors[3])
		      x1: 9.3 / 15,  y1: 0
		      x2: 10.9 / 15,  y2: 0
		      image2: linear_blend(
		        image1: template(colors[4])
		        x1: 11.9 / 15, y1: 0
		        x2: 13.5 / 15, y2: 0
		        image2: template(colors[5])
	   )))))}
	7: {linear_blend(
		image1: template(colors[0])
		x1: 1.3 / 15,  y1: 0
		x2: 2.7 / 15,  y2: 0
		image2: linear_blend(
		  image1: template(colors[1])
		  x1: 3.5 / 15,  y1: 0
		  x2: 4.9 / 15,  y2: 0
		  image2: linear_blend(
		    image1: template(colors[2])
		    x1: 5.7 / 15,  y1: 0
		    x2: 7.1 / 15,  y2: 0
		    image2: linear_blend(
		      image1: template(colors[3])
		      x1: 7.9 / 15,  y1: 0
		      x2: 9.3 / 15,  y2: 0
		      image2: linear_blend(
		        image1: template(colors[4])
		        x1: 10.1 / 15, y1: 0
		        x2: 11.5 / 15, y2: 0
		        image2: linear_blend(
		          image1: template(colors[5])
		          x1: 12.3 / 15, y1: 0
		          x2: 13.7 / 15, y2: 0
		          image2: template(colors[6])
	   ))))))}
]

card_hybrid := [
	radial: [
		0: { template("c") }
		1: { template(colors[0]) }
		2: card_hybrid_2
		3: {linear_blend(
			image1: card_hybrid_2(colors: colors[0] + colors[1]),
			image2: template(colors[2]),
			x1: 0,  y1: 0.55
			x2: 0,  y2: 0.77
		   )}
		4: {linear_blend(
			image1: card_hybrid_2(colors: colors[0] + colors[1])
			image2: card_hybrid_2(colors: colors[3] + colors[2])
			x1: 0,  y1: 0.4
			x2: 0,  y2: 0.62
		   )}
		5: {linear_blend(
			image1: linear_blend(
				image1: card_hybrid_2(colors: colors[0] + colors[1]),
				image2: card_hybrid_2(colors: colors[4] + colors[2]),
				x1: 0,  y1: 0.19
				x2: 0,  y2: 0.35
			),
			image2: template(colors[3]),
			x1: 0,  y1: 0.777
			x2: 0,  y2: 0.937
		   )}
		6: {linear_blend(
			image1: linear_blend(
				image1: card_hybrid_2(colors: colors[0] + colors[1]),
				image2: card_hybrid_2(colors: colors[5] + colors[2]),
				x1: 0,  y1: 0.19
				x2: 0,  y2: 0.35
			),
			image2: card_hybrid_2(colors: colors[4] + colors[3]),
			x1: 0,  y1: 0.777
			x2: 0,  y2: 0.937
		   )}
		7: {linear_blend(
			image1: linear_blend(
				image1: linear_blend(
					image1: card_hybrid_2(colors: colors[0] + colors[1]),
					image2: card_hybrid_2(colors: colors[6] + colors[3]),
					x1: 0,  y1: 0.34
					x2: 0,  y2: 0.50
				),
				image2: card_hybrid_2(colors: colors[5] + colors[3]),
				x1: 0,  y1: 0.877
				x2: 0,  y2: 0.937
			),
			image2: template(colors[4]),
			x1: 0,  y1: 1.140
			x2: 0,  y2: 1.300
		   )}
	]
	horizontal: horizontal_card_hybrid
	vertical: [
		1: { template(colors[0]) }
		2: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[1])
			x1: 0,  y1: 0.4
			x2: 0,  y2: 0.6
		   )}
		3: vertical_card_hybrid
		4: vertical_card_hybrid
		5: vertical_card_hybrid
		6: vertical_card_hybrid
		7: vertical_card_hybrid
	]
	overlay: overlay_hybrid
]


########################################################################
# P/T box
########################################################################
# These are easier

horizontal_pt_hybrid := [
	1: { template(colors[0]) }
	2: { template(colors[1]) }
	3: { linear_blend(
		image1: template(colors[1])
		image2: template(colors[2])
		x1: -0.51, y1: 0
		x2:  0.26, y2: 0
	   )}
	4: { linear_blend(
		image1: template(colors[2])
		image2: template(colors[3])
		x1: -0.1,  y1: 0
		x2:  0.6,  y2: 0
	   )}
	5: { linear_blend(
		image1: template(colors[3])
		image2: template(colors[4])
		x1: 0.08,  y1: 0
		x2: 0.65,  y2: 0
	   )}
	6: { linear_blend(
		image1: template(colors[4])
		image2: template(colors[5])
		x1: 0.07, y1: 0
		x2: 0.7, y2: 0
	   )}
	7: {linear_blend(
		image1: linear_blend(
			image1: template(colors[4]),
			image2: template(colors[5]),
			x1: -0.2,  y1: 0
			x2: 0.2,  y2: 0
		),
		image2: template(colors[6]),
		x1: 0.5,  y1: 0
		x2: 0.7,  y2: 0
	   )}
]

pt_hybrid := [
	radial: [
		0: { template("c")       }
		1: { template(colors[0]) }
		2: { template(colors[1]) }
		3: { template(colors[2]) }
		4: { template(colors[2]) }
		5: { linear_blend(
			image1: template(colors[2])
			image2: template(colors[3])
			x1: 0, y1: -1.5
			x2: 0, y2: 0.7
		   )}
		6: { linear_blend(
			image1: template(colors[2])
			image2: template(colors[3])
			x1: 0, y1: -1.5
			x2: 0, y2: 0.7
		   )}
		7: { template(colors[4]) }
	]
	horizontal: horizontal_pt_hybrid
	vertical: [
		1: { template(colors[0]) }
		2: { template(colors[1]) }
		3: { template(colors[2]) }
		4: { template(colors[3]) }
		5: { template(colors[4]) }
		6: { template(colors[5]) }
		7: { template(colors[6]) }
	]
	overlay: overlay_hybrid
]

########################################################################
# P/T boxes of flip cards
########################################################################

flip_pt_hybrid := [
	radial: [
		0: { template("c")       }
		1: { template(colors[0]) }
		2: { template(colors[1]) }
		3: { template(colors[1]) }
		4: { template(colors[1]) }
		5: { linear_blend(
			image1: template(colors[1])
			image2: template(colors[2])
			x1: 0, y1: -1
			x2: 0, y2: 1.8
		   )}
		6: { linear_blend(
			image1: template(colors[1])
			image2: template(colors[2])
			x1: 0, y1: -1
			x2: 0, y2: 1.8
		   )}
		7: { template(colors[4]) }
	]
	horizontal: horizontal_pt_hybrid
	vertical: [
		1: { template(colors[0]) }
		2: { template(colors[0]) }
		3: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[1])
			x1: 0, y1: 0
			x2: 0, y2: 2
		   )}
		4: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[1])
			x1: 0, y1: -1.5
			x2: 0, y2: 1
		   )}
		5: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[1])
			x1: 0, y1: -1.1
			x2: 0, y2: 0.2
		   )}
		6: { template(colors[1]) } # Probably not right
		7: { template(colors[2]) }
	]
	overlay: overlay_hybrid
]

flip_pt_hybrid2 := [
	radial: [
		0: { template("c")       }
		1: { template(colors[0]) }
		2: { template(colors[0]) }
		3: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[2])
			x1: 0, y1: -1
			x2: 0, y2: 1.1
		   )}
		4: { template(colors[3]) }
		5: { template(colors[4]) }
		6: { template(colors[5]) }
		7: { template(colors[5]) }
	]
	horizontal: [
		1: { template(colors[0]) }
		2: { template(colors[0]) }
		3: { template(colors[0]) }
		4: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[1])
			x1: 0.4,  y1: 0
			x2: 1.5,  y2: 0
		   )}
		5: { linear_blend(
			image1: template(colors[0])
			image2: template(colors[1])
			x1: 0.08,  y1: 0
			x2: 0.65,  y2: 0
		   )}
		6: { template(colors[5]) } #TODO
		7: { template(colors[6]) } #TODO
	]
	vertical: [
		1: { template(colors[0]) }
		2: { template(colors[1]) }
		3: { linear_blend(
			image1: template(colors[1])
			image2: template(colors[2])
			x1: 0, y1: -1
			x2: 0, y2: 1.5
		   )}
		4: { linear_blend(
			image1: template(colors[2])
			image2: template(colors[3])
			x1: 0, y1: 0.5
			x2: 0, y2: 3
		   )}
		5: { template(colors[3]) }
		6: { template(colors[5]) } # Probably not right
		7: { template(colors[6]) }
	]
	overlay: overlay_hybrid
]

########################################################################
# Textbox and typeline for FPM templates and Futureshifts
########################################################################

textbox_hybrid := [
	radial: [
		0: { template("c")       }
		1: { template(colors[0]) }
		2: card_hybrid_2
		3: { template(colors[2]) }
		4: { card_hybrid_2(colors: colors[3] + colors[2]) }
		5: { template(colors[3]) }
		6: { card_hybrid_2(colors: colors[4] + colors[3]) }
		7: { template(colors[4]) }
	]
	horizontal: horizontal_card_hybrid
	vertical:  [
		0: { template("c")       }
		1: { template(colors[0]) }
		2: { template(colors[1]) }
		3: { template(colors[2]) } # TODO
		4: { template(colors[3]) }
		5: { template(colors[4]) }
		6: { template(colors[5]) }
		7: { template(colors[6]) }
	]
	overlay: overlay_hybrid
]

typeline_hybrid := textbox_hybrid

########################################################################
# Putting it all together.
########################################################################

# Determine the card background based on the card_color multiple choice options
color_background := {
	
	# for thumbnails
	if input == "hybrid" then
		input := "white,blue,red,{hybrid_previews},horizontal"
	else if input == "overlay" then
		input := "blue,red,{hybrid_previews},overlay"
	else if input == "radial" or input == "horizontal" or input == "vertical" then
		input := "white,blue,red,{hybrid_previews}," + input
	else if input == "reversed" then
		input := "white,blue,red,{hybrid_previews},horizontal,reversed"
	
	# What type of 'hybrid'?
	land     := chosen(choice:"land")
	multi    := chosen(choice:"multicolor")
	hybrid   := chosen(choice:"hybrid")
	artifact := chosen(choice:"artifact")
	if land and colored_lands then template := land_template # use land template instead?
	
	# The selected colors
	colors := sort_text( order: "(wubrg)"
	                   , (if chosen(choice:"white") then "w")
	                   + (if chosen(choice:"blue")  then "u")
	                   + (if chosen(choice:"black") then "b")
	                   + (if chosen(choice:"red")   then "r")
	                   + (if chosen(choice:"green") then "g"))
	if multi and (hybrid or colors == "") then (
		colors := colors + "m"
		multi := false
	)
	if artifact and (multi or hybrid or colors == "") then (
		colors := colors + "a"
		artifact := false
	)
	if chosen(choice:"reversed") then colors := reverse(colors)
	color_count := number_of_items(in: colors)
	if colors == "" then colors == "c"
	
	# 'shape' / type of hybrid
	shape := if      chosen(choice:"horizontal") then "horizontal"
	         else if chosen(choice:"vertical")   then "vertical"
	         else if chosen(choice:"overlay")    then "overlay"
	         else                                     "radial"
	
	# Determine background (allows styles to hook something else here)
	color_combination()
}

color_combination := {
	# The base hybrid, without the outer frame blended over it
	base := base_hybrid[shape][color_count]()
	
	# Put a frame around it?
	if land and not colored_lands then
		masked_blend(
			mask:  "multicolor_blend_{type}.png",
			dark:  land_template("c"),
			light: base,
		)
	else if land and multi and mask_multi_land_with_color() then
		masked_blend(
			mask:  "hybrid_blend_{type}.png",
			dark:  base,
			light:  land_template("m"),
		)
	else if multi then 
		masked_blend(
			mask:  "multicolor_blend_{type}.png",
			dark:  template("m"),
			light: base,
		)
	else if artifact then 
		masked_blend(
			mask:  "artifact_blend_{type}.png",
			dark:  template("a"),
			light: base,
		)
	else if color_count > 1 and mask_hybrid_with_land() then
		masked_blend(
			mask:  "hybrid_blend_{type}.png",
			dark:  land_template("c"),
			light: base,
		)
	else base
}

########################################################################
# Specific types
########################################################################

card_background := { color_background(type:"card",     base_hybrid:card_hybrid) }
card_ptbox      := { color_background(type:"pt",       base_hybrid:pt_hybrid) }
flip_ptbox      := { color_background(type:"pt",       base_hybrid:flip_pt_hybrid) }
flip_ptbox2     := { color_background(type:"pt2",      base_hybrid:flip_pt_hybrid2) }
card_textbox    := { color_background(type:"textbox",  base_hybrid:textbox_hybrid) }
card_typeline   := { color_background(type:"typeline", base_hybrid:typeline_hybrid) }

########################################################################
# Font colors
########################################################################

# For which colors is the font white?
font_colors_white := { input == "b" or input == "l" }

font_color_positions := [
	radial:     [0,0,0,2,3,3,4,4]
	horizontal: [0,0,0,0,0,0,0,0]
	vertical:   [0,0,1,2,3,4,5,6]
	overlay:    [0,0,0,0,0,0,0,0]
]

# Determine whether light or dark fonts should be used
font_white := {
	hybrid   := chosen(choice:"hybrid")
	artifact := chosen(choice:"artifact")
	colors := sort_text( order: "(wubrg)"
		           , (if chosen(choice:"white") then "w")
		           + (if chosen(choice:"blue")  then "u")
		           + (if chosen(choice:"black") then "b")
		           + (if chosen(choice:"red")   then "r")
		           + (if chosen(choice:"green") then "g"))
		           + (if artifact               then "a")
	font_colors_white(
		if chosen(choice:"land") then "l"
		else if input == "artifact, multicolor" then "a"
		else if chosen(choice:"multicolor")     then "m"
		else if artifact and not hybrid         then "a"
		else if colors == ""                    then "c"
		else colors[
			font_color_positions
			[	if      chosen(choice:"horizontal") then "horizontal"
				else if chosen(choice:"vertical")   then "vertical"
				else if chosen(choice:"overlay")    then "overlay"
				else                                     "radial"
			][number_of_items(in: colors)]
		]
	)
}

font_color := { if font_white() then rgb(255,255,255) else rgb(0,0,0) }
