vsg  1.1.0
VulkanSceneGraph library
color.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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/vec4.h>
16 
17 namespace vsg
18 {
19  template<typename T>
20  constexpr T color_cast(float r, float g, float b, float a)
21  {
22  return {r, g, b, a};
23  }
24 
25  template<>
26  constexpr float color_cast<float>(float r, float, float, float)
27  {
28  return r;
29  }
30 
31  template<>
32  constexpr vec2 color_cast<vec2>(float r, float g, float, float)
33  {
34  return {r, g};
35  }
36 
37  template<>
38  constexpr vec3 color_cast<vec3>(float r, float g, float b, float)
39  {
40  return {r, g, b};
41  }
42 
43  template<>
44  constexpr vec4 color_cast<vec4>(float r, float g, float b, float a)
45  {
46  return {r, g, b, a};
47  }
48 
49  template<>
50  constexpr ubvec4 color_cast<ubvec4>(float r, float g, float b, float a)
51  {
52  return {static_cast<uint8_t>(r * 255.0f), static_cast<uint8_t>(g * 255.0f), static_cast<uint8_t>(b * 255.0f), static_cast<uint8_t>(a * 255.0f)};
53  }
54 
55  template<typename T>
56  constexpr T transparent_black()
57  {
58  return color_cast<T>(0.0f, 0.0f, 0.0f, 0.0f);
59  }
60 
61  template<typename T>
62  constexpr T opaque_black()
63  {
64  return color_cast<T>(0.0f, 0.0f, 0.0f, 1.0f);
65  }
66 
67  template<typename T>
68  constexpr T transparent_white()
69  {
70  return color_cast<T>(1.0f, 1.0f, 1.0f, 0.0f);
71  }
72 
73  template<typename T>
74  constexpr T opaque_white()
75  {
76  return color_cast<T>(0.0f, 0.0f, 1.0f, 1.0f);
77  }
78 
79  template<typename T>
80  constexpr T color_cast(VkBorderColor borderColor)
81  {
82  switch (borderColor)
83  {
84  case (VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK):
85  case (VK_BORDER_COLOR_INT_TRANSPARENT_BLACK):
86  return transparent_black<T>();
87  case (VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK):
88  case (VK_BORDER_COLOR_INT_OPAQUE_BLACK):
89  return opaque_black<T>();
90  case (VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE):
91  case (VK_BORDER_COLOR_INT_OPAQUE_WHITE):
92  return opaque_white<T>();
93  default:
94  // not supported, fallback to VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
95  return transparent_black<T>();
96  }
97  }
98 
99  template<typename T>
100  constexpr t_vec4<T> linear_to_sRGB(const t_vec4<T>& src)
101  {
102  const T exponent = 2.2;
103  return t_vec4<T>(std::pow(src.r, exponent), std::pow(src.g, exponent), std::pow(src.b, exponent), src.a);
104  }
105 
106  template<typename T>
107  constexpr t_vec4<T> linear_to_sRGB(T r, T g, T b, T a)
108  {
109  const T exponent = 2.2;
110  return t_vec4<T>(std::pow(r, exponent), std::pow(g, exponent), std::pow(b, exponent), a);
111  }
112 
113  template<typename T>
114  constexpr t_vec4<T> sRGB_to_linear(const t_vec4<T>& src)
115  {
116  const T exponent = 1.0 / 2.2;
117  return t_vec4<T>(std::pow(src.r, exponent), std::pow(src.g, exponent), std::pow(src.b, exponent), src.a);
118  }
119 
120  template<typename T>
121  constexpr t_vec4<T> sRGB_to_linear(T r, T g, T b, T a)
122  {
123  const T exponent = 1.0 / 2.2;
124  return t_vec4<T>(std::pow(r, exponent), std::pow(g, exponent), std::pow(b, exponent), a);
125  }
126 
127 } // namespace vsg