 Implement Phong shading for color output?

Is it possible to impelmenet a colour output that behaves like the phong shader?

I would like to have the specular as a black and white output to use as as a factor to other nodes.

shader testPhong(float lightIntensity = 100,

vector lightDir = vector(-0.98717,-6.82603,0.5882),   //manually entered since getAttribute does not seem to work
vector cameraSpacePosition = vector(2.24144,7.65891,7.92298),
output color outputColor = color(0,0,0))
{

vector surfaceNormal = normalize(N);
float cosAngIncidence = dot(surfaceNormal, lightDir);
cosAngIncidence = clamp(cosAngIncidence, 0, 1);

vector viewDirection = normalize(-cameraSpacePosition);

vector halfAngle = normalize(lightDir + viewDirection);
float blinnTerm = dot(surfaceNormal, halfAngle);
blinnTerm = clamp(blinnTerm, 0, 1);
blinnTerm = cosAngIncidence != 0.0 ? blinnTerm : 0.0;
blinnTerm = pow(blinnTerm, 10);

float d = blinnTerm*2;

outputColor = color(d,d,d);
}

I only get something that looks like a diffuse shader. Someone have any idea of whats wrong? Please stop me if this is impossible somehow.

@poromaa You needed a smoothstep function in order to limit the values that are above zero. I took the liberty of changing a few things to my liking. I hope this is what you were aiming for.

shader testPhong(float lightIntensity = 1,
float Ceiling = 1, /* Sets the sharpness of the edge of the highlight; lower = sharper */
float Floor = .9, /* Sets the value below which all values are 0 */
vector lightDir = vector(-0.98717,-6.82603,0.5882),   //manually entered since getAttribute does not seem to work
vector cameraSpacePosition = vector(2.24144,7.65891,7.92298),
output color outputColor = color(0,0,0))
{
float f = pow(Floor, .1); /* Power function gives the values more subtlety */
float c = pow(Ceiling, .1);

vector surfaceNormal = normalize(N);
float cosAngIncidence = abs(dot(surfaceNormal, lightDir));
/* Replaced the clamp with abs() since the dot() always lies between -1 and 1 */

vector viewDirection = normalize(-cameraSpacePosition);

vector halfAngle = normalize(lightDir + viewDirection);
float blinnTerm = dot(surfaceNormal, halfAngle);
blinnTerm = cosAngIncidence != 0.0 ? blinnTerm : 0.0;
blinnTerm = smoothstep(f, c, blinnTerm); /* Cuts off the values lower than the f */

float d = blinnTerm * lightIntensity;

outputColor = color(d,d,d);
}

It seems like I made a mistake in my original script, because I cannot get this to work.
My idea was that if the camera and the light was still, you would be able to move the object around and see the specular update.
Because of some reason this does not work. Tried your refined script but above test fails, unfortunately.

The reason for researching this was to be able to fake color changes in specularity by applying a ramp to the above node. That could for exampe be used for special metallic shaders or fake volumetric shading.

Thanks for your effort and sorry for my incomplete script above.

I think you’re better off just using the phong_ramp. Try it:
#include <stdosl.h>

float Exponent = 10.0,
color Color1 = 0,
color Color2 = 0,
color Color3 = 0,
color Color4 = 0,
color Color5 = 0,
color Color6 = 0,
color Color7 = 0.125,
color Color8 = .8,
normal Normal = N,
output closure color BSDF = 0)
{
color Color = {Color1, Color2, Color3, Color4, Color5, Color6, Color7, Color8};

BSDF = phong_ramp(Normal, Exponent, Color);
}