-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolourblind.h
387 lines (339 loc) · 14.6 KB
/
colourblind.h
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
106
107
108
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
/* ISC License Copyright (c) 2018 Andrew Reece
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* TODO:
* - Achromatopsia
* - Add working ColourDifference with Luv
*/
#ifdef __cplusplus
extern "C" {
#endif
#ifdef I_PREFER_THE_AMERICAN_SPELLING_OF_COLOR_BECAUSE_I_AM_SILLY
#define Colo_rblind Colorblind
#define Colo_rblind255 Colorblind255
#define Colo_rblindRGB255 ColorblindRGB255
#define Colo_rblindRGB ColorblindRGB
#else/*I_PREFER_THE_AMERICAN_SPELLING_OF_COLOR_BECAUSE_I_AM_SILLY*/
#define Colo_rblind Colourblind
#define Colo_rblind255 Colourblind255
#define Colo_rblindRGB255 ColourblindRGB255
#define Colo_rblindRGB ColourblindRGB
#endif/*I_PREFER_THE_AMERICAN_SPELLING_OF_COLOR_BECAUSE_I_AM_SILLY*/
/******************************************************************************
* Types
*******/
typedef struct cb_rgb_255 cb_rgb_255;
typedef struct cb_rgb cb_rgb;
typedef enum cb_impairment cb_impairment;
typedef enum cb_guideline cb_guideline;
/******************************************************************************
* Constants
***********/
char *cbImpairmentStrings[];
char *cbGuidelineStrings[];
float cbGuidelineScores[];
/******************************************************************************
* Function Prototypes
*********************/
/* Conversions from normal rgb values to their colourblind equivalent for simulating different impairments.
* The conversion depends on the cone that you are simulating as non-functioning.
* All pointers are used as in and out variables */
/* Missing the first/long-wave/red cone - Red-Green colourblind */
void Protanopia(float *Red, float *Green, float *Blue);
void Protanopia255(unsigned char *R, unsigned char *G, unsigned char *B);
cb_rgb ProtanopiaRGB(cb_rgb RGB);
cb_rgb_255 ProtanopiaRGB255(cb_rgb_255 RGB);
cb_rgb_255 ProtanopiaRGB255Gamma(cb_rgb_255 RGB);
/* Missing the second/medium-wave/green cone - Red-Green colourblind
* This is the most common form of colourblindness */
void Deuteranopia(float *Red, float *Green, float *Blue);
void Deuteranopia255(unsigned char *R, unsigned char *G, unsigned char *B);
cb_rgb DeuteranopiaRGB(cb_rgb RGB);
cb_rgb_255 DeuteranopiaRGB255(cb_rgb_255 RGB);
cb_rgb_255 DeuteranopiaRGB255Gamma(cb_rgb_255 RGB);
/* Missing the third/short-wave/blue cone - Blue-Yellow colourblind */
void Tritanopia(float *Red, float *Green, float *Blue);
void Tritanopia255(unsigned char *R, unsigned char *G, unsigned char *B);
cb_rgb TritanopiaRGB(cb_rgb RGB);
cb_rgb_255 TritanopiaRGB255(cb_rgb_255 RGB);
cb_rgb_255 TritanopiaRGB255Gamma(cb_rgb_255 RGB);
/* Applies the above impairment simulation (or no-op) based on the enum value given */
void Colo_rblind(cb_impairment Impairment, float *R, float *G, float *B);
void Colo_rblind255(cb_impairment Impairment, unsigned char *R, unsigned char *G, unsigned char *B);
cb_rgb Colo_rblindRGB(cb_impairment Impairment, cb_rgb RGB);
cb_rgb_255 Colo_rblindRGB255(cb_impairment Impairment, cb_rgb_255 RGB);
/* WCAG-defined contrast: (L_H+0.5) / (L_L+0.5) */
/* Results range from 1 (the same colour) to 21 (white with black) */
float cbContrast(float RA, float GA, float BA, float RB, float GB, float BB);
float cbContrast255(unsigned char RA, unsigned char GA, unsigned char BA, unsigned char RB, unsigned char GB, unsigned char BB);
float cbContrastRGB(cb_rgb A, cb_rgb B);
float cbContrastRGB255(cb_rgb_255 A, cb_rgb_255 B);
/* ISO 9241-3 contrast modulation: (L_H-L_L) / (L_H+L_L) */
float cbContrastModulation(float RA, float GA, float BA, float RB, float GB, float BB);
float cbContrastModulation255(unsigned char RA, unsigned char GA, unsigned char BA, unsigned char RB, unsigned char GB, unsigned char BB);
float cbContrastModulationRGB(cb_rgb A, cb_rgb B);
float cbContrastModulationRGB255(cb_rgb_255 A, cb_rgb_255 B);
/* ISO 9241-3 contrast ratio: L_H / L_L (divides by zero for pure black) */
float cbContrastRatio(float RA, float GA, float BA, float RB, float GB, float BB);
float cbContrastRatio255(unsigned char RA, unsigned char GA, unsigned char BA, unsigned char RB, unsigned char GB, unsigned char BB);
float cbContrastRatioRGB(cb_rgb A, cb_rgb B);
float cbContrastRatioRGB255(cb_rgb_255 A, cb_rgb_255 B);
/* Gives a 'lightness' value for comparing colours */
float cbLuminance(float R, float G, float B);
float cbLuminance255(unsigned char R, unsigned char G, unsigned char B);
float cbLuminanceRGB(cb_rgb RGB);
float cbLuminanceRGB255(cb_rgb_255 RGB);
/* Convert between 0-255 and 0-1 */
cb_rgb cbNorm(cb_rgb_255 RGB);
cb_rgb_255 cbDenorm(cb_rgb RGB);
/* Convert from Linear to sRGB */
void cbApplyGamma(float *R, float *G, float *B);
cb_rgb cbApplyGammaRGB(cb_rgb RGB);
/* Convert from sRGB to Linear */
void cbRemoveGamma(float *R, float *G, float *B);
cb_rgb cbRemoveGammaRGB(cb_rgb RGB);
#ifdef cbIMPLEMENTATION
typedef struct cb_rgb_255 {
unsigned char R; /* Red */
unsigned char G; /* Green */
unsigned char B; /* Blue */
} cb_rgb_255;
typedef struct cb_rgb {
float R; /* Red */
float G; /* Green */
float B; /* Blue */
} cb_rgb;
typedef enum cb_impairment {
cbUnimpaired, cbProtanopia, cbDeuteranopia, cbTritanopia,
cbImpairmentCount,
cbRedGreenDim = cbProtanopia, cbRedGreen = cbDeuteranopia, cbBlueYellow = cbTritanopia,
cbMissingRed = cbProtanopia, cbMissingGreen = cbDeuteranopia, cbMissingBlue = cbTritanopia,
} cb_impairment;
char *cbImpairmentStrings[] =
{ "Unimpaired", "Protanopia", "Deuteranopia", "Tritanopia" };
#define COL_GUIDELINES \
COL_GUIDELINE(ISO9241_3, ContrastRatio, Pass, >=, 3.0) \
COL_GUIDELINE(WCAG, Contrast, AALarge, >=, 4.0) \
COL_GUIDELINE(WCAG, Contrast, AAALarge, >=, 4.5) \
COL_GUIDELINE(WCAG, Contrast, AA, >=, 4.5) \
COL_GUIDELINE(WCAG, Contrast, AAA, >=, 7.0) \
COL_GUIDELINE(ISO9241_3, ContrastModulation, Pass, >=, 0.5) \
#define COL_GUIDELINE(source, testname, rating, comparison, value) cb## source ##_## testname ##_## rating,
typedef enum cb_guideline { COL_GUIDELINES cgGuidelineCount } cb_guideline;
#undef COL_GUIDELINE
#define COL_GUIDELINE(source, testname, rating, comparison, value) #source " " #testname " " #rating,
char *cbGuidelineStrings[] = { COL_GUIDELINES };
#undef COL_GUIDELINE
#define COL_GUIDELINE(source, testname, rating, comparison, value) value,
float cbGuidelineScores[] = { COL_GUIDELINES };
#undef COL_GUIDELINE
/* assumes already normalized to 0-1 */
#ifdef cbGAMMA_FASTER
#ifndef cbSQRT
#include <math.h> /* sqrt, pow */
#define cbSQRT sqrt
#endif/*cbSQRT*/
#define cbApplyGammaComponent(X) ((float)cbSQRT(X))
#define cbRemoveGammaComponent(X) ((X) * (X))
#else /* cbGAMMA_FASTER */
#ifndef cbPOW
#include <math.h> /* sqrt, pow */
#define cbPOW pow
#endif/*cbPOW*/
#ifdef cbGAMMA_FAST
#define cbApplyGammaComponent(X) ((float)cbPOW(X, 0.454545454545454545454545454545454545454545))
#define cbRemoveGammaComponent(X) ((float)cbPOW(X, 2.2))
#else /* cbGAMMA_FAST */
#define cbApplyGammaComponent(X) \
((float)(X > 0.00313080495356037151702786377709 ? \
1.055 * cbPOW((X), 0.4166666666) - 0.055 : \
X * 12.92))
#define cbRemoveGammaComponent(X) \
((float)(X > 0.04045 ? \
cbPOW((X + 0.055) / 1.055, 2.4) : \
X / 12.92))
#endif /* cbGAMMA_FAST */
#endif /* cbGAMMA_FASTER */
#define cbNormComponent(X) ((float)(X) / 255.f)
#define cbDenormComponent(X) ((unsigned char)((X) * 255.f + 0.5f))
#define cbGamma(dir) \
void dir##Gamma(float *R, float *G, float *B) { \
float r = *R, g = *G, b = *B; \
*R=dir##GammaComponent(r), *G=dir##GammaComponent(g), *B=dir##GammaComponent(b); \
} \
cb_rgb dir##GammaRGB(cb_rgb RGB) { \
cb_rgb Result = \
{ dir##GammaComponent(RGB.R), dir##GammaComponent(RGB.G), dir##GammaComponent(RGB.B) }; \
return Result; \
}
cbGamma(cbApply)
cbGamma(cbRemove)
#undef cbGamma
#define cbDomain(name, in, out) \
out name(in RGB) { \
out Result = \
{ name##Component(RGB.R), name##Component(RGB.G), name##Component(RGB.B) }; \
return Result; \
}
cbDomain(cbNorm, cb_rgb_255, cb_rgb)
cbDomain(cbDenorm, cb_rgb, cb_rgb_255)
#undef cbDomain
/* Luminances */
float cbLuminance(float R, float G, float B) {
R = cbRemoveGammaComponent(R);
G = cbRemoveGammaComponent(G);
B = cbRemoveGammaComponent(B);
float Result = 0.2126f*R + 0.7152f*G + 0.0722f*B;
return Result;
}
float cbLuminance255(unsigned char R, unsigned char G, unsigned char B)
{ return cbLuminance(cbNormComponent(R), cbNormComponent(G), cbNormComponent(B)); }
float cbLuminanceRGB(cb_rgb RGB)
{ return cbLuminance(RGB.R, RGB.G, RGB.B); }
float cbLuminanceRGB255(cb_rgb_255 RGB)
{ return cbLuminance(cbNormComponent(RGB.R), cbNormComponent(RGB.G), cbNormComponent(RGB.B)); }
/* prevents division by 0 */
float cbContrast(float RA, float GA, float BA, float RB, float GB, float BB) {
float LumA = cbLuminance(RA, GA, BA);
float LumB = cbLuminance(RB, GB, BB);
float High = LumA, Low = LumB;
if(High < Low)
{ High = LumB, Low = LumA; }
/* from http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef */
float Ratio = (High + 0.05f) / (Low + 0.05f);
return Ratio;
}
float cbContrastRatio(float RA, float GA, float BA, float RB, float GB, float BB) {
float LumA = cbLuminance(RA, GA, BA);
float LumB = cbLuminance(RB, GB, BB);
float High = LumA, Low = LumB;
if(High < Low)
{ High = LumB, Low = LumA; }
float Ratio = High / Low;
return Ratio;
}
float cbContrastModulation(float RA, float GA, float BA, float RB, float GB, float BB) {
float LumA = cbLuminance(RA, GA, BA);
float LumB = cbLuminance(RB, GB, BB);
float High = LumA, Low = LumB;
if (High == Low) /* for black */
{ return 0; }
else if (High < Low) {
High = LumB;
Low = LumA;
}
float Top = High - Low;
float Bottom = High + Low;
return Top/Bottom;
}
#define cbOTHER_VERSIONS(fn) \
float fn##255(unsigned char RA, unsigned char GA, unsigned char BA, unsigned char RB, unsigned char GB, unsigned char BB) \
{ return fn(cbNormComponent(RA), cbNormComponent(GA), cbNormComponent(BA), \
cbNormComponent(RB), cbNormComponent(GB), cbNormComponent(BB)); } \
float fn##RGB(cb_rgb A, cb_rgb B) \
{ return fn(A.R, A.G, A.B, B.R, B.G, B.B); } \
float fn##RGB255(cb_rgb_255 A, cb_rgb_255 B) \
{ return fn(cbNormComponent(A.R), cbNormComponent(A.G), cbNormComponent(A.B), \
cbNormComponent(B.R), cbNormComponent(B.G), cbNormComponent(B.B)); }
cbOTHER_VERSIONS(cbContrast)
cbOTHER_VERSIONS(cbContrastRatio)
cbOTHER_VERSIONS(cbContrastModulation)
#undef cbOTHER_VERSIONS
/******************************************************************************
* Colourblindness
*****************/
/* http://ixora.io/projects/colorblindness/color-blindness-simulation-research/ */
void Protanopia(float *Red, float *Green, float *Blue) {
float R = *Red, G = *Green, B = *Blue;
*Red = 0.17055699213417f*R + 0.82944301379913f*G + 2.91188E-9f *B;
*Green = 0.17055699092998f*R + 0.82944300785005f*G - 5.98679E-10f*B;
*Blue = -0.00451714424166f*R + 0.00451714427397f*G + B;
}
void Deuteranopia(float *Red, float *Green, float *Blue) {
float R = *Red, G = *Green, B = *Blue;
*Red = 0.33066007266046f*R + 0.66933992517563f*G + 3.559314E-9f*B;
*Green = 0.33066007387760f*R + 0.66933992719147f*G - 1.758327E-9f*B;
*Blue = -0.02785538261323f*R + 0.02785538252318f*G + B;
}
void Tritanopia(float *Red, float *Green, float *Blue) {
float R = *Red, G = *Green, B = *Blue;
*Red = R + 0.12739886310880f*G - 0.12739886341072f*B;
*Green = -4.486E-11f *R + 0.87390929928361f*G + 0.12609070101523f*B;
*Blue = 3.1113E-10f*R + 0.87390929725848f*G + 0.12609070067115f*B;
}
/* assumes value in 0-1 */
#define cbNOPIA(nopia) \
cb_rgb nopia ##RGB(cb_rgb RGB) { \
nopia(&RGB.R, &RGB.G, &RGB.B); \
return RGB; \
} \
/* take and return rgb as 0-255 */ \
cb_rgb_255 nopia ##RGB255(cb_rgb_255 RGB) {\
cb_rgb RGBNorm = cbNorm(RGB);\
nopia(&RGBNorm.R, &RGBNorm.G, &RGBNorm.B); \
cb_rgb_255 Result = cbDenorm(RGBNorm);\
return Result;\
} \
/* take and return gamma-corrected rgb as 0-255 */ \
cb_rgb_255 nopia ##RGB255Gamma(cb_rgb_255 RGB) {\
cb_rgb RGBNorm = cbRemoveGammaRGB(cbNorm(RGB)); \
nopia(&RGBNorm.R, &RGBNorm.G, &RGBNorm.B); \
cb_rgb_255 Result = cbDenorm(cbApplyGammaRGB(RGBNorm)); \
return Result;\
} \
void nopia ##255(unsigned char *R, unsigned char *G, unsigned char *B) { \
float Rf = cbNormComponent(*R), Gf = cbNormComponent(*G), Bf = cbNormComponent(*B); \
nopia(&Rf, &Gf, &Bf); \
*R = cbDenormComponent(Rf); \
*G = cbDenormComponent(Gf); \
*B = cbDenormComponent(Bf); \
}
cbNOPIA(Protanopia)
cbNOPIA(Deuteranopia)
cbNOPIA(Tritanopia)
#undef cbNOPIA
cb_rgb Colo_rblindRGB(cb_impairment Impairment, cb_rgb RGB) {
switch(Impairment) {
case cbProtanopia: return ProtanopiaRGB( RGB);
case cbDeuteranopia: return DeuteranopiaRGB(RGB);
case cbTritanopia: return TritanopiaRGB( RGB);
default: return RGB;
}
}
cb_rgb_255 Colo_rblindRGB255(cb_impairment Impairment, cb_rgb_255 RGB) {
switch(Impairment) {
case cbProtanopia: return ProtanopiaRGB255( RGB);
case cbDeuteranopia: return DeuteranopiaRGB255(RGB);
case cbTritanopia: return TritanopiaRGB255( RGB);
default: return RGB;
}
}
void Colo_rblind255(cb_impairment Impairment, unsigned char *R, unsigned char *G, unsigned char *B) {
switch(Impairment) {
case cbProtanopia: Protanopia255( R, G, B); break;
case cbDeuteranopia: Deuteranopia255(R, G, B); break;
case cbTritanopia: Tritanopia255( R, G, B);
}
}
void Colo_rblind(cb_impairment Impairment, float *R, float *G, float *B) {
switch(Impairment) {
case cbProtanopia: Protanopia( R, G, B); break;
case cbDeuteranopia: Deuteranopia(R, G, B); break;
case cbTritanopia: Tritanopia( R, G, B);
}
}
#endif/* cbIMPLEMENTATION */
#ifdef __cplusplus
}
#endif