Skip to content

Instantly share code, notes, and snippets.

@vahan3x
Created October 1, 2018 17:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vahan3x/c2655c1387c14737ccc9191e27a29566 to your computer and use it in GitHub Desktop.
Save vahan3x/c2655c1387c14737ccc9191e27a29566 to your computer and use it in GitHub Desktop.
Displaying image with normalized Display P3 colors. For use with Metal textures using bgra10_xr_srgb pixel format.
// Simple RGB to Linear RGB function respecting negative values
half channelToLinearChannel(half channel) {
if (fabs(channel) <= 0.04045) {
return channel / 12.92;
}
return sign(channel) * pow((fabs(channel) + 0.055) / 1.055, 2.4);
}
// Converting RGB to Linear RGB
half3 RGBToLinearRGB(half3 color) {
return half3(channelToLinearChannel(color.r),
channelToLinearChannel(color.g),
channelToLinearChannel(color.b));
}
// Converting a color from normalized Display P3 to extendedSRGB color space
half4 displayP3ToExtendedRGB(half4 color) {
const half3x3 displayP3ToExtendedSRGBMatrix = half3x3( 1.2249, -0.2247, 0.0,
-0.0420, 1.0419, 0.0,
-0.0197, -0.0786, 1.0979);
return half4(displayP3ToExtendedSRGBMatrix * RGBToLinearRGB(color.rgb), color.a);
}
@vahan3x
Copy link
Author

vahan3x commented Oct 1, 2018

If anyone knows any other way to display a CIImage with displayP3 color space on a MTKView I would appreciate a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment