CommonLibVR
ColorUtil.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RE/N/NiColor.h"
4 
5 namespace RE
6 {
7  namespace ColorUtil
8  {
9  enum class BLEND_MODE
10  {
11  kDarken = 0,
12  kMultiply,
13  kColorBurn,
16 
17  kLighten,
18  kScreen,
22 
23  kOverlay,
24  kSoftLight,
25  kHardLight,
28  kPinLight,
29  kHardMix,
30 
32  kExclusion,
33  kSubtract,
34  kDivide
35  };
36 
37  namespace detail
38  {
39  //https://www.shadertoy.com/view/XdS3RW
40  //ben, 2013
41 
42  inline NiColor abs(const NiColor& a_rhs)
43  {
44  return NiColor(std::abs(a_rhs.red),
45  std::abs(a_rhs.green),
46  std::abs(a_rhs.blue));
47  }
48 
49  inline NiColor clamp(const NiColor& a_rhs, float a_min, float a_max)
50  {
51  return NiColor(std::clamp(a_rhs.red, a_min, a_max),
52  std::clamp(a_rhs.green, a_min, a_max),
53  std::clamp(a_rhs.blue, a_min, a_max));
54  }
55 
56  inline NiColor floor(const NiColor& a_rhs)
57  {
58  return NiColor(std::floor(a_rhs.red),
59  std::floor(a_rhs.green),
60  std::floor(a_rhs.blue));
61  }
62 
63  inline NiColor min(const NiColor& a_lhs, const NiColor& a_rhs)
64  {
65  return NiColor(
66  std::min(a_lhs.red, a_rhs.red),
67  std::min(a_lhs.green, a_rhs.green),
68  std::min(a_lhs.blue, a_rhs.blue));
69  }
70 
71  inline NiColor max(const NiColor& a_lhs, const NiColor& a_rhs)
72  {
73  return NiColor(
74  std::max(a_lhs.red, a_rhs.red),
75  std::max(a_lhs.green, a_rhs.green),
76  std::max(a_lhs.blue, a_rhs.blue));
77  }
78 
79  // BLEND MODES
80 
81  inline NiColor darken(const NiColor& a_src, const NiColor& a_dest)
82  {
83  return min(a_src, a_dest);
84  }
85 
86  inline NiColor multiply(const NiColor& a_src, const NiColor& a_dest)
87  {
88  return a_src * a_dest;
89  }
90 
91  inline NiColor colorBurn(const NiColor& a_src, const NiColor& a_dest)
92  {
93  return 1.0 - (1.0 - a_dest) / a_src;
94  }
95 
96  inline NiColor linearBurn(const NiColor& a_src, const NiColor& a_dest)
97  {
98  return a_src + a_dest - 1.0;
99  }
100 
101  inline NiColor darkerColor(const NiColor& a_src, const NiColor& a_dest)
102  {
103  return (a_src.red + a_src.green + a_src.blue < a_dest.red + a_dest.green + a_dest.blue) ? a_src : a_dest;
104  }
105 
106  inline NiColor lighten(const NiColor& a_src, const NiColor& a_dest)
107  {
108  return max(a_src, a_dest);
109  }
110 
111  inline NiColor screen(const NiColor& a_src, const NiColor& a_dest)
112  {
113  return a_src + a_dest - a_src * a_dest;
114  }
115 
116  inline NiColor colorDodge(const NiColor& a_src, const NiColor& a_dest)
117  {
118  return a_dest / (1.0 - a_src);
119  }
120 
121  inline NiColor linearDodge(const NiColor& a_src, const NiColor& a_dest)
122  {
123  return a_src + a_dest;
124  }
125 
126  inline NiColor lighterColor(const NiColor& a_src, const NiColor& a_dest)
127  {
128  return (a_src.red + a_src.green + a_src.blue > a_dest.red + a_dest.green + a_dest.blue) ? a_src : a_dest;
129  }
130 
131  inline float overlay(float a_src, float a_dest)
132  {
133  return (a_dest < 0.5f) ? 2.0f * a_src * a_dest : 1.0f - 2.0f * (1.0f - a_src) * (1.0f - a_dest);
134  }
135 
136  inline NiColor overlay(const NiColor& a_src, const NiColor& a_dest)
137  {
138  return NiColor(overlay(a_src.red, a_dest.red), overlay(a_src.green, a_dest.green), overlay(a_src.blue, a_dest.blue));
139  }
140 
141  inline float softLight(float a_src, float a_dest)
142  {
143  return (a_src < 0.5f) ? a_dest - (1.0f - 2.0f * a_src) * a_dest * (1.0f - a_dest) : (a_dest < 0.25f) ? a_dest + (2.0f * a_src - 1.0f) * a_dest * ((16.0f * a_dest - 12.0f) * a_dest + 3.0f) :
144  a_dest + (2.0f * a_src - 1.0f) * (sqrt(a_dest) - a_dest);
145  }
146 
147  inline NiColor softLight(const NiColor& a_src, const NiColor& a_dest)
148  {
149  return NiColor(softLight(a_src.red, a_dest.red), softLight(a_src.green, a_dest.green), softLight(a_src.blue, a_dest.blue));
150  }
151 
152  inline float hardLight(float a_src, float a_dest)
153  {
154  return (a_src < 0.5f) ? 2.0f * a_src * a_dest : 1.0f - 2.0f * (1.0f - a_src) * (1.0f - a_dest);
155  }
156 
157  inline NiColor hardLight(const NiColor& a_src, const NiColor& a_dest)
158  {
159  return NiColor(hardLight(a_src.red, a_dest.red), hardLight(a_src.green, a_dest.green), hardLight(a_src.blue, a_dest.blue));
160  }
161 
162  inline float vividLight(float a_src, float a_dest)
163  {
164  return (a_src < 0.5f) ? 1.0f - (1.0f - a_dest) / (2.0f * a_src) : a_dest / (2.0f * (1.0f - a_src));
165  }
166 
167  inline NiColor vividLight(const NiColor& a_src, const NiColor& a_dest)
168  {
169  return NiColor(vividLight(a_src.red, a_dest.red), vividLight(a_src.green, a_dest.green), vividLight(a_src.blue, a_dest.blue));
170  }
171 
172  inline NiColor linearLight(const NiColor& a_src, const NiColor& a_dest)
173  {
174  return 2.0 * a_src + a_dest - 1.0;
175  }
176 
177  inline float pinLight(float a_src, float a_dest)
178  {
179  return (2.0f * a_src - 1.0f > a_dest) ? 2.0f * a_src - 1.0f : (a_src < 0.5f * a_dest) ? 2.0f * a_src :
180  a_dest;
181  }
182 
183  inline NiColor pinLight(const NiColor& a_src, const NiColor& a_dest)
184  {
185  return NiColor(pinLight(a_src.red, a_dest.red), pinLight(a_src.green, a_dest.green), pinLight(a_src.blue, a_dest.blue));
186  }
187 
188  inline NiColor hardMix(const NiColor& a_src, const NiColor& a_dest)
189  {
190  return floor(a_src + a_dest);
191  }
192 
193  inline NiColor difference(const NiColor& a_src, const NiColor& a_dest)
194  {
195  return abs(a_dest - a_src);
196  }
197 
198  inline NiColor exclusion(const NiColor& a_src, const NiColor& a_dest)
199  {
200  return a_src + a_dest - 2.0 * a_src * a_dest;
201  }
202 
203  inline NiColor subtract(const NiColor& a_src, const NiColor& a_dest)
204  {
205  return a_src - a_dest;
206  }
207 
208  inline NiColor divide(const NiColor& a_src, const NiColor& a_dest)
209  {
210  return a_src / a_dest;
211  }
212 
213  inline NiColor mix(const NiColor& a_src, const NiColor& a_dest, float a_alpha)
214  {
215  return a_src * (1 - a_alpha) + a_dest * a_alpha;
216  }
217  }
218 
219  NiColor Blend(const NiColor& a_src, const NiColor& a_dest, BLEND_MODE a_mode, float a_alpha);
220 
221  float CalcLuminance(const NiColor& a_src);
222 
223  NiColor Mix(const NiColor& a_src, const NiColor& a_dest, float a_percentage);
224  }
225 }
Definition: NiColor.h:11
float red
Definition: NiColor.h:274
float green
Definition: NiColor.h:275
float blue
Definition: NiColor.h:276
NiColor floor(const NiColor &a_rhs)
Definition: ColorUtil.h:56
NiColor abs(const NiColor &a_rhs)
Definition: ColorUtil.h:42
NiColor multiply(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:86
NiColor linearBurn(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:96
NiColor hardMix(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:188
NiColor colorBurn(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:91
NiColor screen(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:111
NiColor lighterColor(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:126
NiColor min(const NiColor &a_lhs, const NiColor &a_rhs)
Definition: ColorUtil.h:63
float vividLight(float a_src, float a_dest)
Definition: ColorUtil.h:162
NiColor clamp(const NiColor &a_rhs, float a_min, float a_max)
Definition: ColorUtil.h:49
float softLight(float a_src, float a_dest)
Definition: ColorUtil.h:141
NiColor divide(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:208
NiColor mix(const NiColor &a_src, const NiColor &a_dest, float a_alpha)
Definition: ColorUtil.h:213
NiColor subtract(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:203
NiColor linearDodge(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:121
NiColor darkerColor(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:101
NiColor colorDodge(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:116
NiColor max(const NiColor &a_lhs, const NiColor &a_rhs)
Definition: ColorUtil.h:71
NiColor linearLight(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:172
float pinLight(float a_src, float a_dest)
Definition: ColorUtil.h:177
NiColor exclusion(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:198
NiColor difference(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:193
float hardLight(float a_src, float a_dest)
Definition: ColorUtil.h:152
NiColor lighten(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:106
NiColor darken(const NiColor &a_src, const NiColor &a_dest)
Definition: ColorUtil.h:81
float overlay(float a_src, float a_dest)
Definition: ColorUtil.h:131
float CalcLuminance(const NiColor &a_src)
BLEND_MODE
Definition: ColorUtil.h:10
NiColor Mix(const NiColor &a_src, const NiColor &a_dest, float a_percentage)
NiColor Blend(const NiColor &a_src, const NiColor &a_dest, BLEND_MODE a_mode, float a_alpha)
Definition: AbsorbEffect.h:6