Skip to content

Commit 802382a

Browse files
Added documentation for color conversion functions
1 parent cfbe8a3 commit 802382a

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

Graphics/GraphicsAccessories/interface/ColorConversion.h

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,72 +27,152 @@
2727

2828
#pragma once
2929

30+
/// \file
31+
/// Color conversion functions
32+
3033
#include <cmath>
3134
#include "../../../Primitives/interface/BasicTypes.h"
3235
#include "../../../Common/interface/BasicMath.hpp"
3336

3437
DILIGENT_BEGIN_NAMESPACE(Diligent)
3538

36-
// https://en.wikipedia.org/wiki/SRGB
39+
/// Converts linear color to gamma color space
40+
41+
/// \param x - Linear color value in the range [0, 1]
42+
/// \return Gamma color value in the range [0, 1]
43+
///
44+
/// See https://en.wikipedia.org/wiki/SRGB
3745
inline float LinearToGamma(float x)
3846
{
3947
return x <= 0.0031308 ? x * 12.92f : 1.055f * std::pow(x, 1.f / 2.4f) - 0.055f;
4048
}
4149

50+
51+
/// Converts gamma color to linear color space
52+
53+
/// \param x - Gamma color value in the range [0, 1]
54+
/// \return Linear color value in the range [0, 1]
4255
inline float GammaToLinear(float x)
4356
{
4457
return x <= 0.04045f ? x / 12.92f : std::pow((x + 0.055f) / 1.055f, 2.4f);
4558
}
4659

60+
61+
/// Converts linear color to gamma color space
62+
63+
/// \param x - Linear color value in the range [0, 255]
64+
/// \return Gamma color value in the range [0, 255]
4765
float LinearToGamma(Uint8 x);
66+
67+
68+
/// Converts gamma color to linear color space
69+
70+
/// \param x - Gamma color value in the range [0, 255]
71+
/// \return Linear color value in the range [0, 255]
4872
float GammaToLinear(Uint8 x);
4973

74+
75+
/// Converts linear color to gamma color space using fast approximation
76+
77+
/// \param x - Linear color value in the range [0, 1]
78+
/// \return Gamma color value in the range [0, 1]
5079
inline float FastLinearToGamma(float x)
5180
{
5281
return x < 0.0031308f ? 12.92f * x : 1.13005f * sqrtf(std::abs(x - 0.00228f)) - 0.13448f * x + 0.005719f;
5382
}
5483

84+
85+
/// Converts gamma color to linear color space using fast approximation
86+
87+
/// \param x - Gamma color value in the range [0, 1]
88+
/// \return Linear color value in the range [0, 1]
5589
inline float FastGammaToLinear(float x)
5690
{
5791
// http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
5892
return x * (x * (x * 0.305306011f + 0.682171111f) + 0.012522878f);
5993
}
6094

95+
96+
/// Converts RGB color from linear to gamma color space
97+
98+
/// \param RGB - Linear color value in the range [0, 1]
99+
/// \return Gamma color value in the range [0, 1]
61100
inline float3 LinearToSRGB(const float3& RGB)
62101
{
63102
return float3{LinearToGamma(RGB.r), LinearToGamma(RGB.g), LinearToGamma(RGB.b)};
64103
}
65104

105+
106+
/// Converts RGBA color from linear to gamma color space
107+
108+
/// \param RGBA - Linear color value in the range [0, 1]
109+
/// \return Gamma color value in the range [0, 1]
110+
///
111+
/// \note Alpha channel is not converted
66112
inline float4 LinearToSRGBA(const float4& RGBA)
67113
{
68114
return float4{LinearToGamma(RGBA.r), LinearToGamma(RGBA.g), LinearToGamma(RGBA.b), RGBA.a};
69115
}
70116

117+
118+
/// Converts RGB color from gamma to linear color space using fast approximation
119+
120+
/// \param RGB - Gamma color value in the range [0, 1]
121+
/// \return Linear color value in the range [0, 1]
71122
inline float3 FastLinearToSRGB(const float3& RGB)
72123
{
73124
return float3{FastLinearToGamma(RGB.r), FastLinearToGamma(RGB.g), FastLinearToGamma(RGB.b)};
74125
}
75126

127+
128+
/// Converts RGBA color from gamma to linear color space using fast approximation
129+
130+
/// \param RGBA - Gamma color value in the range [0, 1]
131+
/// \return Linear color value in the range [0, 1]
132+
///
133+
/// \note Alpha channel is not converted
76134
inline float4 FastLinearToSRGBA(const float4& RGBA)
77135
{
78136
return float4{FastLinearToGamma(RGBA.r), FastLinearToGamma(RGBA.g), FastLinearToGamma(RGBA.b), RGBA.a};
79137
}
80138

139+
140+
/// Converts RGB color from gamma to linear color space
141+
142+
/// \param RGB - Gamma color value in the range [0, 1]
143+
/// \return Linear color value in the range [0, 1]
81144
inline float3 SRGBToLinear(const float3& SRGB)
82145
{
83146
return float3{GammaToLinear(SRGB.r), GammaToLinear(SRGB.g), GammaToLinear(SRGB.b)};
84147
}
85148

149+
150+
/// Converts RGBA color from gamma to linear color space
151+
152+
/// \param RGBA - Gamma color value in the range [0, 1]
153+
/// \return Linear color value in the range [0, 1]
154+
///
155+
/// \note Alpha channel is not converted
86156
inline float4 SRGBAToLinear(const float4& SRGBA)
87157
{
88158
return float4{GammaToLinear(SRGBA.r), GammaToLinear(SRGBA.g), GammaToLinear(SRGBA.b), SRGBA.a};
89159
}
90160

161+
162+
/// Converts RGB color from gamma to linear color space using fast approximation
163+
164+
/// \param RGB - Gamma color value in the range [0, 1]
165+
/// \return Linear color value in the range [0, 1]
91166
inline float3 FastSRGBToLinear(const float3& SRGB)
92167
{
93168
return float3{FastGammaToLinear(SRGB.r), FastGammaToLinear(SRGB.g), FastGammaToLinear(SRGB.b)};
94169
}
95170

171+
172+
/// Converts RGBA color from gamma to linear color space using fast approximation
173+
174+
/// \param RGBA - Gamma color value in the range [0, 1]
175+
/// \return Linear color value in the range [0, 1]
96176
inline float4 FastSRGBAToLinear(const float4& SRGBA)
97177
{
98178
return float4{FastGammaToLinear(SRGBA.r), FastGammaToLinear(SRGBA.g), FastGammaToLinear(SRGBA.b), SRGBA.a};

0 commit comments

Comments
 (0)