Your idea is OK, but your function is wrong, correct one is here:
int rgb = Convert.ToInt32("#FFFFFF", 16);
var a = calcLuminance(rgb);
float calcLuminance(int rgb)
{
int r = (rgb & 0xff0000) >> 16;
int g = (rgb & 0xff00) >> 8;
int b = (rgb & 0xff);
return (r*0.299f + g*0.587f + b*0.114f) / 256;
}