|
1 | 1 | /* |
2 | | - * Copyright 2019-2023 Diligent Graphics LLC |
| 2 | + * Copyright 2019-2025 Diligent Graphics LLC |
3 | 3 | * Copyright 2015-2019 Egor Yusov |
4 | 4 | * |
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
27 | 27 |
|
28 | 28 | #pragma once |
29 | 29 |
|
| 30 | +/// \file |
| 31 | +/// Color conversion functions |
| 32 | + |
30 | 33 | #include <cmath> |
31 | 34 | #include "../../../Primitives/interface/BasicTypes.h" |
32 | 35 | #include "../../../Common/interface/BasicMath.hpp" |
33 | 36 |
|
34 | 37 | DILIGENT_BEGIN_NAMESPACE(Diligent) |
35 | 38 |
|
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 |
37 | 45 | inline float LinearToGamma(float x) |
38 | 46 | { |
39 | 47 | return x <= 0.0031308 ? x * 12.92f : 1.055f * std::pow(x, 1.f / 2.4f) - 0.055f; |
40 | 48 | } |
41 | 49 |
|
| 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] |
42 | 55 | inline float GammaToLinear(float x) |
43 | 56 | { |
44 | 57 | return x <= 0.04045f ? x / 12.92f : std::pow((x + 0.055f) / 1.055f, 2.4f); |
45 | 58 | } |
46 | 59 |
|
| 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] |
47 | 65 | 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] |
48 | 72 | float GammaToLinear(Uint8 x); |
49 | 73 |
|
| 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] |
50 | 79 | inline float FastLinearToGamma(float x) |
51 | 80 | { |
52 | 81 | return x < 0.0031308f ? 12.92f * x : 1.13005f * sqrtf(std::abs(x - 0.00228f)) - 0.13448f * x + 0.005719f; |
53 | 82 | } |
54 | 83 |
|
| 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] |
55 | 89 | inline float FastGammaToLinear(float x) |
56 | 90 | { |
57 | 91 | // http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html |
58 | 92 | return x * (x * (x * 0.305306011f + 0.682171111f) + 0.012522878f); |
59 | 93 | } |
60 | 94 |
|
| 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] |
61 | 100 | inline float3 LinearToSRGB(const float3& RGB) |
62 | 101 | { |
63 | 102 | return float3{LinearToGamma(RGB.r), LinearToGamma(RGB.g), LinearToGamma(RGB.b)}; |
64 | 103 | } |
65 | 104 |
|
| 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 |
66 | 112 | inline float4 LinearToSRGBA(const float4& RGBA) |
67 | 113 | { |
68 | 114 | return float4{LinearToGamma(RGBA.r), LinearToGamma(RGBA.g), LinearToGamma(RGBA.b), RGBA.a}; |
69 | 115 | } |
70 | 116 |
|
| 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] |
71 | 122 | inline float3 FastLinearToSRGB(const float3& RGB) |
72 | 123 | { |
73 | 124 | return float3{FastLinearToGamma(RGB.r), FastLinearToGamma(RGB.g), FastLinearToGamma(RGB.b)}; |
74 | 125 | } |
75 | 126 |
|
| 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 |
76 | 134 | inline float4 FastLinearToSRGBA(const float4& RGBA) |
77 | 135 | { |
78 | 136 | return float4{FastLinearToGamma(RGBA.r), FastLinearToGamma(RGBA.g), FastLinearToGamma(RGBA.b), RGBA.a}; |
79 | 137 | } |
80 | 138 |
|
| 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] |
81 | 144 | inline float3 SRGBToLinear(const float3& SRGB) |
82 | 145 | { |
83 | 146 | return float3{GammaToLinear(SRGB.r), GammaToLinear(SRGB.g), GammaToLinear(SRGB.b)}; |
84 | 147 | } |
85 | 148 |
|
| 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 |
86 | 156 | inline float4 SRGBAToLinear(const float4& SRGBA) |
87 | 157 | { |
88 | 158 | return float4{GammaToLinear(SRGBA.r), GammaToLinear(SRGBA.g), GammaToLinear(SRGBA.b), SRGBA.a}; |
89 | 159 | } |
90 | 160 |
|
| 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] |
91 | 166 | inline float3 FastSRGBToLinear(const float3& SRGB) |
92 | 167 | { |
93 | 168 | return float3{FastGammaToLinear(SRGB.r), FastGammaToLinear(SRGB.g), FastGammaToLinear(SRGB.b)}; |
94 | 169 | } |
95 | 170 |
|
| 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] |
96 | 176 | inline float4 FastSRGBAToLinear(const float4& SRGBA) |
97 | 177 | { |
98 | 178 | return float4{FastGammaToLinear(SRGBA.r), FastGammaToLinear(SRGBA.g), FastGammaToLinear(SRGBA.b), SRGBA.a}; |
|
0 commit comments