OSL slope detection shader

Just started tinkering with OSL. As a first exercise I wanted to make slope detection shader which allows to shade horizontal and vertical geometry with nice smooth falloff between the two (depending on the face angle). I did something wrong thou, because it’s not working.

Here is my current attempt:

color safe_mix(color vertical_color, color horizontal_color, float mixvalue)
    return mix(vertical_color, horizontal_color, clamp(mixvalue, 0, 1));

shader test_landscape_shader(
    color Vertical_Color = color(1,1,1),
    color Horizontal_Color = color(0,0,0),
    output color Color = 0,
    float slope = ( atan( N[1] / N[0] )) * ( hypot( N[0], N[1] ) / N[2] );

    Color = safe_mix(Vertical_Color, Horizontal_Color, slope);

And the result:

My thougth process was to get theta angle of the normal N at the point P and use it as a factor in mixing two colors.
For math I used formula for Cartesian system conversion https://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates

I’m using official OSL manual and Zap Aandersson tutorials as guides.
Thanks for any help.