vsg 1.1.10
VulkanSceneGraph library
Loading...
Searching...
No Matches
CoordinateSpace.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2024 Chris Djali
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/maths/color.h>
16
17namespace vsg
18{
19
20 enum class CoordinateSpace
21 {
22 NO_PREFERENCE = 0,
23 LINEAR = (1 << 0),
24 sRGB = (1 << 1)
25 };
26 VSG_type_name(vsg::CoordinateSpace);
27
28 template<typename T>
29 constexpr T linear_to_sRGB(T c)
30 {
31 constexpr T cutoff = static_cast<T>(0.04045 / 12.92);
32 constexpr T linearFactor = static_cast<T>(12.92);
33 constexpr T nonlinearFactor = static_cast<T>(1.055);
34 constexpr T exponent = static_cast<T>(1.0 / 2.4);
35 if (c <= cutoff)
36 return c * linearFactor;
37 else
38 return std::pow(c, exponent) * nonlinearFactor - static_cast<T>(0.055);
39 }
40
41 template<typename T>
42 constexpr T sRGB_to_linear(T c)
43 {
44 constexpr T cutoff = static_cast<T>(0.04045);
45 constexpr T linearFactor = static_cast<T>(1.0 / 12.92);
46 constexpr T nonlinearFactor = static_cast<T>(1.0 / 1.055);
47 constexpr T exponent = static_cast<T>(2.4);
48 if (c <= cutoff)
49 return c * linearFactor;
50 else
51 return std::pow((c + static_cast<T>(0.055)) * nonlinearFactor, exponent);
52 }
53
54 template<typename T>
55 constexpr t_vec3<T> linear_to_sRGB(const t_vec3<T>& src)
56 {
57 return t_vec3<T>(linear_to_sRGB(src.r), linear_to_sRGB(src.g), linear_to_sRGB(src.b));
58 }
59
60 template<typename T>
61 constexpr t_vec4<T> linear_to_sRGB(const t_vec4<T>& src)
62 {
63 return t_vec4<T>(linear_to_sRGB(src.r), linear_to_sRGB(src.g), linear_to_sRGB(src.b), src.a);
64 }
65
66 template<typename T>
67 constexpr t_vec3<T> linear_to_sRGB(T r, T g, T b)
68 {
69 return t_vec3<T>(linear_to_sRGB(r), linear_to_sRGB(g), linear_to_sRGB(b));
70 }
71
72 template<typename T>
73 constexpr t_vec3<T> linear_to_sRGB(T r, T g, T b, T a)
74 {
75 return t_vec4<T>(linear_to_sRGB(r), linear_to_sRGB(g), linear_to_sRGB(b), a);
76 }
77
78 template<typename T>
79 constexpr t_vec3<T> sRGB_to_linear(const t_vec3<T>& src)
80 {
81 return t_vec3<T>(sRGB_to_linear(src.r), sRGB_to_linear(src.g), sRGB_to_linear(src.b));
82 }
83
84 template<typename T>
85 constexpr t_vec4<T> sRGB_to_linear(const t_vec4<T>& src)
86 {
87 return t_vec4<T>(sRGB_to_linear(src.r), sRGB_to_linear(src.g), sRGB_to_linear(src.b), src.a);
88 }
89
90 template<typename T>
91 constexpr t_vec3<T> sRGB_to_linear(T r, T g, T b)
92 {
93 return t_vec3<T>(sRGB_to_linear(r), sRGB_to_linear(g), sRGB_to_linear(b));
94 }
95
96 template<typename T>
97 constexpr t_vec4<T> sRGB_to_linear(T r, T g, T b, T a)
98 {
99 return t_vec4<T>(sRGB_to_linear(r), sRGB_to_linear(g), sRGB_to_linear(b), a);
100 }
101
102 template<typename T>
103 void convert(T& data, CoordinateSpace source, CoordinateSpace target)
104 {
105 if (source == CoordinateSpace::sRGB && target == CoordinateSpace::LINEAR)
106 data = sRGB_to_linear(data);
107 else if (source == CoordinateSpace::LINEAR && target == CoordinateSpace::sRGB)
108 data = linear_to_sRGB(data);
109 }
110
111 template<typename T>
112 void convert(size_t num, T* data, CoordinateSpace source, CoordinateSpace target)
113 {
114 if (source == CoordinateSpace::sRGB && target == CoordinateSpace::LINEAR)
115 for (size_t i = 0; i < num; ++i) data[i] = sRGB_to_linear(data[i]);
116 else if (source == CoordinateSpace::LINEAR && target == CoordinateSpace::sRGB)
117 for (size_t i = 0; i < num; ++i) data[i] = linear_to_sRGB(data[i]);
118 }
119
120 extern VSG_DECLSPEC VkFormat uNorm_to_sRGB(VkFormat format);
121 extern VSG_DECLSPEC VkFormat sRGB_to_uNorm(VkFormat format);
122
123} // namespace vsg
t_vec3 template class that represents a 3D vector
Definition vec3.h:34
t_vec4 template class that represents a 4D vector
Definition vec4.h:35