Skip to content

Instantly share code, notes, and snippets.

@RoyLab
Created May 19, 2017 17:04
Show Gist options
  • Save RoyLab/b3ff2711ba48bcfa589d83979a9645b1 to your computer and use it in GitHub Desktop.
Save RoyLab/b3ff2711ba48bcfa589d83979a9645b1 to your computer and use it in GitHub Desktop.
greyscale to rgb
float interpolate(float val, float y0, float x0, float y1, float x1) {
return (val - x0)*(y1 - y0) / (x1 - x0) + y0;
}
float blue(float grayscale) {
if (grayscale < -0.33) return 1.0;
else if (grayscale < 0.33) return interpolate(grayscale, 1.0, -0.33, 0.0, 0.33);
else return 0.0;
}
float green(float grayscale) {
if (grayscale < -1.0) return 0.0; // unexpected grayscale value
if (grayscale < -0.33) return interpolate(grayscale, 0.0, -1.0, 1.0, -0.33);
else if (grayscale < 0.33) return 1.0;
else if (grayscale <= 1.0) return interpolate(grayscale, 1.0, 0.33, 0.0, 1.0);
else return 1.0; // unexpected grayscale value
}
float red(float grayscale) {
if (grayscale < -0.33) return 0.0;
else if (grayscale < 0.33) return interpolate(grayscale, 0.0, -0.33, 1.0, 0.33);
else return 1.0;
}
float4 torgb(float val)
{
return float4(red(val), green(val), blue(val), 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment