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
use crate::RevBlind;
use image::Rgba;
const ANCHOR: [f64; 12] = [
0.08008, 0.1579, 0.5897, 0.1284, 0.2237, 0.3636, 0.9856, 0.7325, 0.001079, 0.0914, 0.007009,
0.0,
];
const ANCHOR_E: [f64; 3] = [
RGB2LMS[0] + RGB2LMS[1] + RGB2LMS[2],
RGB2LMS[3] + RGB2LMS[4] + RGB2LMS[5],
RGB2LMS[6] + RGB2LMS[7] + RGB2LMS[8],
];
pub fn bvm97(variant: RevBlind) -> impl Fn(&Rgba<u8>) -> Rgba<u8> {
use RevBlind::*;
let context = match variant {
Deutan => Context {
a1: ANCHOR_E[1] * ANCHOR[8] - ANCHOR_E[2] * ANCHOR[7],
b1: ANCHOR_E[2] * ANCHOR[6] - ANCHOR_E[0] * ANCHOR[8],
c1: ANCHOR_E[0] * ANCHOR[7] - ANCHOR_E[1] * ANCHOR[6],
a2: ANCHOR_E[1] * ANCHOR[2] - ANCHOR_E[2] * ANCHOR[1],
b2: ANCHOR_E[2] * ANCHOR[0] - ANCHOR_E[0] * ANCHOR[2],
c2: ANCHOR_E[0] * ANCHOR[1] - ANCHOR_E[1] * ANCHOR[0],
inflection: (ANCHOR_E[2] / ANCHOR_E[0]),
},
Protan => Context {
a1: ANCHOR_E[1] * ANCHOR[8] - ANCHOR_E[2] * ANCHOR[7],
b1: ANCHOR_E[2] * ANCHOR[6] - ANCHOR_E[0] * ANCHOR[8],
c1: ANCHOR_E[0] * ANCHOR[7] - ANCHOR_E[1] * ANCHOR[6],
a2: ANCHOR_E[1] * ANCHOR[2] - ANCHOR_E[2] * ANCHOR[1],
b2: ANCHOR_E[2] * ANCHOR[0] - ANCHOR_E[0] * ANCHOR[2],
c2: ANCHOR_E[0] * ANCHOR[1] - ANCHOR_E[1] * ANCHOR[0],
inflection: (ANCHOR_E[2] / ANCHOR_E[1]),
},
Tritan => Context {
a1: ANCHOR_E[1] * ANCHOR[11] - ANCHOR_E[2] * ANCHOR[10],
b1: ANCHOR_E[2] * ANCHOR[9] - ANCHOR_E[0] * ANCHOR[11],
c1: ANCHOR_E[0] * ANCHOR[10] - ANCHOR_E[1] * ANCHOR[9],
a2: ANCHOR_E[1] * ANCHOR[5] - ANCHOR_E[2] * ANCHOR[4],
b2: ANCHOR_E[2] * ANCHOR[3] - ANCHOR_E[0] * ANCHOR[5],
c2: ANCHOR_E[0] * ANCHOR[4] - ANCHOR_E[1] * ANCHOR[3],
inflection: (ANCHOR_E[1] / ANCHOR_E[0]),
},
};
move |p| gimp_algorithm(p, variant.clone(), &context)
}
pub struct Context {
inflection: f64,
a1: f64,
b1: f64,
c1: f64,
a2: f64,
b2: f64,
c2: f64,
}
const RGB2LMS: [f64; 9] = [
0.05059983, 0.08585369, 0.00952420, 0.01893033, 0.08925308, 0.01370054, 0.00292202, 0.00975732,
0.07145979,
];
const LMS2RGB: [f64; 9] = [
30.830854, -29.832659, 1.610474, -6.481468, 17.715578, -2.532642, -0.375690, -1.199062,
14.273846,
];
pub fn gimp_algorithm(pixel: &Rgba<u8>, variant: RevBlind, colorblind: &Context) -> Rgba<u8> {
#![allow(non_snake_case)]
let Context {
inflection,
a1,
a2,
b1,
b2,
c1,
c2,
} = colorblind;
let mut red = pixel.0[0] as f64;
let mut green = pixel.0[1] as f64;
let mut blue = pixel.0[2] as f64;
let mut redOld = red;
let mut greenOld = green;
red = redOld * RGB2LMS[0] + greenOld * RGB2LMS[1] + blue * RGB2LMS[2];
green = redOld * RGB2LMS[3] + greenOld * RGB2LMS[4] + blue * RGB2LMS[5];
blue = redOld * RGB2LMS[6] + greenOld * RGB2LMS[7] + blue * RGB2LMS[8];
match variant {
RevBlind::Deutan => {
let tmp = blue / red;
if tmp < *inflection {
green = -(a1 * red + c1 * blue) / b1;
} else {
green = -(a2 * red + c2 * blue) / b2;
}
}
RevBlind::Protan => {
let tmp = blue / green;
if tmp < *inflection {
red = -(b1 * green + c1 * blue) / a1;
} else {
red = -(b2 * green + c2 * blue) / a2;
}
}
RevBlind::Tritan => {
let tmp = green / red;
if tmp < *inflection {
blue = -(a1 * red + b1 * green) / c1;
} else {
blue = -(a2 * red + b2 * green) / c2;
}
}
}
redOld = red;
greenOld = green;
red = redOld * LMS2RGB[0] + greenOld * LMS2RGB[1] + blue * LMS2RGB[2];
green = redOld * LMS2RGB[3] + greenOld * LMS2RGB[4] + blue * LMS2RGB[5];
blue = redOld * LMS2RGB[6] + greenOld * LMS2RGB[7] + blue * LMS2RGB[8];
Rgba([
red.round() as u8,
green.round() as u8,
blue.round() as u8,
pixel[3],
])
}