ColorTransform.cpp
Go to the documentation of this file.
1 #include "ColorTransform.hpp"
2 
4 #include <cmath>
5 
6 namespace iv
7 {
8 
10 {
11  return glm::translate( float4x4( 1 ), float3( radians, 0, 0 ) );
12 }
13 
15 {
16  return glm::translate( float4x4( 1 ), float3( 0, shift, 0 ) );
17 }
18 
20 {
21  return glm::scale( float4x4( 1 ), float3( 1, 1, scale ) );
22 }
23 
25 {
26  return glm::translate( float4x4( 1 ), float3( 0, 0, shift ) );
27 }
28 
30 {
31  float hfrom = ColorTransform::GetHue( from );
32  float hto = ColorTransform::GetHue( to );
33  return ColorTransform::RotateHue( hto - hfrom );
34 }
35 
37 {
38  float cfrom = ColorTransform::GetSaturation( from );
39  float cto = ColorTransform::GetSaturation( to );
40  return ColorTransform::ShiftSaturation( cto - cfrom );
41 }
42 
44 {
45  float lfrom = ColorTransform::GetValue( from );
46  float lto = ColorTransform::GetValue( to );
47  return ColorTransform::ScaleValue( lto / lfrom );
48 }
49 
51 {
52  float4 from_hsv = linearRGB_to_HSV( sRGB_to_linearRGB( from ) );
53  float4 to_hsv = linearRGB_to_HSV( sRGB_to_linearRGB( to ) );
54  return ColorTransform::RotateHue( to_hsv.x - from_hsv.x ) * ColorTransform::ShiftSaturation( to_hsv.y - from_hsv.y ) * ColorTransform::ScaleValue( to_hsv.z / from_hsv.z );
55 }
56 
58 {
59  return glm::scale( float4x4( 1 ), float3( 0, 0, 0 ) );
60 }
61 
63 {
65  return glm::scale( float4x4( 1 ), float3( val.x, val.y, val.z ) );
66 }
67 
69 {
71  return glm::translate( float4x4( 1 ), float3( val.x, val.y, val.z ) );
72 }
73 
75 {
77 }
78 
80 {
82 }
83 
85 {
87 }
88 
90 {
91  float4 incolor_hsv = linearRGB_to_HSV( sRGB_to_linearRGB( color ) );
92  float4 outcolor_hsv = transform * incolor_hsv;
93  return linearRGB_to_sRGB( HSV_to_linearRGB( outcolor_hsv ) );
94 }
95 
97 {
98  if( val <= 0.0404482362771082f )
99  return val / 12.92f;
100  else
101  return std::pow( ( val + 0.055f ) / 1.055f, 2.4f );
102 }
103 
105 {
106  return float4( sRGB_to_linearRGB( val.x ), sRGB_to_linearRGB( val.y ), sRGB_to_linearRGB( val.z ), val.w );
107 }
108 
110 {
111  if( val <= 0.00313066844250063f )
112  return val * 12.92f;
113  else
114  return 1.055f * std::pow( val, 1.0f / 2.4f ) - 0.055f;
115 }
116 
118 {
119  return float4( linearRGB_to_sRGB( val.x ), linearRGB_to_sRGB( val.y ), linearRGB_to_sRGB( val.z ), val.w );
120 }
121 
123 {
124  float R = rgb.x;
125  float G = rgb.y;
126  float B = rgb.z;
127 
128  float Min = std::min( R, std::min( G, B ) );
129  float Max = std::max( R, std::max( G, B ) );
130 
131  float PI = 3.1415926535f;
132  float V = Max;
133  float C = Max - Min;
134  float S;
135  if( V == 0.0f )
136  S = 0.0f;
137  else
138  S = C / V;
139 
140  float H;
141 
142  if( C == 0.0f )
143  H = 0.0f;
144  else if( V == R )
145  H = PI / 3.0f * ( 0.0f + ( G - B ) / C );
146  else if( V == G )
147  H = PI / 3.0f * ( 2.0f + ( B - R ) / C );
148  else
149  H = PI / 3.0f * ( 4.0f + ( R - G ) / C );
150 
151  return float4( H, S, V, rgb.w );
152 }
153 
155 {
156  float H = hsv.x;
157  float S = std::clamp( hsv.y, 0.0f, 1.0f );
158  float V = std::clamp( hsv.z, 0.0f, 1.0f );
159 
160  float PI = 3.1415926535f;
161  float C = V * S;
162  float Hn = sig_fmod( H, 2.0f * PI ) / ( PI / 3.0f );
163  float X = C * ( 1.0f - std::abs( std::fmod( Hn, 2.0f ) - 1.0f ) );
164 
165  float m = V - C;
166  float R = m;
167  float G = m;
168  float B = m;
169 
170  if( Hn <= 1.0f )
171  {
172  R += C;
173  G += X;
174  }
175  else if( Hn <= 2.0f )
176  {
177  G += C;
178  R += X;
179  }
180  else if( Hn <= 3.0f )
181  {
182  G += C;
183  B += X;
184  }
185  else if( Hn <= 4.0f )
186  {
187  B += C;
188  G += X;
189  }
190  else if( Hn <= 5.0f )
191  {
192  B += C;
193  R += X;
194  }
195  else if( Hn <= 6.0f )
196  {
197  R += C;
198  B += X;
199  }
200 
201  return float4( R, G, B, hsv.w );
202 }
203 
204 }