Gamma 校正可能有任何值,但考虑到线性 RGB/非线性 sRGB 转换,2.2 是一个近似值,因此您的公式可能被认为是错误和正确的:
https ://en.wikipedia.org/wiki/SRGB#Theory_of_the_transformation
真正的 sRGB 传递函数基于 2.4 伽马系数,并且在暗值处具有不连续性,如下所示:
float Convert_sRGB_FromLinear (float theLinearValue) {
return theLinearValue <= 0.0031308f
? theLinearValue * 12.92f
: powf (theLinearValue, 1.0f/2.4f) * 1.055f - 0.055f;
}
float Convert_sRGB_ToLinear (float thesRGBValue) {
return thesRGBValue <= 0.04045f
? thesRGBValue / 12.92f
: powf ((thesRGBValue + 0.055f) / 1.055f, 2.4f);
}
实际上,您可能会在某些 GLSL 代码中发现更粗略的近似值,使用 2.0 系数而不是 2.2 和 2.4,以避免使用昂贵的pow()
( x*x
and sqrt()
are used instead)。这是为了实现最佳性能(在旧图形硬件的情况下)和代码简单性,同时牺牲色彩再现。实际上,牺牲并不那么明显,而且大多数游戏都应用了额外的色调映射和用户管理的伽马校正系数,因此结果与 sRGB 标准没有直接关系。
GL_FRAMEBUFFER_SRGB
并且从GL_SRGB8
纹理中采样预计将使用更正确的公式(在纹理采样的情况下,它更有可能在 GPU 上预先计算查找表,而不是真正的公式,因为只有 256 个值要转换)。例如,请参阅对GL_ARB_framebuffer_sRGB 扩展的注释:
Given a linear RGB component, cl, convert it to an sRGB component, cs, in the range [0,1], with this pseudo-code:
if (isnan(cl)) {
/* Map IEEE-754 Not-a-number to zero. */
cs = 0.0;
} else if (cl > 1.0) {
cs = 1.0;
} else if (cl < 0.0) {
cs = 0.0;
} else if (cl < 0.0031308) {
cs = 12.92 * cl;
} else {
cs = 1.055 * pow(cl, 0.41666) - 0.055;
}
The NaN behavior in the pseudo-code is recommended but not specified in the actual specification language.
sRGB components are typically stored as unsigned 8-bit fixed-point values.
If cs is computed with the above pseudo-code, cs can be converted to a [0,255] integer with this formula:
csi = floor(255.0 * cs + 0.5)
这是另一篇描述 OpenGL 应用程序中 sRGB 使用的文章,您可能会发现它很有用:https ://unlimited3d.wordpress.com/2020/01/08/srgb-color-space-in-opengl/