-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlibbrewscheme.mata
1458 lines (998 loc) · 43 KB
/
libbrewscheme.mata
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* *
* libbrewscheme *
* *
* A collection of Mata classes, methods, functions, and wrappers used by *
* programs in the brewscheme package. This includes classes/methods used to *
* provide simulated RGB values for different forms of color blindness, and *
* utility programs used to search for/look up RGB values used in the different *
* programs in the package/toolkit/suite of programs. *
* *
* Code adopted from: *
* *
* Wickline, M. (2014). Color.Vision.Simulate. version 0.1. Retrieved from: *
* galacticmilk.com/labs/Color-Vision/Javascript/Color.Vision.Simulate.js *
* Retrieved on: 24nov2015 *
* *
* Code also references work of: *
* *
* Meyer, G. W., & Greenberg, D. P. (1988). Color-Defective Vision and Computer *
* Graphics Displays. Computer Graphics and Applications, IEEE 8(5), pp. 28-40. *
* *
* Smith, V. C., & Pokorny, J. (2005). Spectral sensitivity of the foveal cone *
* photopigments between 400 and 500nm. Journal of the Optical Society of *
* America A, 22(10) pp. 2060-2071. *
* *
* Lindbloom, B. (). RGB working space information. Retrieved from: *
* http://www.brucelindbloom.com. Retrieved on 24nov2015. *
* *
* Version: 1.1.1 *
* Distribution Date: 03JUN2017 *
* *
*******************************************************************************/
// Change to Mata interpreter/compiler
mata:
// Define a generic class to store color blindness constants and methods
class cbtype {
// Private members
private:
// Constants used to rescale RGB values for different forms of colorblindness
real scalar x, y, m, yint
// Publicly accessible members
public:
// Accessor methods for the constants
real scalar getConfuseX(), getConfuseY(), getConfuseM(),
getConfuseYint()
// Setter methods for the constants
void setConfuseX(), setConfuseY(), setConfuseM(),
setConfuseYint()
} // End of base class definition
// Accessor for the x member variable
real scalar cbtype::getConfuseX() {
// Returns the x member variable value
return(this.x)
} // End of Accessor method definition
// Accessor for the y member variable
real scalar cbtype::getConfuseY() {
// Returns the y member variable value
return(this.y)
} // End of Accessor method definition
// Accessor for the slope member variable
real scalar cbtype::getConfuseM() {
// Returns the slope member variable value
return(this.m)
} // End of Accessor method definition
// Accessor for the intercept member variable
real scalar cbtype::getConfuseYint() {
// Returns the intercept member variable value
return(this.yint)
} // End of Accessor method definition
// Method used to set the value of the member variable x
void cbtype::setConfuseX(real scalar xarg) {
// Set the value of the member variable x to the value passed to the method
this.x = xarg
} // End of Setter method definition
// Method used to set the value of the member variable y
void cbtype::setConfuseY(real scalar yarg) {
// Set the value of the member variable y to the value passed to the method
this.y = yarg
} // End of Setter method definition
// Method used to set the value of the slope member variable
void cbtype::setConfuseM(real scalar slope) {
// Set the value of the member variable slope to the value passed to the method
this.m = slope
} // End of Setter method definition
// Method used to set the value of the intercept member variable
void cbtype::setConfuseYint(real scalar intercept) {
// Set the value of the member variable intercept to the value passed to the method
this.yint = intercept
} // End of Setter method definition
/* Class for Protanopia (Red Colorblindness) inherits from cbtype;
the constructor will set the values of the parent class and will implement
the getter methods to retrieve those values. */
class Protanopia extends cbtype {
// Public Methods
public:
// Defines the constructor method
void new()
} // End of class definition for Protanopia
// Protanopia class constructor method
void Protanopia::new() {
// Sets the x member variable's value
super.setConfuseX(0.7465)
// Sets the y member variable's value
super.setConfuseY(0.2535)
// Sets the slope member variable's value
super.setConfuseM(1.273463)
// Sets the intercept variable's value
super.setConfuseYint(-0.073894)
} // End of constructor method for red-colorblindness class
/* Class for Dueteranopia (Green Colorblindness) inherits from cbtype;
the constructor will set the values of the parent class and will implement
the getter methods to retrieve those values. */
class Deuteranopia extends cbtype {
// Public Methods
public:
// Defines the constructor method
void new()
} // End of class definition for Dueteranopia
// Deuteranopia class constructor method
void Deuteranopia::new() {
// Sets the x member variable's value
super.setConfuseX(1.4)
// Sets the y member variable's value
super.setConfuseY(-0.4)
// Sets the slope member variable's value
super.setConfuseM(0.968437)
// Sets the intercept variable's value
super.setConfuseYint(0.003331)
} // End of constructor method for green-colorblindness class
/* Class for Tritanopia (Blue Colorblindness) inherits from cbtype;
the constructor will set the values of the parent class and will implement
the getter methods to retrieve those values. */
class Tritanopia extends cbtype {
// Public Methods
public:
// Defines the constructor method
void new()
} // End of class definition for Tritanopia
// Tritanopia class constructor method
void Tritanopia::new() {
// Sets the x member variable's value
super.setConfuseX(0.1748)
// Sets the y member variable's value
super.setConfuseY(0.0)
// Sets the slope member variable's value
super.setConfuseM(0.062921)
// Sets the intercept variable's value
super.setConfuseYint(0.292119)
} // End of constructor method for blue-colorblindness class
// Class that implements transformations of RGB values and returns values that
// simulate various types of colorblindness.
class colorblind {
// Private member variables
private:
// A row vector with the types of colorblindness
string rowvector types, typelabs
// The initial RGB values, amount parameter, gamma correction parameter, and
// inverse gamma correction parameter
real scalar inputR, inputG, inputB, amount, gamma,
invgamma
// Member variable that stores the transformed RGB values
real matrix transformedRGB
// Class with constants defined for Red color blindness transformations
class Protanopia scalar protanope
// Class with constants defined for Green color blindness transformations
class Deuteranopia scalar deuteranope
// Class with constants defined for Blue color blindness transformations
class Tritanopia scalar tritanope
// Public member variables
public:
// Setter methods, class constructor, and simulation method
void new(), achromatopsia(), setR(),
setG(), setB(), setAmount(), setRGB(),
simulate()
// Getter methods for member variables/returning results to users
real scalar getR(), getG(), getB(), getGamma(),
getInvGamma(), getConfuseX(),
getConfuseY(), getConfuseM(),
getConfuseYint(), getAmount(), condMax()
// Method to return the string names of the available types
string rowvector getTypes(), getTypeLabs()
// Returns the name of a single type of color blindness
string scalar getType(), getTypeLab()
// Returns a matrix with the transformed RGB values
real matrix getTransformedRgbs()
// Methods that return a single row of numeric data
real rowvector getTransformedRgb(), checkRange()
// Method used to return the string representation of the transformed RGB
void getRgbString()
// Method used to return the string representation of all transformed RGB values
void getRgbStrings()
// Member variables used to store data transformed throughout the simulation
// method call
real matrix drgb1, drgb2, drgb3, drgb4, drgb5,
powrgb, xyz, chroma, sim, diffrgb,
fitrgb, trnsconstants
} // End of Class definition
// Class constructor method
void colorblind::new() {
// Initialize the color blindness class objects
this.protanope = Protanopia()
this.deuteranope = Deuteranopia()
this.tritanope = Tritanopia()
// Initialize the matrix used to store the transformation results
this.transformedRGB = J(5, 3, .)
// Store the types of colorblindness for macro names
this.types = ("baseline", "achromatopsia", "protanopia", "deuteranopia", "tritanopia")
// Store the types of colorblindness
this.typelabs = ("Normal Vision", "Achromatopsic Vision", ///
"Protanopic Vision", "Deuteranopic Vision", "Tritanopic Vision")
// Store prototype values for amount, gamma, and inverse gamma parameters
this.amount = 1
this.gamma = 2.2
this.invgamma = 1/2.2
// Initialize null matrices used to store data created in the simulation method
this.drgb1 = J(5, 3, .)
this.powrgb = J(5, 3, .)
this.xyz = J(5, 3, .)
this.chroma = J(5, 2, .)
this.sim = J(5, 8, .)
this.diffrgb = J(5, 5, .)
this.drgb2 = J(5, 3, .)
this.drgb3 = J(5, 4, .)
this.drgb4 = J(5, 3, .)
this.drgb5 = J(5, 3, .)
this.fitrgb = J(5, 3, .)
this.trnsconstants = J(5, 4, .)
// Set null baseline values for matrices
this.drgb1[1, 1..3] = (0, 0, 0)
this.powrgb[1, 1..3] = (0, 0, 0)
this.xyz[1, 1..3] = (0, 0, 0)
this.chroma[1, 1..2] = (0, 0)
this.sim[1, 1..8] = (0, 0, 0, 0, 0, 0, 0, 0)
this.diffrgb[1, 1..5] = (0, 0, 0, 0, 0)
this.drgb2[1, 1..3] = (0, 0, 0)
this.drgb3[1, 1..4] = (0, 0, 0, 0)
this.drgb4[1, 1..3] = (0, 0, 0)
this.drgb5[1, 1..3] = (0, 0, 0)
this.fitrgb[1, 1..3] = (0, 0, 0)
this.trnsconstants[1, 1..4] = (0, 0, 0, 0)
} // End Colorblind class constructor definition
// Method to set the amount member variable
void colorblind::setAmount(real scalar amt) {
// Changes the default amount parameter value to the value passed to the method
this.amount = amt
} // End of Setter method
// Sets the Initial value of the red channel
void colorblind::setR(real scalar red) {
// Sets the inputR member variable to the value passed to the method
this.inputR = red
// Set first element of transform matrix to baseline red channel
this.transformedRGB[1, 1] = red
} // End of Setter method
// Sets the Initial value of the green channel
void colorblind::setG(real scalar green) {
// Sets the inputG member variable to the value passed to the method
this.inputG = green
// Set first element of transform matrix to baseline green channel
this.transformedRGB[1, 2] = green
} // End of Setter method
// Sets the Initial value of the blue channel
void colorblind::setB(real scalar blue) {
// Sets the inputB member variable to the value passed to the method
this.inputB = blue
// Set first element of transform matrix to baseline blue channel
this.transformedRGB[1, 3] = blue
} // End of Setter method
// Sets the Initial values for the red, green, and blue channels
void colorblind::setRGB(real scalar red, real scalar green, real scalar blue) {
// Set the red channel
setR(red)
// Set the green channel
setG(green)
// Set the blue channel
setB(blue)
} // End of Setter method declaration
// Gets the red channel parameter value
real scalar colorblind::getR() {
// Returns the user specified red channel value
return(this.inputR)
} // End Accessor method declaration for red channel input
// Gets the green channel parameter value
real scalar colorblind::getG() {
// Returns the user specified green channel value
return(this.inputG)
} // End Accessor method declaration for green channel input
// Gets the blue channel parameter value
real scalar colorblind::getB() {
// Returns the user specified blue channel value
return(this.inputB)
} // End Accessor method declaration for blue channel input
// Gets the gamma correction parameter value
real scalar colorblind::getGamma() {
// Returns the gamma correction parameter value
return(this.gamma)
} // End Accessor method declaration for gamma correction parameter
// Gets the inverse gamma correction parameter value
real scalar colorblind::getInvGamma() {
// Returns the inverse gamma correction parameter value
return(this.invgamma)
} // End Accessor method declaration for inverse gamma correction parameter
// Gets the color blind types member variable
string rowvector colorblind::getTypes() {
// Returns the names of all of the color blindness types
return(this.types)
} // End Accessor method declaration for color blindness types
// Gets the color blind type labels member variable
string rowvector colorblind::getTypeLabs() {
// Returns labels used for returned macros
return(this.typelabs)
} // End Accessor method declaration for color blind type labels
// Gets color blindness type label for specific form of color sight impairment
string scalar colorblind::getTypeLab(real scalar typ) {
// Returns the label to use for graphs/etc...
return(this.typelabs[1, typ])
} // End Accessor method declaration for individual color blindness type label
// Gets a specific type of color blindness
string scalar colorblind::getType(real scalar typ) {
// Returns the name of specified color blindness type
return(this.types[1, typ])
} // End Accessor method declaration for individual color blindness type
// Gets the transformed RGB values for all available transformations requested
real matrix colorblind::getTransformedRgbs() {
// Returns the matrix containing all transformed values
return(this.transformedRGB)
} // End Accessor method declaration for
// Gets a specific transformed RGB value
real rowvector colorblind::getTransformedRgb(real scalar type) {
// Returns the RGB values for a specified transformation
return(this.transformedRGB[type, .])
} // End Accessor method declaration for single transformed RGB value
// Method to return a column vector of transformed RGB values
void colorblind::getRgbStrings() {
// Declares column vector variable to use to build the return value
string matrix x
// Declares variable to use for looping over elements in the transformed matrix
real scalar i
// Initializes the column vector with null strings
x = J(rows(getTransformedRgbs()), 2, "")
// Loop over the rows of the matrix with the transformed results
for(i = 1; i <= rows(getTransformedRgbs()); i++) {
// Set the first element in the row to the name of the vision type
x[i, 1] = getTypeLab(i)
// Populate each element of the column vector with the numeric values
// appended into a single string value
x[i, 2] = strofreal(this.transformedRGB[i, 1]) + " " + ///
strofreal(this.transformedRGB[i, 2]) + " " + ///
strofreal(this.transformedRGB[i, 3])
// Set Stata locals with labels and values
st_local((getType(i) + "lab"), x[i, 1])
// Set Stata locals with values
st_local(getType(i), (x[i, 2]))
} // End Loop over rows of the transformed matrix
} // End of getter method
// Getter method used to retrieve a single RGB string for the specified transform
void colorblind::getRgbString(real scalar type) {
// String local variable used to store the retrieved RGB value for return
string scalar x
// Populate each element of the column vector with the numeric values
// appended into a single string value
x = strofreal(this.transformedRGB[type, 1]) + " " + ///
strofreal(this.transformedRGB[type, 2]) + " " + ///
strofreal(this.transformedRGB[type, 3])
// Set return value with labels and values
st_local((getType(type) + "lab"), getTypeLab(type))
// Set Stata locals with values
st_local(getType(type), x)
} // End of getter method
// Getter method used to access the amount member variable
real scalar colorblind::getAmount() {
// Returns the current value of the amount member variable for the object
return(this.amount)
} // End of getter method
// Getter method used to access the x member variable of the IDd colorblind object
real scalar colorblind::getConfuseX(real scalar type) {
// If value passed to method is 3
if (type == 3) {
// Return the constant for Protanopia transformation
return(this.protanope.getConfuseX())
} // End IF Block for Protanopia
// If value 4 is passed
else if (type == 4) {
// Return the constant for Deuteranopia transformation
return(this.deuteranope.getConfuseX())
} // End ELSEIF Block for Deuteranopia
// If a value of 5 is passed
else if (type == 5) {
// Return the constant for Tritanopia transformation
return(this.tritanope.getConfuseX())
} // End ELSEIF Block for Tritanopia
} // End of getter method for the color blind x variable
// Getter method used to access the y member variable of the IDd colorblind object
real scalar colorblind::getConfuseY(real scalar type) {
// If value passed to method is 3
if (type == 3) {
// Return the constant for Protanopia transformation
return(this.protanope.getConfuseY())
} // End IF Block for Protanopia
// If value 4 is passed
else if (type == 4) {
// Return the constant for Deuteranopia transformation
return(this.deuteranope.getConfuseY())
} // End ELSEIF Block for Deuteranopia
// Otherwise
else if (type == 5) {
// Return the constant for Tritanopia transformation
return(this.tritanope.getConfuseY())
} // End ELSEIF Block for Tritanopia
} // End of getter method for the color blind y variable
// Getter method used to access the slope member variable of the IDd colorblind object
real scalar colorblind::getConfuseM(real scalar type) {
// If value passed to method is 3
if (type == 3) {
// Return the constant for Protanopia transformation
return(this.protanope.getConfuseM())
} // End IF Block for Protanopia
// If value 4 is passed
else if (type == 4) {
// Return the constant for Deuteranopia transformation
return(this.deuteranope.getConfuseM())
} // End ELSEIF Block for Deuteranopia
// If a value of 5 is passed
else if (type == 5) {
// Return the constant for Tritanopia transformation
return(this.tritanope.getConfuseM())
} // End ELSEIF Block for Tritanopia
} // End of getter method for the color blind slope variable
// Getter method used to access the intercept member variable of the IDd colorblind object
real scalar colorblind::getConfuseYint(real scalar type) {
// If value passed to method is 3
if (type == 3) {
// Return the constant for Protanopia transformation
return(this.protanope.getConfuseYint())
} // End IF Block for Protanopia
// If value 4 is passed
else if (type == 4) {
// Return the constant for Deuteranopia transformation
return(this.deuteranope.getConfuseYint())
} // End ELSEIF Block for Deuteranopia
// If a value of 5 is passed
else if (type == 5) {
// Return the constant for Tritanopia transformation
return(this.tritanope.getConfuseYint())
} // End ELSEIF Block for Tritanopia
} // End of getter method for the color blind intercept variable
// Method to ensure that RGB values are within [0, 255]
real rowvector colorblind::checkRange(real rowvector rgb) {
// Row vector to store updated RGB values
real rowvector updateRGB
// Scalar used for iterator in loop below
real scalar i
// Initialized a null row vector to store updated values
updateRGB = J(1, cols(rgb), .)
// Loop over the elements of the row vector
for(i = 1; i <= cols(rgb); i++) {
// If the value is < 0
if (rgb[1, i] < 0) {
// Force value to 0
updateRGB[1, i] = 0
} // End IF Block for values < 0
// If the value is > 255
else if (rgb[1, i] > 255) {
// Force the value to 255
updateRGB[1, i] = 255
} // End ELSEIF Block for values > 255
// Otherwise
else {
// Truncate the value and store only the integer portion of it
updateRGB[1, i] = trunc(rgb[1, i])
} // End ELSE Block for values in [0, 255]
} // End Loop over row vector elements
// Return the updated/validated RGB values
return(updateRGB)
} // End of method to validate range for RGB values
// Total colorblindness method (type == 1)
void colorblind::achromatopsia() {
// Declare local variables for method
real scalar dr, dg, db
// Declare rowvector used to store the transformed RGB values
real rowvector nrgb
// Convert to Monochrome using sRGB WhitePoint
dr = (getR() * 0.212656 + getG() * 0.715158 + getB() * 0.072186)
// Anomylize colors
dr = (getR() * (1.0 - getAmount())) + dr * getAmount()
dg = (getR() * (1.0 - getAmount())) + dr * getAmount()
db = (getR() * (1.0 - getAmount())) + dr * getAmount()
// Pass the values and check that they are in [0, 255]
nrgb = checkRange((dr, dg, db))
// Update the matrix containing the transformed values
this.transformedRGB[2, .] = nrgb
} // End Method used to transform value to complete colorblind values
// Conditional maximum method for simulations
real scalar colorblind::condMax(real rowvector fits) {
// Declares variable used for loop
real scalar i
// Declares variable used to store the new RGB rowvector
real rowvector x
// Initializes a null rowvector
x = J(1, 3, .)
// Loop over the elements of the rowvector passed as an argument
for(i = 1; i <= cols(fits); i++) {
// If the value is [0, 1] use that value
if (fits[1, i] >= 0 & fits[1, i] <= 1) {
// Populate element with passed value
x[1, i] = fits[1, i]
} // End IF Block to check for range in [0, 1]
// If the value is not [0, 1]
else {
// Set a default value of 0
x[1, i] = 0
} // End ELSE Block for values outside of [0, 1]
} // End Loop over elements of rowvector argument
// Return the adjusted rowvector
return(max(x))
} // End method declaration for conditional maximum value
// Simulate colorblind RGB values and populate the transformed matrix with new values
void colorblind::simulate(| real rowvector types) {
// Declares scalar variables local to this method
real scalar dr, dg, db, powr, powg, powb, x, y, z, chromax,
chromay, m, yint, deviatex, deviatey, neutralx,
neutralz, diffx, diffz, diffr, diffg, diffb,
fitr, fitg, fitb, adjust, i, thetype
// Declares row vector variables local to this method
real rowvector tomax, updateRGB
// If no value specified iterate over all values
if (args() == 0) {
// Create a row vector with all possible values
types = (1, 2, 3, 4)
} // End IF Block for optional argument handling
// Add baseline color to color matrix
this.transformedRGB[1, .] = ((getR(), getG(), getB()))
// Loop over the arguments passed to the method
for(i = 1; i <= cols(types); i++) {
// Get the transformation/simulation type
thetype = types[1, i] + 1
// For type = 1 (Full Color Blindness
if (thetype == 2) {
// Transform values for full/complete color blindness
achromatopsia()
} // End IF Block for full color blindness
/* For all other types of color blindness the type codes are:
3 = Protanopia (Red Color Blindness)
4 = Deuteranopia (Green Color Blindness)
5 = Tritanopia (Blue Color Blindness)
*/
else {
// Transform value variables
dr = getR()
dg = getG()
db = getB()
// Stores the starting RGB values for this iteration
this.drgb1[thetype, .] = (dr, dg, db)
// RGB Adjusted for gamma level 2.2
powr = dr^getGamma()
powg = dg^getGamma()
powb = db^getGamma()
// Stores the RGB values raised to Gamma for debugging
this.powrgb[thetype, .] = (powr, powg, powb)
// XYZ color space representation of RGB values
x = powr * 0.412424 + powg * 0.357579 + powb * 0.180464
y = powr * 0.212656 + powg * 0.715158 + powb * 0.0721856
z = powr * 0.0193324 + powg * 0.119193 + powb * 0.950444
// Stores the xyz color space transformation values for debugging
this.xyz[thetype, .] = (x, y, z)
// Convert XYZ into xyY Chromacity Coordinates (xy) and Luminance (Y)
chromax = x / (x + y + z)
chromay = y / (x + y + z)
// Stores the chroma transformation values for debugging
this.chroma[thetype, .] = (chromax, chromay)
// Stores the transformation constants for debugging
this.trnsconstants[thetype, .] = (getConfuseX(thetype),
getConfuseY(thetype), getConfuseM(thetype), getConfuseYint(thetype))
// Generate the "Confusion Line" between the source color and the Confusion Point
// slope of the confusion line
m = (chromay - getConfuseY(thetype)) / (chromax - getConfuseX(thetype))
// Intercept for confusion line
yint = chromay - chromax * m
// How far the xy coords deviate from the simulation
deviatex = (getConfuseYint(thetype) - yint) / (m - getConfuseM(thetype))
deviatey = (m * deviatex) + yint
// Compute the simulated color's XYZ coords
x = deviatex * y / deviatey
z = (1.0 - (deviatex + deviatey)) * y / deviatey
// Neutral grey calculated from luminance (in D65)
neutralx = 0.312713 * y / 0.329016
neutralz = 0.358271 * y / 0.329016
// Stores simulation variable values for debugging
this.sim[thetype, .] = (yint, m, deviatex, deviatey, neutralx, neutralz, x, z)
// Difference between simulated color and neutral grey
diffx = neutralx - x
diffz = neutralz - z
// Used to compensate transformed values into RGB color space
diffr = diffx * 3.24071 + diffz * -0.498571
diffg = diffx * -0.969258 + diffz * 0.0415557
diffb = diffx * 0.0556352 + diffz * 1.05707
// Stores difference variables for debugging
this.diffrgb[thetype, .] = (diffr, diffg, diffb, diffx, diffz)
// Convert XYZ to RGB color space (xyz -> rgb (sRGB:D65))
dr = x * 3.24071 + y * -1.53726 + z * -0.498571
dg = x * -0.969258 + y * 1.87599 + z * 0.0415557
db = x * 0.0556352 + y * -0.203996 + z * 1.05707
// Stores initial transform values for debugging
this.drgb2[thetype, .] = (dr, dg, db)
// Compensate simulated color towards a neutral fit in RGB space
// If the transformed red is >= 0
if (dr >= 0) {
// Fitted value for red is 1 - transformedRed / red residual
fitr = (1 - dr) / diffr
} // End IF Block for transformed red values >= 0
// Otherwise
else {
// Value is 0 - transformedRed / red residual
fitr = (0 - dr) / diffr
} // End ELSE Block for fitted red values
// If the transformed green is >= 0
if (dg >= 0) {
// Fitted value for green is 1 - transformedRed / green residual
fitg = (1 - dg) / diffg
} // End IF Block for transformed green values >= 0
// Otherwise
else {
// Value is 0 - transformedGreen / green residual
fitg = (0 - dg) / diffg
} // End ELSE Block for fitted green values
// If the transformed blue is >= 0
if (db >= 0) {
// Fitted value for blue is 1 - transformedBlue / blue residual
fitb = (1 - db) / diffb
} // End IF Block for transformed blue values >= 0
// Otherwise
else {
// Value is 0 - transformedBlue / blue residual
fitb = (0 - db) / diffb
} // End ELSE Block for fitted blue values
// Store the fitted values for debugging
this.fitrgb[thetype, .] = (fitr, fitg, fitb)
// Used to get the maxmimum parameter value across r, g, and b parameters
tomax = (fitb, fitg, fitr)
// Maximum value across all parameters
adjust = condMax(tomax)
// Shift proportional to the greatest shift
dr = dr + (adjust * diffr)
dg = dg + (adjust * diffg)
db = db + (adjust * diffb)
// Store the proportionally adjusted RGB values and the adjustment factor
this.drgb3[thetype, .] = (dr, dg, db, adjust)
// Apply gamma correction
dr = dr^getInvGamma()
dg = dg^getInvGamma()
db = db^getInvGamma()
// Store RGB values after gamma correction
this.drgb4[thetype, .] = (dr, dg, db)
// Anomylize colors
dr = (getR() * (1.0 - getAmount())) + dr * getAmount()
dg = (getG() * (1.0 - getAmount())) + dg * getAmount()
db = (getB() * (1.0 - getAmount())) + db * getAmount()
// Store anomylized colors
this.drgb5[thetype, .] = (dr, dg, db)
// Check to make sure ranges are in [0, 255]
updateRGB = (dr, dg, db)
// Update the matrix with the RGB values
this.transformedRGB[thetype, .] = checkRange(updateRGB)
} // End ELSE Block for non achromatope types of color blindness
} // End Loop over the number of transformations
} // End Method definition
// Function for simple color transform
void translateColor(real scalar red, real scalar green, real scalar blue) {
// Declare a colorblind type member variable
class colorblind scalar x
// Stores vector with names of color sight impairments
string rowvector cbtypes
// Used to store RGB strings for these two constants
string scalar black, white
// Used for loops and to sum the RGB values
real scalar i, j
// Sum of the RGB values to translate
j = red + green + blue
// RGB constant for the color black
black = "0 0 0"
// RGB constant for the color white
white = "255 255 255"