/* Oxygen Atmosphere - color_utils.glsl
Usefull data for color manipulation.

Javier Garduño - GNU Lesser General Public License v3.0
Oxygen Atmosphere visual theme added 2026-08-02.
Oxygen Atmosphere palette revision added 2026-08-02.
*/

uniform float dayMoment;
uniform float dayMixer;
uniform float nightMixer;
uniform int moonPhase;

#ifdef OXYGEN_ATMOSPHERE_STYLE
    uniform int worldDay;
    #include "/lib/oxygen_atmosphere_lunar_event.glsl"

    #define OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE oxygenAtmosphereLunarEclipseAmount(worldDay)
    #define OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE_LIGHTING (OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE * OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE_INTENSITY)
#endif

#ifdef UNKNOWN_DIM
    uniform vec3 fogColor;
    uniform vec3 skyColor;
#endif

#define NIGHT_BRIGHT_PHASE (NIGHT_BRIGHT + (NIGHT_BRIGHT * (abs(4.0 - moonPhase) * 0.25)))

#ifdef OXYGEN_ATMOSPHERE_STYLE
    // The fork's palette remains stable even if Iris retains an old
    // COLOR_SCHEME override in its per-pack configuration.
    #define OMNI_TINT 0.32
    #define LIGHT_SUNSET_COLOR vec3(0.90, 0.70, 0.56)
    #define LIGHT_DAY_COLOR vec3(0.84, 0.87, 0.88)
    #define LIGHT_NIGHT_COLOR mix(vec3(0.060, 0.086, 0.142), vec3(0.245, 0.052, 0.028), OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE_LIGHTING) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.27, 0.33, 0.38)
    #define ZENITH_DAY_COLOR vec3(0.36, 0.44, 0.50)
    #define ZENITH_NIGHT_COLOR mix(vec3(0.009, 0.018, 0.038), vec3(0.055, 0.009, 0.006), OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE_LIGHTING) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(0.72, 0.61, 0.54)
    #define HORIZON_DAY_COLOR vec3(0.60, 0.65, 0.68)
    #define HORIZON_NIGHT_COLOR mix(vec3(0.020, 0.035, 0.064), vec3(0.110, 0.020, 0.010), OXYGEN_ATMOSPHERE_LUNAR_ECLIPSE_LIGHTING) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.025, 0.075, 0.12)
#elif COLOR_SCHEME == 0  // Ethereal
    #define OMNI_TINT 0.4
    #define LIGHT_SUNSET_COLOR vec3(0.887528, 0.443394, 0.301044)
    #define LIGHT_DAY_COLOR vec3(0.90, 0.84, 0.79)
    #define LIGHT_NIGHT_COLOR vec3(0.0317353, 0.0467353, 0.0637353) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.2617647, 0.33529412, 0.52352941)
    #define ZENITH_DAY_COLOR vec3(0.0785098, 0.24352941, 0.54901961)
    #define ZENITH_NIGHT_COLOR vec3(0.0168, 0.0228, 0.03) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(1.0, 0.6, 0.394)
    #define HORIZON_DAY_COLOR vec3(0.65, 0.91, 1.3)
    #define HORIZON_NIGHT_COLOR vec3(0.02556, 0.03772, 0.05244) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.05, 0.1, 0.11)
#elif COLOR_SCHEME == 1  // New shoka
    #define OMNI_TINT 0.25
    #define LIGHT_SUNSET_COLOR vec3(1.0, 0.588, 0.3555)
    #define LIGHT_DAY_COLOR vec3(0.90, 0.84, 0.79)
    #define LIGHT_NIGHT_COLOR vec3(0.04786874, 0.05175001, 0.06112969) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.143, 0.24394118, 0.36450981)
    #define ZENITH_DAY_COLOR vec3(0.143, 0.24394118, 0.36450981)
    #define ZENITH_NIGHT_COLOR vec3(0.014, 0.019, 0.025) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(1.0, 0.648, 0.37824)
    #define HORIZON_DAY_COLOR vec3(0.65, 0.91, 1.3)
    #define HORIZON_NIGHT_COLOR vec3(0.0213, 0.0306, 0.0387) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.05, 0.1, 0.11)
#elif COLOR_SCHEME == 2  // Shoka
    #define OMNI_TINT 0.5
    #define LIGHT_SUNSET_COLOR vec3(0.70656, 0.44436, 0.2898)
    #define LIGHT_DAY_COLOR vec3(0.91640625, 0.91640625, 0.635375)
    #define LIGHT_NIGHT_COLOR vec3(0.04786874, 0.05175001, 0.06112969) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.104, 0.17741177, 0.26509804)
    #define ZENITH_DAY_COLOR vec3(0.13, 0.22176471, 0.33137255)
    #define ZENITH_NIGHT_COLOR vec3(0.014, 0.019, 0.025) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(0.715 , 0.5499, 0.416)
    #define HORIZON_DAY_COLOR vec3(0.364 , 0.6825, 0.91)
    #define HORIZON_NIGHT_COLOR vec3(0.0213, 0.0306, 0.0387) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.01647059, 0.13882353, 0.16470588)
#elif COLOR_SCHEME == 3  // Legacy
    #define OMNI_TINT 0.5
    #define LIGHT_SUNSET_COLOR vec3(0.96876, 0.4356254, 0.26002448)
    #define LIGHT_DAY_COLOR vec3(0.88504, 0.88504, 0.8372)
    #define LIGHT_NIGHT_COLOR vec3(0.04693014, 0.0507353 , 0.05993107) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.09410295, 0.20145588, 0.34905882)
    #define ZENITH_DAY_COLOR vec3(0.182, 0.351, 0.754)
    #define ZENITH_NIGHT_COLOR vec3(0.00841175, 0.01651763, 0.025) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(0.81, 0.44165647, 0.25293529)
    #define HORIZON_DAY_COLOR vec3(0.572, 1.014, 1.248)
    #define HORIZON_NIGHT_COLOR vec3(0.01078431, 0.02317647, 0.035) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.01647059, 0.13882353, 0.16470588)
#elif COLOR_SCHEME == 4  // Captain
    #define OMNI_TINT 0.5
    #define LIGHT_SUNSET_COLOR vec3(0.84456, 0.52992, 0.26496001)
    #define LIGHT_DAY_COLOR vec3(0.83064961, 0.93448079, 1.1032065)
    #define LIGHT_NIGHT_COLOR vec3(0.02597646, 0.05195295, 0.069) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.18135 , 0.230256, 0.332592)
    #define ZENITH_DAY_COLOR vec3(0.104, 0.26, 0.507)
    #define ZENITH_NIGHT_COLOR vec3(0.004 ,0.01, 0.0195) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(1.3, 0.8632, 0.3952)
    #define HORIZON_DAY_COLOR vec3(0.65, 0.91, 1.3)
    #define HORIZON_NIGHT_COLOR vec3(0.025, 0.035, 0.05) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.05, 0.1, 0.11)
#elif COLOR_SCHEME == 5  // Psychedelic
    #define OMNI_TINT 0.45
    #define LIGHT_SUNSET_COLOR vec3(0.85 , 0.47058824, 0.17921569)
    #define LIGHT_DAY_COLOR vec3(0.91021875, 0.95771875, 0.6)
    #define LIGHT_NIGHT_COLOR vec3(0.04223712, 0.04566177, 0.05393796) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.18135 , 0.230256, 0.332592)
    #define ZENITH_DAY_COLOR vec3(0.104, 0.26, 0.507)
    #define ZENITH_NIGHT_COLOR vec3(0.004 ,0.01, 0.0195) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(1.3, 0.8632, 0.3952)
    #define HORIZON_DAY_COLOR vec3(0.65, 0.91, 1.3)
    #define HORIZON_NIGHT_COLOR vec3(0.025, 0.035, 0.05) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.018, 0.12 , 0.18)
#elif COLOR_SCHEME == 6  // Cocoa
    #define OMNI_TINT 0.4
    #define LIGHT_SUNSET_COLOR vec3(0.918528, 0.5941728, 0.2712528)
    #define LIGHT_DAY_COLOR vec3(0.897, 0.897, 0.5718375)
    #define LIGHT_NIGHT_COLOR vec3(0.04693014, 0.0507353, 0.05993107) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.117, 0.26, 0.494)
    #define ZENITH_DAY_COLOR vec3(0.234, 0.403, 0.676)
    #define ZENITH_NIGHT_COLOR vec3(0.014, 0.019, 0.031) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(1.183, 0.858, 0.611)
    #define HORIZON_DAY_COLOR vec3(0.52, 0.975, 1.3)
    #define HORIZON_NIGHT_COLOR vec3(0.022, 0.029, 0.049) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.0196, 0.1804, 0.3216)
#elif COLOR_SCHEME == 7  // Testigo
    #define OMNI_TINT 0.65
    #define LIGHT_SUNSET_COLOR vec3(0.70656, 0.44436, 0.2898)
    #define LIGHT_DAY_COLOR vec3(0.88504, 0.88504, 0.8372)
    #define LIGHT_NIGHT_COLOR vec3(0.04786874, 0.05175001, 0.06112969) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.104, 0.17741177, 0.26509804)
    #define ZENITH_DAY_COLOR vec3(0.05098, 0.25990, 0.44313)
    #define ZENITH_NIGHT_COLOR vec3(0.004 ,0.01, 0.0195) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(0.715 , 0.5499, 0.416)
    #define HORIZON_DAY_COLOR vec3(0.65, 0.91, 1.3)
    #define HORIZON_NIGHT_COLOR vec3(0.025, 0.035, 0.05) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.0118, 0.1098, 0.1922)
#elif COLOR_SCHEME == 8  // Oxygen Atmosphere
    // Neutral daylight preserves biome colors. Cool sky and shadows provide
    // atmosphere, while warm sunset light creates cinematic color contrast.
    #define OMNI_TINT 0.32
    #define LIGHT_SUNSET_COLOR vec3(1.00, 0.68, 0.48)
    #define LIGHT_DAY_COLOR vec3(0.93, 0.92, 0.88)
    #define LIGHT_NIGHT_COLOR vec3(0.070, 0.094, 0.150) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(0.10, 0.18, 0.35)
    #define ZENITH_DAY_COLOR vec3(0.20, 0.32, 0.48)
    #define ZENITH_NIGHT_COLOR vec3(0.010, 0.019, 0.040) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(1.00, 0.55, 0.35)
    #define HORIZON_DAY_COLOR vec3(0.68, 0.72, 0.74)
    #define HORIZON_NIGHT_COLOR vec3(0.022, 0.038, 0.068) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(0.025, 0.075, 0.12)
#elif COLOR_SCHEME == 99 // Custom
    #define OMNI_TINT OMNI_TINT_CUSTOM
    #define LIGHT_SUNSET_COLOR vec3(LIGHT_SUNSET_COLOR_R, LIGHT_SUNSET_COLOR_G, LIGHT_SUNSET_COLOR_B)
    #define LIGHT_DAY_COLOR vec3(LIGHT_DAY_COLOR_R, LIGHT_DAY_COLOR_G, LIGHT_DAY_COLOR_B)
    #define LIGHT_NIGHT_COLOR vec3(LIGHT_NIGHT_COLOR_R, LIGHT_NIGHT_COLOR_G, LIGHT_NIGHT_COLOR_B) * NIGHT_BRIGHT_PHASE

    #define ZENITH_SUNSET_COLOR vec3(ZENITH_SUNSET_COLOR_R, ZENITH_SUNSET_COLOR_G, ZENITH_SUNSET_COLOR_B)
    #define ZENITH_DAY_COLOR vec3(ZENITH_DAY_COLOR_R, ZENITH_DAY_COLOR_G, ZENITH_DAY_COLOR_B)
    #define ZENITH_NIGHT_COLOR vec3(ZENITH_NIGHT_COLOR_R, ZENITH_NIGHT_COLOR_G, ZENITH_NIGHT_COLOR_B) * NIGHT_BRIGHT_PHASE

    #define HORIZON_SUNSET_COLOR vec3(HORIZON_SUNSET_COLOR_R, HORIZON_SUNSET_COLOR_G, HORIZON_SUNSET_COLOR_B)
    #define HORIZON_DAY_COLOR vec3(HORIZON_DAY_COLOR_R, HORIZON_DAY_COLOR_G, HORIZON_DAY_COLOR_B)
    #define HORIZON_NIGHT_COLOR vec3(HORIZON_NIGHT_COLOR_R, HORIZON_NIGHT_COLOR_G, HORIZON_NIGHT_COLOR_B) * NIGHT_BRIGHT_PHASE

    #define WATER_COLOR vec3(WATER_COLOR_R, WATER_COLOR_G, WATER_COLOR_B)
#endif

#define NV_COLOR vec3(NV_COLOR_R, NV_COLOR_G, NV_COLOR_B)

#if BLOCKLIGHT_TEMP == 0
    #define CANDLE_BASELIGHT vec3(0.29975, 0.15392353, 0.0799)
#elif BLOCKLIGHT_TEMP == 1
    #define CANDLE_BASELIGHT vec3(0.27475, 0.17392353, 0.0899)
#elif BLOCKLIGHT_TEMP == 2
    #define CANDLE_BASELIGHT vec3(0.24975, 0.19392353, 0.0999)
#elif BLOCKLIGHT_TEMP == 3
    #define CANDLE_BASELIGHT vec3(0.22, 0.19, 0.14)
#else
    #define CANDLE_BASELIGHT vec3(0.19, 0.19, 0.19)
#endif

#include "/lib/day_blend.glsl"

#ifdef OXYGEN_ATMOSPHERE_STYLE
    vec3 oxygenAtmosphereWaterSurfacePalette(
        float waterRainStrength
    ) {
        vec3 clearWater = dayBlend(
            vec3(0.052, 0.112, 0.122),
            vec3(0.038, 0.124, 0.142),
            vec3(0.026, 0.074, 0.138)
        );
        vec3 rainWater = dayBlend(
            vec3(0.046, 0.104, 0.120),
            vec3(0.040, 0.108, 0.128),
            vec3(0.030, 0.082, 0.148)
        );
        return mix(
            clearWater,
            rainWater,
            waterRainStrength * 0.58
        );
    }

    vec3 oxygenAtmosphereUnderwaterPalette(
        float waterRainStrength
    ) {
        // Preserve the surface identity below water while allowing the volume
        // to scatter slightly more cyan/blue as depth increases.
        vec3 surfaceWater =
            oxygenAtmosphereWaterSurfacePalette(
                waterRainStrength
            );
        vec3 depthScattering = dayBlend(
            vec3(0.038, 0.096, 0.116),
            vec3(0.028, 0.108, 0.138),
            vec3(0.024, 0.068, 0.132)
        );
        vec3 volumeWater = mix(
            surfaceWater,
            depthScattering,
            0.46
        );
        return max(
            volumeWater,
            vec3(0.020, 0.058, 0.112)
        );
    }

    float oxygenAtmosphereOvercastDirectStrength(float sourceStrength) {
        float directionality = dayBlendFloat(0.38, 0.44, 0.27);
        float diffuseFloor = dayBlendFloat(0.18, 0.20, 0.12);
        return mix(
            diffuseFloor,
            clamp(sourceStrength, 0.0, 1.0),
            directionality
        );
    }

    // A permanently cloud-heavy sky should behave like a very large area
    // light. Keep a small directional component for shape, but let diffuse
    // skylight carry most of the terrain so noon never looks sun-blasted.
    void applyOxygenAtmosphereOvercastLighting(
        inout vec3 directColor,
        inout float directStrength,
        inout vec3 ambientLight,
        float skyVisibility
    ) {
        float directionalScale = dayBlendFloat(0.46, 0.42, 0.28);
        directStrength = oxygenAtmosphereOvercastDirectStrength(directStrength);
        directColor *= directionalScale;

        vec3 overcastAmbient = dayBlend(
            vec3(0.32, 0.32, 0.31),
            vec3(0.36, 0.39, 0.41),
            vec3(0.070, 0.100, 0.165) * NIGHT_BRIGHT_PHASE
        );
        overcastAmbient *= mix(
            0.42,
            1.0,
            smoothstep(0.05, 0.92, skyVisibility)
        );

        // Do not flatten caves: the new diffuse field is introduced only on
        // surfaces that genuinely see the sky.
        float outdoorBlend =
            smoothstep(0.10, 0.88, skyVisibility) *
            dayBlendFloat(0.58, 0.68, 0.52);
        ambientLight = mix(ambientLight, overcastAmbient, outdoorBlend);
    }
#endif

// Fog parameter per hour
#ifdef OXYGEN_ATMOSPHERE_STYLE
    // Lower exponents bring the mist closer while retaining foreground detail.
    #define FOG_DENSITY 1.35
    #define FOG_DAY 1.35
    #define FOG_SUNSET 1.20
    #define FOG_NIGHT 1.50
#elif VOL_LIGHT == 1 || (VOL_LIGHT == 2 && defined SHADOW_CASTING) || defined UNKNOWN_DIM
    #define FOG_DENSITY 3.0
#else
    #define FOG_DAY 3.0
    #define FOG_SUNSET 2.0
    #define FOG_NIGHT 3.0
#endif

#include "/lib/color_conversion.glsl"
