Hue Preserving Color Blending
functions.cginc
Go to the documentation of this file.
1 
5 float3 RGBtoHSV(float3 c)
9 {
10  float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
11  float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
12  float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
13 
14  float d = q.x - min(q.w, q.y);
15  float e = 1.0e-10;
16  return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
17 }
18 
22 float3 HSVtoRGB(float3 c)
23 {
24  float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
25  float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
26  return float3(c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y));
27 }
28 
31 float3 oppositeColor(float3 color1, float3 color2)
32 {
33  float3 color1hsv = RGBtoHSV(color1);
34  float3 color2hsv = RGBtoHSV(color2);
35  float3 oppositeColor = float3(fmod(((color1hsv.x * 360) + 180), 360) / 360, color2hsv.yz);
36 
37  return HSVtoRGB(oppositeColor);
38 }
39 
41 bool equalHue(float3 color1, float3 color2)
42 {
43  float3 color1hsv = RGBtoHSV(color1);
44  float3 color2hsv = RGBtoHSV(color2);
45 
46  if (abs(color1hsv.x - color2hsv.x) < 0.05)
47  return true;
48  else
49  return false;
50 }
51 
53 float3 huePreservingColorAdd(float3 color1, float3 color2)
54 {
55  if (equalHue(color1, color2))
56  return color1 + color2;
57 
58  float3 color2Prime = oppositeColor(color1, color2);
59  float3 result = color1 + color2Prime;
60 
61  // If color1 was the dominant color, the result should now be the same hue as color1.
62  // If this isn't the case, switch colors.
63  if (!equalHue(color1, result))
64  {
65  float3 color1Prime = oppositeColor(color2, color1);
66  result = color1Prime + color2;
67  }
68 
69  return result;
70 }
71 
72 /*
73 float3 huePreservingColorAddBias(float3 color1, float3 color2, float bias)
74 {
75  if (equalHue(color1, color2))
76  return color1 + color2;
77 
78  float3 color2Prime = oppositeColor(color1, color2);
79  float3 result = color1 + color2Prime;
80 
81  if (!equalHue(color1, result))
82  {
83  float3 color1Prime = oppositeColor(color2, color1);
84  result = lerp(color1Prime + color2, color2, bias);
85  } else {
86  result = lerp(color1 + color2Prime, color1, bias);
87  }
88 
89  return result;
90 }
91 */
92 
93 /*
94 float4 blendFrontToBack(float4 src, float4 dest)
95 {
96  // Front to back blending:
97  // GL_ONE_MINUS_DST_ALPHA, GL_ONE
98 
99  //finalColor.rgb = finalColor.rgb + (1.0 - finalColor.a) * color.rgb;
100  //finalColor.a = finalColor.a + (1.0 - finalColor.a) * color.a;
101 
102  src.rgb *= src.a;
103  dest = (1.0 - dest.a) * src + dest;
104 
105  return dest;
106 }
107 
108 float4 blendBackToFront(float4 src, float4 dest)
109 {
110  src.rgb *= src.a;
111  dest = src + (1.0 - src.a) * dest;
112 
113  //dest = src.a * src + (1.0 - src.a) * dest;
114 
115  return dest;
116 }
117 */
float3 HSVtoRGB(float3 c)
Definition: functions.cginc:22
float3 huePreservingColorAdd(float3 color1, float3 color2)
Hue preserving blending function from paper.
Definition: functions.cginc:53
float3 RGBtoHSV(float3 c)
Definition: functions.cginc:8
bool equalHue(float3 color1, float3 color2)
Checking if the difference between two hues is less than threshold 0.05.
Definition: functions.cginc:41
float3 oppositeColor(float3 color1, float3 color2)
Definition: functions.cginc:31