#version 150

#moj_import <minecraft:fog.glsl>
#moj_import <minecraft:dynamictransforms.glsl>

uniform sampler2D Sampler0;

in float sphericalVertexDistance;
in float cylindricalVertexDistance;
in vec4 vertexColor;
in vec2 texCoord0;

out vec4 fragColor;

//	küss sein herz
//	https://airtightinteractive.com/util/hex-to-glsl/

//	5TMOD_VARS
bool _CONF_RECOLORS = true;
bool _FLAG_ISMAP = false;

//in vec3 ausgabePos;
//	5TMOD_END

//	FARBVERGLEICH
bool istGewollteFarbe(vec3 rgbCurrent, vec3 rgbWanted, float leniencyAmount) {    
    if ( abs(rgbCurrent.r - rgbWanted.r) > leniencyAmount ) { return false; }
	if ( abs(rgbCurrent.g - rgbWanted.g) > leniencyAmount ) { return false; }
	if ( abs(rgbCurrent.b - rgbWanted.b) > leniencyAmount ) { return false; }
	
	return true;
}

//	I  have a slight feel allocating all these colors each frame in the shader isnt the best perf wise */

//	FARBEN
vec3 debugBeige = vec3(0.878, 0.812, 0.722);
vec3 splashYellow = vec3(1.0, 0.925, 0.0);
vec3 shadowBeige = vec3(0.247, 0.227, 0.204);	
vec3 colorYellow = vec3(1.0, 0.925, 0.275);
vec3 shadowYellow = vec3(0.247,0.227,0.067);
vec3 colorGreen = vec3(0.333, 0.925, 0.275);
vec3 shadowGreen = vec3(0.137, 0.176, 0.071);
vec3 colorDarkGreen = vec3(0.0, 0.616, 0.0);
vec3 colorGold = vec3(1.0, 0.616, 0.0);
vec3 shadowGold = vec3(0.247, 0.153, 0.0);
vec3 colorRed = vec3(0.667, 0.0, 0.0);
vec3 colorLightRed = vec3(1.0, 0.31, 0.275);
vec3 shadowLightRed = vec3(0.247, 0.075, 0.067);
vec3 colorDarkBlue = vec3(0.0, 0.0, 0.545);
vec3 shadowDarkBlue = vec3(0.0, 0.0, 0.133);
vec3 shadowDarkGreen = vec3(0.082, 0.227, 0.067);
vec3 shadowRed = vec3(0.165, 0.0, 0.0);
vec3 shadowDarkerGreen = vec3(0.0, 0.153, 0.0);
vec3 shadowGray = vec3(0.165,0.153,0.133);
vec3 shadowDarkGray = vec3(0.082,0.075,0.067);
vec3 shadowDarkerGray = vec3(0.125, 0.118, 0.102);
vec3 shadowDarkerBlue = vec3(0.082, 0.075, 0.204);

vec3 shadowCyan = vec3(0.082, 0.227, 0.204);
vec3 shadowAqua = vec3(0.0, 0.153, 0.133);
vec3 shadowPink = vec3(0.247, 0.075, 0.204);
vec3 shadowPurple = vec3(0.165, 0.0, 0.133);
vec3 colorPurple = vec3(0.667, 0.0, 0.545);
vec3 colorPink = vec3(1.0, 0.31, 0.82);

/*  E  */ vec3 newYellow = vec3(0.988, 0.729, 0.271);
/*  E  */ vec3 newYellowShadow = vec3(0.42, 0.145, 0.18);
/*  F  */ vec3 newWhiteShadow = vec3(0.184, 0.314, 0.188);
/*  0  */ vec3 newBlack = vec3(0.102, 0.078, 0.141);
/*  A  */ vec3 newGreen = vec3(0.549, 0.714, 0.286);
/*  2  */ vec3 newDarkGreen = vec3(0.243, 0.537, 0.259); 		//vec3(0.286, 0.537, 0.243);
/*  6  */ vec3 newGold = vec3(0.941, 0.561, 0.188);
/*  6  */ vec3 newGoldShadow = vec3(0.11, 0.149, 0.333);
/*  4  */ vec3 newRed = vec3(1.0, 0.263, 0.357); 				//vec3(0.91, 0.173, 0.294);
/*	C  */ vec3 newLightRed = vec3(1.0, 0.333, 0.412);			//vec3(1.0, 0.275, 0.325);
/*	1  */ vec3 newDarkBlue = vec3(0.129, 0.133, 0.361);
/*  4  */ vec3 newRedShadow = vec3(0.2, 0.075, 0.125);
/*  7  */ vec3 newGrayShadow = vec3(0.106, 0.118, 0.137);
/*  8  */ vec3 newDarkerGreenShadow = vec3(0.075, 0.149, 0.145);
/*  5  */ vec3 newPurple = vec3(0.682, 0.122, 0.392);			//vec3(0.639, 0.078, 0.329);
/*  5  */ vec3 newPink = vec3(0.961, 0.447, 0.784);

void main() {
    vec4 color = texture(Sampler0, texCoord0) * vertexColor * ColorModulator;
    if (color.a < 0.1) { discard; }
	
	/*	
	[ ! ] THIS FILE ALSO AFFECTS:
		- name tag above entities
		- typing underscore cursor
		- pixels on maps held in hand & on item frames 
			( MOJANG WHY DO MAPS USE THE TEXT SHADERS !! )
		- icons displayed on maps
			( T_T ) 
	*/
	
	//	MAP LAYER CHECK
	ivec2 textureSize2d = textureSize(Sampler0, 0);
	if (textureSize2d.x == 128.0 && textureSize2d.y == 128.0 ) 
		{ _FLAG_ISMAP = true; }
			
	/*
		ngl - reducing saturation of the colors makes servers which
		have many text elements so much easier on the eyes tbh
	*/
	if (_CONF_RECOLORS && _FLAG_ISMAP == false) {
		/*
		[ ! ] NOTED ISSUE:
			when culled the text is missing in container screens as
			container screen text only uses the shadow color alone.
			unfortunately idk how to fix/check for this.
			
			else I would've made a text shadow toggle in here too
		*/
		float globalLeniency = 0.08;
		
		//	BLACK
		if( istGewollteFarbe(color.rgb, vec3(0,0,0), 0.0) ){ 
			color.rgb = newBlack.rgb;
		}
		//	GREEN BLACK
		if( istGewollteFarbe(color.rgb, vec3(0.016, 0.031, 0.012), 0.08) ){ 
			color.rgb = newDarkerGreenShadow;
		}
		
		//	WHITE 	>	SHADOW
		if( istGewollteFarbe(color.rgb, shadowBeige.rgb, 0.08) ){ 
			color.rgb = newWhiteShadow.rgb;
		}
		
		//	YELLOW
		if( istGewollteFarbe(color.rgb, colorYellow.rgb, globalLeniency) ){ 
			color.rgb = newYellow.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowYellow.rgb, 0.06) ){ 
			color.rgb = newYellowShadow.rgb;
		}
		
		//	YELLOW 	>	SPLASH TEXT
		if( istGewollteFarbe(color.rgb, splashYellow.rgb, globalLeniency) ){ 
			color.rgb = newYellow.rgb;
		}
		if( istGewollteFarbe(color.rgb, vec3(0.247, 0.227, 0.0), 0.08) ){ 
			color.rgb = newYellowShadow;
		}
		
		//	GREEN
		if( istGewollteFarbe(color.rgb, colorGreen.rgb, globalLeniency) ){ 
			color.rgb = newGreen.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowGreen.rgb, 0.04) ){ 
			color.rgb = newBlack.rgb;
		}
		
		//	DARK GREEN
		if( istGewollteFarbe(color.rgb, colorDarkGreen.rgb, globalLeniency) ){ 
			color.rgb = newDarkGreen.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowDarkGreen.rgb, 0.04) ){ 
			color.rgb = newBlack.rgb;
		}
		
		//	GOLD
		if( istGewollteFarbe(color.rgb, colorGold.rgb, globalLeniency) ){ 
			color.rgb = newGold.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowGold.rgb, 0.06) ){ 
			color.rgb = newGoldShadow.rgb;
		}
		
		//	LIGHT RED
		if( istGewollteFarbe(color.rgb, colorLightRed.rgb, 0.06) ){ 
			color.rgb = newLightRed.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowLightRed.rgb, 0.06) ){ 
			color.rgb = newBlack.rgb;
		}
		
		//	DARK BLUE
		if( istGewollteFarbe(color.rgb, colorDarkBlue.rgb, 0.06) ){ 
			color.rgb = newDarkBlue.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowDarkBlue.rgb, 0.04) ){ 
			color.rgb = newBlack.rgb;
		}
		
		//	DARK RED
		if( istGewollteFarbe(color.rgb, colorRed.rgb, 0.06) ){ 
			color.rgb = newRed.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowRed.rgb, 0.06) ){ 
			color.rgb = newRedShadow.rgb;
		}
		
		//	DARKER GREEN
		if( istGewollteFarbe(color.rgb, shadowDarkerGreen.rgb, 0.04) ){ 
			color.rgb = newDarkBlue.rgb;
		}
		
		//	GRAY
		if( istGewollteFarbe(color.rgb, shadowGray.rgb, 0.02) ){ 
			color.rgb = newDarkerGreenShadow.rgb;
		}
		
		//	DARK GRAY
		if( istGewollteFarbe(color.rgb, shadowDarkGray.rgb, 0.02) ){ 
			color.rgb = newBlack.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowDarkerGray.rgb, 0.02) ){ 
			color.rgb = newDarkerGreenShadow.rgb;
		}
		
		//	DARKER BLUE
		if( istGewollteFarbe(color.rgb, shadowDarkerBlue.rgb, 0.04) ){ 
			color.rgb = newDarkerGreenShadow.rgb;
		}
		
		//	CYAN
		if( istGewollteFarbe(color.rgb, shadowCyan.rgb, 0.04) ){ 
			color.rgb = newDarkerGreenShadow.rgb;
		}
		
		//	AQUA
		if( istGewollteFarbe(color.rgb, shadowAqua.rgb, 0.04) ){ 
			color.rgb = newBlack.rgb;
		}
		
		//	PINK
		if( istGewollteFarbe(color.rgb, colorPink.rgb, 0.06) ){ 
			color.rgb = newPink.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowPink.rgb, 0.04) ){ 
			color.rgb = newRedShadow.rgb;
		}
		
		//	PURPLE
		if( istGewollteFarbe(color.rgb, colorPurple.rgb, 0.06) ){ 
			color.rgb = newPurple.rgb;
		}
		if( istGewollteFarbe(color.rgb, shadowPurple.rgb, 0.04) ){ 
			color.rgb = newDarkBlue.rgb;
		}
		
		//	RED (HARCORE MODE TEXT)
		if( istGewollteFarbe(color.rgb, vec3(1.0, 0.0, 0.0), 0.0) ){ 
			color.rgb = newRed.rgb;
		}
		if( istGewollteFarbe(color.rgb, vec3(0.247, 0.0, 0.0), 0.1) ){ 
			color.rgb = newRedShadow;
		}
	}
	
    fragColor = apply_fog(color, sphericalVertexDistance, cylindricalVertexDistance, FogEnvironmentalStart, FogEnvironmentalEnd, FogRenderDistanceStart, FogRenderDistanceEnd, FogColor);
}
