// By Fallow Deer - fallowdeer.eu
#ifndef VD_COLOR_GLSL
#define VD_COLOR_GLSL
#include "/lib/settings.glsl"

#ifndef CAT_THE_END
#define CAT_THE_END 8
#endif
#ifndef CAT_NETHER
#define CAT_NETHER 16
#endif

float vd_luma(vec3 color) {
    return dot(color, vec3(0.2126, 0.7152, 0.0722));
}

vec3 vd_saturate(vec3 color, float amount) {
    float l = vd_luma(color);
    return mix(vec3(l), color, amount);
}

vec3 vd_vibrance(vec3 color, float amount) {
    float mx = max(max(color.r, color.g), color.b);
    float avg = (color.r + color.g + color.b) * 0.3333333;
    float protection = clamp(1.0 - abs(mx - avg), 0.0, 1.0);
    float l = vd_luma(color);
    return mix(vec3(l), color, 1.0 + amount * protection * 0.42);
}

vec3 vd_contrast(vec3 color, float amount) {
    return (color - 0.5) * amount + 0.5;
}

vec3 vd_filmic(vec3 x) {
    
    x = max(x, vec3(0.0));
    return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

vec3 vd_grade(vec3 color) {
    color *= EXPOSURE;
    color = vd_saturate(color, SATURATION);
    color = vd_vibrance(color, VIBRANCE - 1.0);
    color = vd_contrast(color, CONTRAST);

    
    float l = vd_luma(color);
    vec3 shadowTint = vec3(0.90, 0.97, 1.10);
    vec3 highlightTint = vec3(1.11, 1.04, 0.94);
    color *= mix(shadowTint, highlightTint, smoothstep(0.18, 0.92, l));

    color = vd_filmic(color);
    color = pow(clamp(color, 0.0, 1.0), vec3(0.94));
    return color;
}

vec3 vd_lush_block_color(vec3 color, float entityId) {
    
    if (abs(entityId - 10010.0) < 0.5) {
        color *= vec3(0.94, 1.14, 0.92);
        color = vd_saturate(color, 1.08);
    }
    if (abs(entityId - 10011.0) < 0.5) {
        color *= vec3(0.91, 1.12, 0.91);
        color = vd_saturate(color, 1.07);
    }
    if (abs(entityId - 10013.0) < 0.5) {
        color *= vec3(1.30, 1.10, 0.84);
    }
    return color;
}

bool vd_is_nether(int biomeCategory, bool hasCeiling, bool hasSkylight) {
    return (biomeCategory == CAT_NETHER) || (hasCeiling && !hasSkylight);
}

bool vd_is_end(int biomeCategory) {
    return biomeCategory == CAT_THE_END;
}

vec3 vd_dimension_surface_tint(vec3 color, int biomeCategory, bool hasCeiling, bool hasSkylight, float endFlashIntensity) {
    if (vd_is_nether(biomeCategory, hasCeiling, hasSkylight)) {
        color *= mix(vec3(1.0), vec3(1.15, 0.88, 0.72), NETHER_ATMOSPHERE * 0.34);
        float ember = smoothstep(0.46, 0.98, color.r - color.b * 0.25);
        color += vec3(0.06, 0.018, 0.004) * ember * NETHER_ATMOSPHERE;
    }
    if (vd_is_end(biomeCategory)) {
        color *= mix(vec3(1.0), vec3(1.06, 0.98, 1.18), END_ATMOSPHERE * 0.28);
        color += vec3(0.040, 0.022, 0.080) * END_ATMOSPHERE;
        color += vec3(0.10, 0.08, 0.18) * clamp(endFlashIntensity, 0.0, 1.0) * 0.18;
    }
    return color;
}

#endif
