| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * Global exports. Used by popup to show effect of filter during setup. | 7 * Global exports. Used by popup to show effect of filter during setup. |
| 8 */ | 8 */ |
| 9 (function(exports) { | 9 (function(exports) { |
| 10 // TODO(wnwen): Replace var with let. |
| 10 var curDelta = 0; | 11 var curDelta = 0; |
| 11 var curSeverity = 0; | 12 var curSeverity = 0; |
| 12 var curType = 'PROTANOMALY'; | 13 var curType = 'PROTANOMALY'; |
| 13 var curSimulate = false; | 14 var curSimulate = false; |
| 14 var curEnable = false; | 15 var curEnable = false; |
| 15 var curFilter = 0; | 16 var curFilter = 0; |
| 17 var cssTemplate = ` |
| 18 html[cvd="0"] { |
| 19 -webkit-filter: url('#cvd_extension_0'); |
| 20 } |
| 21 html[cvd="1"] { |
| 22 -webkit-filter: url('#cvd_extension_1'); |
| 23 } |
| 24 `; |
| 16 | 25 |
| 26 /** @const {string} */ |
| 27 var SVG_DEFAULT_MATRIX = |
| 28 '1 0 0 0 0 ' + |
| 29 '0 1 0 0 0 ' + |
| 30 '0 0 1 0 0 ' + |
| 31 '0 0 0 1 0'; |
| 32 |
| 33 var svgContent = ` |
| 34 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> |
| 35 <defs> |
| 36 <filter x="0" y="0" width="99999" height="99999" id="cvd_extension_0"> |
| 37 <feColorMatrix id="cvd_matrix_0" type="matrix" values=" |
| 38 ${SVG_DEFAULT_MATRIX}"/> |
| 39 </filter> |
| 40 <filter x="0" y="0" width="99999" height="99999" id="cvd_extension_1"> |
| 41 <feColorMatrix id="cvd_matrix_1" type="matrix" values=" |
| 42 ${SVG_DEFAULT_MATRIX}"/> |
| 43 </filter> |
| 44 </defs> |
| 45 </svg> |
| 46 `; |
| 17 | 47 |
| 18 // ======= 3x3 matrix ops ======= | 48 // ======= 3x3 matrix ops ======= |
| 19 | 49 |
| 20 /** | 50 /** |
| 21 * The 3x3 identity matrix. | 51 * The 3x3 identity matrix. |
| 22 * @const {object} | 52 * @const {object} |
| 23 */ | 53 */ |
| 24 var IDENTITY_MATRIX_3x3 = [ | 54 var IDENTITY_MATRIX_3x3 = [ |
| 25 [1, 0, 0], | 55 [1, 0, 0], |
| 26 [0, 1, 0], | 56 [0, 1, 0], |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 add3x3(IDENTITY_MATRIX_3x3, cvdCorrectionMatrix), | 325 add3x3(IDENTITY_MATRIX_3x3, cvdCorrectionMatrix), |
| 296 tmpProduct); | 326 tmpProduct); |
| 297 } | 327 } |
| 298 | 328 |
| 299 return effectiveMatrix; | 329 return effectiveMatrix; |
| 300 } | 330 } |
| 301 | 331 |
| 302 | 332 |
| 303 // ======= Page linker ======= | 333 // ======= Page linker ======= |
| 304 | 334 |
| 305 /** @const {string} */ | 335 const STYLE_ID = 'cvd_style'; |
| 306 var SVG_DEFAULT_MATRIX = | 336 const WRAP_ID = 'cvd_extension_svg_filter'; |
| 307 '1 0 0 0 0 ' + | |
| 308 '0 1 0 0 0 ' + | |
| 309 '0 0 1 0 0 ' + | |
| 310 '0 0 0 1 0'; | |
| 311 | |
| 312 var svgContent = | |
| 313 '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">' + | |
| 314 ' <defs>' + | |
| 315 ' <filter id="cvd_extension_0">' + | |
| 316 ' <feColorMatrix id="cvd_matrix_0" type="matrix" values="' + | |
| 317 SVG_DEFAULT_MATRIX + '"/>' + | |
| 318 ' </filter>' + | |
| 319 ' <filter id="cvd_extension_1">' + | |
| 320 ' <feColorMatrix id="cvd_matrix_1" type="matrix" values="' + | |
| 321 SVG_DEFAULT_MATRIX + '"/>' + | |
| 322 ' </filter>' + | |
| 323 ' </defs>' + | |
| 324 '</svg>'; | |
| 325 | 337 |
| 326 /** | 338 /** |
| 327 * Checks for svg filter matrix presence and append to DOM if not present. | 339 * Checks for required elements, adding if missing. |
| 328 */ | 340 */ |
| 329 function addSvgIfMissing() { | 341 function addElements() { |
| 330 var wrap = document.getElementById('cvd_extension_svg_filter'); | 342 var style = document.getElementById(STYLE_ID); |
| 343 if (!style) { |
| 344 var baseUrl = window.location.href.replace(window.location.hash, ''); |
| 345 style = document.createElement('style'); |
| 346 style.id = STYLE_ID; |
| 347 style.setAttribute('type', 'text/css'); |
| 348 style.innerHTML = cssTemplate.replace(/#/g, baseUrl + '#'); |
| 349 document.head.appendChild(style); |
| 350 } |
| 351 |
| 352 var wrap = document.getElementById(WRAP_ID); |
| 331 if (!wrap) { | 353 if (!wrap) { |
| 332 wrap = document.createElement('span'); | 354 wrap = document.createElement('span'); |
| 333 wrap.id = 'cvd_extension_svg_filter'; | 355 wrap.id = WRAP_ID; |
| 334 wrap.setAttribute('hidden', ''); | 356 wrap.setAttribute('hidden', ''); |
| 335 wrap.innerHTML = svgContent; | 357 wrap.innerHTML = svgContent; |
| 336 document.body.appendChild(wrap); | 358 document.body.appendChild(wrap); |
| 337 } | 359 } |
| 338 } | 360 } |
| 339 | 361 |
| 340 /** | 362 /** |
| 341 * Updates the SVG filter based on the RGB correction/simulation matrix. | 363 * Updates the SVG filter based on the RGB correction/simulation matrix. |
| 342 * @param {!Object} matrix 3x3 RGB transformation matrix. | 364 * @param {!Object} matrix 3x3 RGB transformation matrix. |
| 343 */ | 365 */ |
| 344 function setFilter(matrix) { | 366 function setFilter(matrix) { |
| 345 addSvgIfMissing(); | 367 addElements(); |
| 346 var next = 1 - curFilter; | 368 var next = 1 - curFilter; |
| 347 | 369 |
| 348 debugPrint('update: matrix#' + next + '=' + | 370 debugPrint('update: matrix#' + next + '=' + |
| 349 humanReadbleStringFrom3x3(matrix)); | 371 humanReadbleStringFrom3x3(matrix)); |
| 350 | 372 |
| 351 var matrixElem = document.getElementById('cvd_matrix_' + next); | 373 var matrixElem = document.getElementById('cvd_matrix_' + next); |
| 352 matrixElem.setAttribute('values', svgMatrixStringFrom3x3(matrix)); | 374 matrixElem.setAttribute('values', svgMatrixStringFrom3x3(matrix)); |
| 353 | 375 |
| 354 document.documentElement.setAttribute('cvd', next); | 376 document.documentElement.setAttribute('cvd', next); |
| 355 | 377 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 | 495 |
| 474 /** | 496 /** |
| 475 * Clears color correction filter. | 497 * Clears color correction filter. |
| 476 */ | 498 */ |
| 477 exports.clearColorEnhancementFilter = function() { | 499 exports.clearColorEnhancementFilter = function() { |
| 478 clearFilter(); | 500 clearFilter(); |
| 479 }; | 501 }; |
| 480 })(this); | 502 })(this); |
| 481 | 503 |
| 482 this.initializeExtension(); | 504 this.initializeExtension(); |
| OLD | NEW |