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 17 #include "Daltonizer.h" 18 #include <ui/mat4.h> 19 20 namespace android { 21 22 Daltonizer::Daltonizer() : 23 mType(deuteranomaly), mMode(simulation), mDirty(true) { 24 } 25 26 Daltonizer::~Daltonizer() { 27 } 28 29 void Daltonizer::setType(Daltonizer::ColorBlindnessTypes type) { 30 if (type != mType) { 31 mDirty = true; 32 mType = type; 33 } 34 } 35 36 void Daltonizer::setMode(Daltonizer::Mode mode) { 37 if (mode != mMode) { 38 mDirty = true; 39 mMode = mode; 40 } 41 } 42 43 const mat4& Daltonizer::operator()() { 44 if (mDirty) { 45 mDirty = false; 46 update(); 47 } 48 return mColorTransform; 49 } 50 51 void Daltonizer::update() { 52 // converts a linear RGB color to the XYZ space 53 const mat4 rgb2xyz( 0.4124, 0.2126, 0.0193, 0, 54 0.3576, 0.7152, 0.1192, 0, 55 0.1805, 0.0722, 0.9505, 0, 56 0 , 0 , 0 , 1); 57 58 // converts a XYZ color to the LMS space. 59 const mat4 xyz2lms( 0.7328,-0.7036, 0.0030, 0, 60 0.4296, 1.6975, 0.0136, 0, 61 -0.1624, 0.0061, 0.9834, 0, 62 0 , 0 , 0 , 1); 63 64 // Direct conversion from linear RGB to LMS 65 const mat4 rgb2lms(xyz2lms*rgb2xyz); 66 67 // And back from LMS to linear RGB 68 const mat4 lms2rgb(inverse(rgb2lms)); 69 70 // To simulate color blindness we need to "remove" the data lost by the absence of 71 // a cone. This cannot be done by just zeroing out the corresponding LMS component 72 // because it would create a color outside of the RGB gammut. 73 // Instead we project the color along the axis of the missing component onto a plane 74 // within the RGB gammut: 75 // - since the projection happens along the axis of the missing component, a 76 // color blind viewer perceives the projected color the same. 77 // - We use the plane defined by 3 points in LMS space: black, white and 78 // blue and red for protanopia/deuteranopia and tritanopia respectively. 79 80 // LMS space red 81 const vec3& lms_r(rgb2lms[0].rgb); 82 // LMS space blue 83 const vec3& lms_b(rgb2lms[2].rgb); 84 // LMS space white 85 const vec3 lms_w((rgb2lms * vec4(1)).rgb); 86 87 // To find the planes we solve the a*L + b*M + c*S = 0 equation for the LMS values 88 // of the three known points. This equation is trivially solved, and has for 89 // solution the following cross-products: 90 const vec3 p0 = cross(lms_w, lms_b); // protanopia/deuteranopia 91 const vec3 p1 = cross(lms_w, lms_r); // tritanopia 92 93 // The following 3 matrices perform the projection of a LMS color onto the given plane 94 // along the selected axis 95 96 // projection for protanopia (L = 0) 97 const mat4 lms2lmsp( 0.0000, 0.0000, 0.0000, 0, 98 -p0.y / p0.x, 1.0000, 0.0000, 0, 99 -p0.z / p0.x, 0.0000, 1.0000, 0, 100 0 , 0 , 0 , 1); 101 102 // projection for deuteranopia (M = 0) 103 const mat4 lms2lmsd( 1.0000, -p0.x / p0.y, 0.0000, 0, 104 0.0000, 0.0000, 0.0000, 0, 105 0.0000, -p0.z / p0.y, 1.0000, 0, 106 0 , 0 , 0 , 1); 107 108 // projection for tritanopia (S = 0) 109 const mat4 lms2lmst( 1.0000, 0.0000, -p1.x / p1.z, 0, 110 0.0000, 1.0000, -p1.y / p1.z, 0, 111 0.0000, 0.0000, 0.0000, 0, 112 0 , 0 , 0 , 1); 113 114 // We will calculate the error between the color and the color viewed by 115 // a color blind user and "spread" this error onto the healthy cones. 116 // The matrices below perform this last step and have been chosen arbitrarily. 117 118 // The amount of correction can be adjusted here. 119 120 // error spread for protanopia 121 const mat4 errp( 1.0, 0.7, 0.7, 0, 122 0.0, 1.0, 0.0, 0, 123 0.0, 0.0, 1.0, 0, 124 0, 0, 0, 1); 125 126 // error spread for deuteranopia 127 const mat4 errd( 1.0, 0.0, 0.0, 0, 128 0.7, 1.0, 0.7, 0, 129 0.0, 0.0, 1.0, 0, 130 0, 0, 0, 1); 131 132 // error spread for tritanopia 133 const mat4 errt( 1.0, 0.0, 0.0, 0, 134 0.0, 1.0, 0.0, 0, 135 0.7, 0.7, 1.0, 0, 136 0, 0, 0, 1); 137 138 const mat4 identity; 139 140 // And the magic happens here... 141 // We construct the matrix that will perform the whole correction. 142 143 // simulation: type of color blindness to simulate: 144 // set to either lms2lmsp, lms2lmsd, lms2lmst 145 mat4 simulation; 146 147 // correction: type of color blindness correction (should match the simulation above): 148 // set to identity, errp, errd, errt ([0] for simulation only) 149 mat4 correction(0); 150 151 switch (mType) { 152 case protanopia: 153 case protanomaly: 154 simulation = lms2lmsp; 155 if (mMode == Daltonizer::correction) 156 correction = errp; 157 break; 158 case deuteranopia: 159 case deuteranomaly: 160 simulation = lms2lmsd; 161 if (mMode == Daltonizer::correction) 162 correction = errd; 163 break; 164 case tritanopia: 165 case tritanomaly: 166 simulation = lms2lmst; 167 if (mMode == Daltonizer::correction) 168 correction = errt; 169 break; 170 } 171 172 mColorTransform = lms2rgb * 173 (simulation * rgb2lms + correction * (rgb2lms - simulation * rgb2lms)); 174 } 175 176 } /* namespace android */ 177