colorspace.frag 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
31
32
33
// The weights of RGB contributions to luminance.
// Should sum to unity.
const vec3 HCYwts_ = vec3(0.299, 0.587, 0.114);

34
35
36
37
float HCLgamma_ = 3.0;
float HCLy0_ = 100;
float HCLmaxL_ = 0.530454533953517;

38
/**
39
40
41
42
 * Converts pure Hue to RGB
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   H   Pure hue
 * \return  The pure hue converted to RGB.
43
 */
44
45
46
47
48
49
vec3 hue2rgb(in float H) {
    float R = abs(H * 6.0 - 3.0) - 1.0;
    float G = 2.0 - abs(H * 6.0 - 2.0);
    float B = 2.0 - abs(H * 6.0 - 4.0);
    return clamp(vec3(R,G,B), 0.0, 1.0);
}
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * Converts RGB color plus chromacity and value to hue value.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   RGB     color in RGB space
 * \param   C       Chromacity
 * \param   V       Value
 * \return  The corresponding hue.
 */
float rgbcv2hue(in vec3 RGB, in float C, in float V) {
    vec3 delta = (V - RGB) / C;
    delta.rgb -= delta.brg;
    delta.rgb += vec3(2.0, 4.0, 6.0);
    delta.brg = step(V, RGB) * delta.brg;
    float H;
    H = max(delta.r, max(delta.g, delta.b));
    return fract(H / 6.0);
}
68

69
70
71
72
73
74
75
76
77
78
/**
 * Converts a color from HSV space to RGB space.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   HSV    color in HSV space
 * \return  The given color in RGB space.
 */
vec3 hsv2rgb(in vec3 HSV) {
    vec3 RGB = hue2rgb(HSV.x);
    return ((RGB - 1.0) * HSV.y + 1.0) * HSV.z;
}
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * Converts a color from HSL space to RGB space.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   HSL    color in HSL space
 * \return  The given color in RGB space.
 */
vec3 hsl2rgb(in vec3 HSL) {
    vec3 RGB = hue2rgb(HSL.x);
    float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;
    return (RGB - 0.5) * C + HSL.z;
}
   
/**
 * Converts a color from HCY space to RGB space.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   HCY    color in HCY space
 * \return  The given color in RGB space.
 */
vec3 hcy2rgb(in vec3 HCY) {
    vec3 RGB = hue2rgb(HCY.x);
    float Z = dot(RGB, HCYwts_);
    if (HCY.z < Z) {
        HCY.y *= HCY.z / Z;
    }
    else if (Z < 1.0) {
        HCY.y *= (1.0 - HCY.z) / (1 - Z);
106
    }
107
108
    return (RGB - vec3(Z, Z, Z)) * HCY.y + HCY.z;
}
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

vec3 hcl2rgb(in vec3 HCL)
{
  vec3 RGB = vec3(0.0, 0.0, 0.0);
  if (HCL.z != 0.0) {
    float H = HCL.x;
    float C = HCL.y;
    float L = HCL.z * HCLmaxL_;
    float Q = exp((1.0 - C / (2.0 * L)) * (HCLgamma_ / HCLy0_));
    float U = (2.0 * L - C) / (2.0 * Q - 1.0);
    float V = C / Q;
    float T = tan((H + min(fract(2.0 * H) / 4.0, fract(-2.0 * H) / 8.0)) * 6.283185307);
    H *= 6.0;
    if (H <= 1.0) {
      RGB.r = 1.0;
      RGB.g = T / (1.0 + T);
    }
    else if (H <= 2.0) {
      RGB.r = (1.0 + T) / T;
      RGB.g = 1.0;
    }
    else if (H <= 3.0) {
      RGB.g = 1.0;
      RGB.b = 1.0 + T;
    }
    else if (H <= 4.0) {
      RGB.g = 1.0 / (1.0 + T);
      RGB.b = 1.0;
    }
    else if (H <= 5.0) {
      RGB.r = -1.0 / T;
      RGB.b = 1.0;
    }
    else {
      RGB.r = 1.0;
      RGB.b = -T;
    }
    return RGB * V + U;
  }
  return RGB;
}

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * Converts a color from RGB space to HSV space.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   RGB    color in RGB space
 * \return  The given color in HSV space.
 */
vec3 rgb2hsv(in vec3 RGB) {
    vec3 HSV = vec3(0.0, 0.0, 0.0);
    HSV.z = max(RGB.r, max(RGB.g, RGB.b));
    float M = min(RGB.r, min(RGB.g, RGB.b));
    float C = HSV.z - M;
    if (C != 0) {
        HSV.x = rgbcv2hue(RGB, C, HSV.z);
        HSV.y = C / HSV.z;
    }
167
168
169
170
    return HSV;
}

/**
171
172
173
174
 * Converts a color from RGB space to HSL space.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   RGB    color in RGB space
 * \return  The given color in HSL space.
175
 */
176
177
178
179
180
181
182
183
184
185
186
187
vec3 rgb2hsl(in vec3 RGB) {
    vec3 HSL = vec3(0.0, 0.0, 0.0);
    float U, V;
    U = -min(RGB.r, min(RGB.g, RGB.b));
    V = max(RGB.r, max(RGB.g, RGB.b));
    HSL.z = (V - U) * 0.5;
    float C = V + U;
    if (C != 0) {
        HSL.x = rgbcv2hue(RGB, C, V);
        HSL.y = C / (1.0 - abs(2.0 * HSL.z - 1.0));
    }
    return HSL;
188
189
190
}


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
 * Converts a color from RGB space to HCY space.
 * Using the efficient implementation from http://www.chilliant.com/rgb2hsv.html.
 * \param   RGB    color in RGB space
 * \return  The given color in HCY space.
 */
vec3 rgb2hcy(in vec3 RGB) {
    vec3 HCY = vec3(0.0, 0.0, 0.0);
    float U, V;
    U = -min(RGB.r, min(RGB.g, RGB.b));
    V = max(RGB.r, max(RGB.g, RGB.b));
    HCY.y = V + U;
    HCY.z = dot(RGB, HCYwts_);
    if (HCY.y != 0) {
        HCY.x = rgbcv2hue(RGB, HCY.y, V);
        float Z = dot(hue2rgb(HCY.x), HCYwts_);
        if (HCY.z > Z) {
            HCY.z = 1 - HCY.z;
            Z = 1 - Z;
        }
        HCY.y *= Z / HCY.z;
    }
    return HCY;
}

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
vec3 rgb2hcl(in vec3 RGB) {
  vec3 HCL = vec3(0.0, 0.0, 0.0);
  float H = 0.0;
  float U, V;
  U = -min(RGB.r, min(RGB.g, RGB.b));
  V = max(RGB.r, max(RGB.g, RGB.b));
  float Q = HCLgamma_ / HCLy0_;
  HCL.y = V + U;
  if (HCL.y != 0.0)
  {
    H = atan(RGB.g - RGB.b, RGB.r - RGB.g) / 3.14159265;
    Q *= -U / V;
  }
  Q = exp(Q);
  HCL.x = fract(H / 2.0 - min(fract(H), fract(-H)) / 6.0);
  HCL.y *= Q;
  HCL.z = mix(U, V, Q) / (HCLmaxL_ * 2.0);
  return HCL;
}

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * Converts a color from RGB space to CIEXYZ space.
 * \see     http://wiki.labomedia.org/images/1/10/Orange_Book_-_OpenGL_Shading_Language_2nd_Edition.pdf
 * \param   colorRGB    color in RGB space
 * \return  The given color in CIEXYZ space.
 */
vec3 rgb2ciexyz(in vec3 colorRGB) {
    const mat3 conversionMatrix = mat3(
        0.412453, 0.212671, 0.019334,
        0.357580, 0.715160, 0.119193,
        0.180423, 0.072169, 0.950227);
    return colorRGB * conversionMatrix;
}

/**
 * Converts a color from CIEXYZ space to RGB space.
 * \see     http://wiki.labomedia.org/images/1/10/Orange_Book_-_OpenGL_Shading_Language_2nd_Edition.pdf
 * \param   colorCIEXYZ   color in CIEXYZ space
 * \return  The given color in RGB space.
 */
vec3 ciexyz2rgb(in vec3 colorCIEXYZ) {
    const mat3 conversionMatrix = mat3(
        3.240479, -0.969256, 0.055648,
        -1.537150, 1.875992, -0.204043,
        -0.498535, 0.041556, 1.057311);
    return colorCIEXYZ * conversionMatrix;
}
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

vec3 rgb2tsl(in vec3 RGB) {
    const float twoPiRCP_ = 0.15915494309;
    const float oneThird = 1.0 / 3.0;
    vec3 TSL = vec3(0.0, 0.0, 0.0);
    float sum = RGB.r + RGB.g + RGB.b;
    float rs = (RGB.r / sum) - oneThird;
    float gs = (RGB.g / sum) - oneThird;

    if (gs > 0)
        TSL.x = twoPiRCP_ * atan(rs/gs + 0.25);
    else if (gs < 0)
        TSL.x = twoPiRCP_ * atan(rs/gs + 0.75);

    TSL.y = sqrt(9.0 * (rs*rs + gs*gs) / 5.0);

    TSL.z = dot(RGB, HCYwts_);
    return TSL;
};

vec3 tsl2rgb(in vec3 TSL) {
    vec3 RGB = vec3(0.0, 0.0, 0.0);
    float x = -1.0 / tan(TSL.x * 6.283185307);
    
    if (TSL.x == 0.0)
        RGB.g = 0.0;
    else if (TSL.x > 0.5)
        RGB.g = TSL.y * -sqrt(5.0/(9.0 * (x*x + 1.0)));
    else if (TSL.x < 0.5)
        RGB.g = TSL.y * sqrt(5.0/(9.0 * (x*x + 1.0)));
    
    if (TSL.x == 0)
        RGB.r = TSL.y * sqrt(5.0)/3.0;
    else
        RGB.r = x * RGB.g + (1.0/3.0);

    RGB.b = 1.0 - RGB.r - RGB.g;
    float k = 1.0 / (0.185*RGB.r + 0.473*RGB.b + 0.114);
    return k * RGB;
}

// ================================================================================================

float lab_helper_F(in float p) {
    if (p<0.008856)
        return p*(841.0/108.0) + (4.0/29.0);
    return pow(p,1.0/3.0);
}


vec3 xyz2lab(in vec3 XYZ) {
    float fX = XYZ.x/0.950456;
    float fY = XYZ.y/1.0;
    float fZ = XYZ.z/1.088754;
    fX = lab_helper_F(fX);
    fY = lab_helper_F(fY);
    fZ = lab_helper_F(fZ);
    return vec3(116.0 * fY - 16.0,
                500.0 * (fX - fY),
                200.0 * (fY - fZ));
}

float lab_helper_invF(in float p) {
326
    float r = pow(p, 3.0);
327
    if (r < 0.008856)
328
        return (p - 4.0/29.0)*(108.0/841.0);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    else
        return r;
}

vec3 lab2xyz(in vec3 LAB) {
    float Y = (LAB.x + 16.0)/116.0;
    float X = Y + LAB.y/500.0;
    float Z = Y - LAB.z/200.0;
    X = 0.950456 * lab_helper_invF(X);
    Y = 1.0 * lab_helper_invF(Y);
    Z = 1.088754 * lab_helper_invF(Z);
    return vec3(X, Y, Z);
}

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// ================================================================================================

const float lab_epsilon_ = 0.008856;
const float lab_kappa_ = 903.3;
const float pi_ = 3.14159265;
const float two_pi_ = 6.2831853;

float lab_helper_F2(in float p) {
    if (p > lab_epsilon_)
        return pow(p, 1.0/3.0);
    else
        return (lab_kappa_ * p + 16.0) / 116.0;
}

vec3 xyz2lab2(in vec3 XYZ) {
    float fX = XYZ.x/0.950456;
    float fY = XYZ.y/1.0;
    float fZ = XYZ.z/1.088754;
    fX = lab_helper_F2(fX);
    fY = lab_helper_F2(fY);
    fZ = lab_helper_F2(fZ);
    return vec3(116.0 * fY - 16.0,
                500.0 * (fX - fY),
                200.0 * (fY - fZ));
}

float lab_helper_invF2(in float p) {
    float r = p*p*p;
    if (r < 0.008856)
        return (p-4.0/29.0)*(108.0/841.0);
    else
        return r;
}

vec3 lab2xyz2(in vec3 LAB) {
    float yr = (LAB.x > lab_kappa_ * lab_epsilon_) ? pow((LAB.x + 16.0) / 116.0, 3.0) : LAB.x / lab_kappa_;

    float fy = (yr > lab_epsilon_) ? (LAB.x + 16.0) / 116.0 : (lab_kappa_ * yr + 16.0) / 116.0;
    float fx = LAB.y / 500.0 + fy;
    float fz = fy - LAB.z / 200.0;

    float xr = (pow(fx, 3.0) > lab_epsilon_) ? pow(fx, 3.0) : (fx * 116.0 - 16.0) / lab_kappa_;
    float zr = (pow(fz, 3.0) > lab_epsilon_) ? pow(fz, 3.0) : (fz * 116.0 - 16.0) / lab_kappa_;

    return vec3(
        0.950456 * xr,
        1.0 * yr,
        1.088754 * zr);
}

vec3 lab2lch(in vec3 LAB) {
    LAB.y = sqrt(pow(LAB.y, 2.0) + pow(LAB.z, 2.0));
    LAB.z = atan(LAB.z, LAB.y);

    LAB.z = mod(LAB.z, two_pi_);

    return LAB;
}

vec3 lch2lab(in vec3 LCH) {
    float c = LCH.y;
    LCH.y = cos(LCH.z) * c;
    LCH.z = sin(LCH.z) * c;
    return LCH;
}

409
410
411
412
413
414
vec3 rgb2lab(in vec3 RGB) {
    return xyz2lab(rgb2ciexyz(RGB));
}

vec3 lab2rgb(in vec3 LAB) {
    return ciexyz2rgb(lab2xyz(LAB));
415
416
417
418
419
420
421
422
}

vec3 rgb2lch(in vec3 RGB) {
    return lab2lch(rgb2lab(RGB));
}

vec3 lch2rgb(in vec3 LCH) {
    return lab2rgb(lch2lab(LCH));
423
}