vsg 1.1.10
VulkanSceneGraph library
Loading...
Searching...
No Matches
color.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2022 Robert Osfield
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/vec4.h>
16
17namespace 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} // namespace vsg