Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 21 | #include "Daltonizer.h" |
Mathias Agopian | 1d77b71 | 2017-02-17 15:46:13 -0800 | [diff] [blame] | 22 | #include <math/mat4.h> |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 26 | void Daltonizer::setType(ColorBlindnessType type) { |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 27 | if (type != mType) { |
| 28 | mDirty = true; |
| 29 | mType = type; |
| 30 | } |
| 31 | } |
| 32 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 33 | void Daltonizer::setMode(ColorBlindnessMode mode) { |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 34 | if (mode != mMode) { |
| 35 | mDirty = true; |
| 36 | mMode = mode; |
| 37 | } |
| 38 | } |
| 39 | |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 40 | void Daltonizer::setLevel(int32_t level) { |
| 41 | if (level < 0 || level > 10) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | float newLevel = level / 10.0f; |
| 46 | if (std::fabs(mLevel - newLevel) > 0.09f) { |
| 47 | mDirty = true; |
| 48 | } |
| 49 | mLevel = newLevel; |
| 50 | } |
| 51 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 52 | const mat4& Daltonizer::operator()() { |
| 53 | if (mDirty) { |
| 54 | mDirty = false; |
| 55 | update(); |
| 56 | } |
| 57 | return mColorTransform; |
| 58 | } |
| 59 | |
| 60 | void Daltonizer::update() { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 61 | if (mType == ColorBlindnessType::None) { |
| 62 | mColorTransform = mat4(); |
| 63 | return; |
| 64 | } |
| 65 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 66 | // converts a linear RGB color to the XYZ space |
| 67 | const mat4 rgb2xyz( 0.4124, 0.2126, 0.0193, 0, |
| 68 | 0.3576, 0.7152, 0.1192, 0, |
| 69 | 0.1805, 0.0722, 0.9505, 0, |
| 70 | 0 , 0 , 0 , 1); |
| 71 | |
| 72 | // converts a XYZ color to the LMS space. |
| 73 | const mat4 xyz2lms( 0.7328,-0.7036, 0.0030, 0, |
| 74 | 0.4296, 1.6975, 0.0136, 0, |
| 75 | -0.1624, 0.0061, 0.9834, 0, |
| 76 | 0 , 0 , 0 , 1); |
| 77 | |
| 78 | // Direct conversion from linear RGB to LMS |
| 79 | const mat4 rgb2lms(xyz2lms*rgb2xyz); |
| 80 | |
| 81 | // And back from LMS to linear RGB |
| 82 | const mat4 lms2rgb(inverse(rgb2lms)); |
| 83 | |
| 84 | // To simulate color blindness we need to "remove" the data lost by the absence of |
| 85 | // a cone. This cannot be done by just zeroing out the corresponding LMS component |
| 86 | // because it would create a color outside of the RGB gammut. |
| 87 | // Instead we project the color along the axis of the missing component onto a plane |
| 88 | // within the RGB gammut: |
| 89 | // - since the projection happens along the axis of the missing component, a |
| 90 | // color blind viewer perceives the projected color the same. |
| 91 | // - We use the plane defined by 3 points in LMS space: black, white and |
| 92 | // blue and red for protanopia/deuteranopia and tritanopia respectively. |
| 93 | |
| 94 | // LMS space red |
| 95 | const vec3& lms_r(rgb2lms[0].rgb); |
| 96 | // LMS space blue |
| 97 | const vec3& lms_b(rgb2lms[2].rgb); |
| 98 | // LMS space white |
| 99 | const vec3 lms_w((rgb2lms * vec4(1)).rgb); |
| 100 | |
| 101 | // To find the planes we solve the a*L + b*M + c*S = 0 equation for the LMS values |
| 102 | // of the three known points. This equation is trivially solved, and has for |
| 103 | // solution the following cross-products: |
| 104 | const vec3 p0 = cross(lms_w, lms_b); // protanopia/deuteranopia |
| 105 | const vec3 p1 = cross(lms_w, lms_r); // tritanopia |
| 106 | |
| 107 | // The following 3 matrices perform the projection of a LMS color onto the given plane |
| 108 | // along the selected axis |
| 109 | |
| 110 | // projection for protanopia (L = 0) |
| 111 | const mat4 lms2lmsp( 0.0000, 0.0000, 0.0000, 0, |
| 112 | -p0.y / p0.x, 1.0000, 0.0000, 0, |
| 113 | -p0.z / p0.x, 0.0000, 1.0000, 0, |
| 114 | 0 , 0 , 0 , 1); |
| 115 | |
| 116 | // projection for deuteranopia (M = 0) |
| 117 | const mat4 lms2lmsd( 1.0000, -p0.x / p0.y, 0.0000, 0, |
| 118 | 0.0000, 0.0000, 0.0000, 0, |
| 119 | 0.0000, -p0.z / p0.y, 1.0000, 0, |
| 120 | 0 , 0 , 0 , 1); |
| 121 | |
| 122 | // projection for tritanopia (S = 0) |
| 123 | const mat4 lms2lmst( 1.0000, 0.0000, -p1.x / p1.z, 0, |
| 124 | 0.0000, 1.0000, -p1.y / p1.z, 0, |
| 125 | 0.0000, 0.0000, 0.0000, 0, |
| 126 | 0 , 0 , 0 , 1); |
| 127 | |
| 128 | // We will calculate the error between the color and the color viewed by |
| 129 | // a color blind user and "spread" this error onto the healthy cones. |
| 130 | // The matrices below perform this last step and have been chosen arbitrarily. |
| 131 | |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 132 | // Scale 0 represents no change (mColorTransform is identical matrix). |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 133 | // error spread for protanopia |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 134 | const mat4 errp(1.0, mLevel, mLevel, 0.0, |
| 135 | 0.0, 1.0, 0.0, 0.0, |
| 136 | 0.0, 0.0, 1.0, 0.0, |
| 137 | 0.0, 0.0, 0.0, 1.0); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 138 | |
| 139 | // error spread for deuteranopia |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 140 | const mat4 errd( 1.0, 0.0, 0.0, 0.0, |
| 141 | mLevel, 1.0, mLevel, 0.0, |
| 142 | 0.0, 0.0, 1.0, 0.0, |
| 143 | 0.0, 0.0, 0.0, 1.0); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 144 | |
| 145 | // error spread for tritanopia |
Isaac Chai | 1ec2d9a | 2024-04-11 20:10:34 +0000 | [diff] [blame] | 146 | const mat4 errt( 1.0, 0.0, 0.0, 0.0, |
| 147 | 0.0, 1.0, 0.0, 0.0, |
| 148 | mLevel, mLevel, 1.0, 0.0, |
| 149 | 0.0, 0.0, 0.0, 1.0); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 150 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 151 | // And the magic happens here... |
| 152 | // We construct the matrix that will perform the whole correction. |
| 153 | |
| 154 | // simulation: type of color blindness to simulate: |
| 155 | // set to either lms2lmsp, lms2lmsd, lms2lmst |
| 156 | mat4 simulation; |
| 157 | |
| 158 | // correction: type of color blindness correction (should match the simulation above): |
| 159 | // set to identity, errp, errd, errt ([0] for simulation only) |
| 160 | mat4 correction(0); |
| 161 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 162 | switch (mType) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 163 | case ColorBlindnessType::Protanomaly: |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 164 | simulation = lms2lmsp; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 165 | if (mMode == ColorBlindnessMode::Correction) |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 166 | correction = errp; |
| 167 | break; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 168 | case ColorBlindnessType::Deuteranomaly: |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 169 | simulation = lms2lmsd; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 170 | if (mMode == ColorBlindnessMode::Correction) |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 171 | correction = errd; |
| 172 | break; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 173 | case ColorBlindnessType::Tritanomaly: |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 174 | simulation = lms2lmst; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 175 | if (mMode == ColorBlindnessMode::Correction) |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 176 | correction = errt; |
| 177 | break; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 178 | case ColorBlindnessType::None: |
| 179 | // We already caught this at the beginning of the method, but the |
| 180 | // compiler doesn't know that |
| 181 | break; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Alan Viverette | cd554e3 | 2014-03-10 12:42:08 -0700 | [diff] [blame] | 184 | mColorTransform = lms2rgb * |
| 185 | (simulation * rgb2lms + correction * (rgb2lms - simulation * rgb2lms)); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } /* namespace android */ |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 189 | |
| 190 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 191 | #pragma clang diagnostic pop // ignored "-Wconversion" |