2016-01-05 00:46:52 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
2017-10-25 21:48:27 +00:00
|
|
|
Copyright 2017 New Vector Ltd
|
2016-01-05 00:46:52 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const DEBUG = 0;
|
2016-04-16 00:00:10 +00:00
|
|
|
|
2017-11-15 01:01:17 +00:00
|
|
|
// utility to turn #rrggbb or rgb(r,g,b) into [red,green,blue]
|
|
|
|
function colorToRgb(color) {
|
2017-11-15 14:47:20 +00:00
|
|
|
if (!color) {
|
|
|
|
return [0, 0, 0];
|
|
|
|
}
|
|
|
|
|
2017-11-15 01:01:17 +00:00
|
|
|
if (color[0] === '#') {
|
|
|
|
color = color.slice(1);
|
|
|
|
if (color.length === 3) {
|
|
|
|
color = color[0] + color[0] +
|
|
|
|
color[1] + color[1] +
|
|
|
|
color[2] + color[2];
|
|
|
|
}
|
|
|
|
const val = parseInt(color, 16);
|
|
|
|
const r = (val >> 16) & 255;
|
|
|
|
const g = (val >> 8) & 255;
|
|
|
|
const b = val & 255;
|
|
|
|
return [r, g, b];
|
2017-11-15 10:14:16 +00:00
|
|
|
} else {
|
|
|
|
const match = color.match(/rgb\((.*?),(.*?),(.*?)\)/);
|
2017-11-15 01:01:17 +00:00
|
|
|
if (match) {
|
2017-11-15 10:14:16 +00:00
|
|
|
return [
|
|
|
|
parseInt(match[1]),
|
|
|
|
parseInt(match[2]),
|
|
|
|
parseInt(match[3]),
|
|
|
|
];
|
2017-11-15 01:01:17 +00:00
|
|
|
}
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
2017-11-15 10:14:16 +00:00
|
|
|
return [0, 0, 0];
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
// utility to turn [red,green,blue] into #rrggbb
|
2017-11-15 01:01:17 +00:00
|
|
|
function rgbToColor(rgb) {
|
2017-10-11 16:56:17 +00:00
|
|
|
const val = (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
|
2017-01-20 14:22:27 +00:00
|
|
|
return '#' + (0x1000000 + val).toString(16).slice(1);
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
class Tinter {
|
|
|
|
constructor() {
|
|
|
|
// The default colour keys to be replaced as referred to in CSS
|
|
|
|
// (should be overridden by .mx_theme_accentColor and .mx_theme_secondaryAccentColor)
|
|
|
|
this.keyRgb = [
|
|
|
|
"rgb(118, 207, 166)", // Vector Green
|
|
|
|
"rgb(234, 245, 240)", // Vector Light Green
|
2017-11-15 01:01:17 +00:00
|
|
|
"rgb(211, 239, 225)", // roomsublist-label-bg-color (20% Green overlaid on Light Green)
|
2017-11-08 14:55:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Some algebra workings for calculating the tint % of Vector Green & Light Green
|
|
|
|
// x * 118 + (1 - x) * 255 = 234
|
|
|
|
// x * 118 + 255 - 255 * x = 234
|
|
|
|
// x * 118 - x * 255 = 234 - 255
|
|
|
|
// (255 - 118) x = 255 - 234
|
|
|
|
// x = (255 - 234) / (255 - 118) = 0.16
|
|
|
|
|
|
|
|
// The colour keys to be replaced as referred to in SVGs
|
|
|
|
this.keyHex = [
|
|
|
|
"#76CFA6", // Vector Green
|
|
|
|
"#EAF5F0", // Vector Light Green
|
2017-11-15 01:01:17 +00:00
|
|
|
"#D3EFE1", // roomsublist-label-bg-color (20% Green overlaid on Light Green)
|
2017-11-08 14:55:07 +00:00
|
|
|
"#FFFFFF", // white highlights of the SVGs (for switching to dark theme)
|
2017-11-15 10:40:07 +00:00
|
|
|
"#000000", // black lowlights of the SVGs (for switching to dark theme)
|
2017-11-08 14:55:07 +00:00
|
|
|
];
|
|
|
|
|
2017-11-15 01:45:51 +00:00
|
|
|
// track the replacement colours actually being used
|
2017-11-08 14:55:07 +00:00
|
|
|
// defaults to our keys.
|
|
|
|
this.colors = [
|
|
|
|
this.keyHex[0],
|
|
|
|
this.keyHex[1],
|
|
|
|
this.keyHex[2],
|
|
|
|
this.keyHex[3],
|
2017-11-15 10:40:07 +00:00
|
|
|
this.keyHex[4],
|
2017-11-08 14:55:07 +00:00
|
|
|
];
|
|
|
|
|
2017-11-15 01:45:51 +00:00
|
|
|
// track the most current tint request inputs (which may differ from the
|
|
|
|
// end result stored in this.colors
|
|
|
|
this.currentTint = [
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
2017-11-15 10:40:07 +00:00
|
|
|
undefined,
|
2017-11-15 01:45:51 +00:00
|
|
|
];
|
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
this.cssFixups = [
|
|
|
|
// { theme: {
|
|
|
|
// style: a style object that should be fixed up taken from a stylesheet
|
|
|
|
// attr: name of the attribute to be clobbered, e.g. 'color'
|
|
|
|
// index: ordinal of primary, secondary or tertiary
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
];
|
|
|
|
|
|
|
|
// CSS attributes to be fixed up
|
|
|
|
this.cssAttrs = [
|
|
|
|
"color",
|
|
|
|
"backgroundColor",
|
|
|
|
"borderColor",
|
|
|
|
"borderTopColor",
|
|
|
|
"borderBottomColor",
|
|
|
|
"borderLeftColor",
|
|
|
|
];
|
|
|
|
|
|
|
|
this.svgAttrs = [
|
|
|
|
"fill",
|
|
|
|
"stroke",
|
|
|
|
];
|
|
|
|
|
|
|
|
// List of functions to call when the tint changes.
|
|
|
|
this.tintables = [];
|
|
|
|
|
|
|
|
// the currently loaded theme (if any)
|
|
|
|
this.theme = undefined;
|
2017-11-14 15:27:28 +00:00
|
|
|
|
|
|
|
// whether to force a tint (e.g. after changing theme)
|
|
|
|
this.forceTint = false;
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
2016-11-16 14:16:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to fire when the tint changes.
|
|
|
|
* This is used to rewrite the tintable SVGs with the new tint.
|
|
|
|
*
|
|
|
|
* It's not possible to unregister a tintable callback. So this can only be
|
|
|
|
* used to register a static callback. If a set of tintables will change
|
|
|
|
* over time then the best bet is to register a single callback for the
|
|
|
|
* entire set.
|
|
|
|
*
|
2020-01-16 11:54:36 +00:00
|
|
|
* To ensure the tintable work happens at least once, it is also called as
|
|
|
|
* part of registration.
|
|
|
|
*
|
2016-11-16 14:16:51 +00:00
|
|
|
* @param {Function} tintable Function to call when the tint changes.
|
|
|
|
*/
|
2017-11-08 14:55:07 +00:00
|
|
|
registerTintable(tintable) {
|
|
|
|
this.tintables.push(tintable);
|
2020-01-16 11:54:36 +00:00
|
|
|
tintable();
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getKeyRgb() {
|
|
|
|
return this.keyRgb;
|
|
|
|
}
|
|
|
|
|
|
|
|
tint(primaryColor, secondaryColor, tertiaryColor) {
|
2018-05-25 02:17:29 +00:00
|
|
|
return;
|
2018-10-17 14:20:38 +00:00
|
|
|
// eslint-disable-next-line no-unreachable
|
2017-11-15 01:45:51 +00:00
|
|
|
this.currentTint[0] = primaryColor;
|
|
|
|
this.currentTint[1] = secondaryColor;
|
|
|
|
this.currentTint[2] = tertiaryColor;
|
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
this.calcCssFixups();
|
2016-01-05 00:46:52 +00:00
|
|
|
|
2017-11-15 10:14:16 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
console.log("Tinter.tint(" + primaryColor + ", " +
|
|
|
|
secondaryColor + ", " +
|
|
|
|
tertiaryColor + ")");
|
|
|
|
}
|
2017-11-15 01:01:17 +00:00
|
|
|
|
2016-01-08 03:22:38 +00:00
|
|
|
if (!primaryColor) {
|
2017-11-08 14:55:07 +00:00
|
|
|
primaryColor = this.keyRgb[0];
|
|
|
|
secondaryColor = this.keyRgb[1];
|
2017-11-15 01:01:17 +00:00
|
|
|
tertiaryColor = this.keyRgb[2];
|
2016-01-08 03:22:38 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 00:46:52 +00:00
|
|
|
if (!secondaryColor) {
|
2017-01-20 14:22:27 +00:00
|
|
|
const x = 0.16; // average weighting factor calculated from vector green & light green
|
2017-11-15 01:01:17 +00:00
|
|
|
const rgb = colorToRgb(primaryColor);
|
2016-01-05 00:46:52 +00:00
|
|
|
rgb[0] = x * rgb[0] + (1 - x) * 255;
|
|
|
|
rgb[1] = x * rgb[1] + (1 - x) * 255;
|
|
|
|
rgb[2] = x * rgb[2] + (1 - x) * 255;
|
2017-11-15 01:01:17 +00:00
|
|
|
secondaryColor = rgbToColor(rgb);
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tertiaryColor) {
|
2017-01-20 14:22:27 +00:00
|
|
|
const x = 0.19;
|
2017-11-15 01:01:17 +00:00
|
|
|
const rgb1 = colorToRgb(primaryColor);
|
|
|
|
const rgb2 = colorToRgb(secondaryColor);
|
2016-01-05 00:46:52 +00:00
|
|
|
rgb1[0] = x * rgb1[0] + (1 - x) * rgb2[0];
|
|
|
|
rgb1[1] = x * rgb1[1] + (1 - x) * rgb2[1];
|
|
|
|
rgb1[2] = x * rgb1[2] + (1 - x) * rgb2[2];
|
2017-11-15 01:01:17 +00:00
|
|
|
tertiaryColor = rgbToColor(rgb1);
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:27:28 +00:00
|
|
|
if (this.forceTint == false &&
|
|
|
|
this.colors[0] === primaryColor &&
|
2017-11-08 14:55:07 +00:00
|
|
|
this.colors[1] === secondaryColor &&
|
|
|
|
this.colors[2] === tertiaryColor) {
|
2016-01-08 03:22:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-14 15:27:28 +00:00
|
|
|
this.forceTint = false;
|
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
this.colors[0] = primaryColor;
|
|
|
|
this.colors[1] = secondaryColor;
|
|
|
|
this.colors[2] = tertiaryColor;
|
2016-01-05 03:34:52 +00:00
|
|
|
|
2017-11-15 10:14:16 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
console.log("Tinter.tint final: (" + primaryColor + ", " +
|
|
|
|
secondaryColor + ", " +
|
|
|
|
tertiaryColor + ")");
|
|
|
|
}
|
2016-04-16 00:00:10 +00:00
|
|
|
|
2016-01-05 00:46:52 +00:00
|
|
|
// go through manually fixing up the stylesheets.
|
2017-11-04 23:50:57 +00:00
|
|
|
this.applyCssFixups();
|
2016-01-05 00:46:52 +00:00
|
|
|
|
2016-01-07 03:39:00 +00:00
|
|
|
// tell all the SVGs to go fix themselves up
|
2016-04-16 00:00:10 +00:00
|
|
|
// we don't do this as a dispatch otherwise it will visually lag
|
2017-11-08 14:55:07 +00:00
|
|
|
this.tintables.forEach(function(tintable) {
|
2016-11-16 14:16:51 +00:00
|
|
|
tintable();
|
|
|
|
});
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
2016-01-06 02:29:08 +00:00
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
tintSvgWhite(whiteColor) {
|
2017-11-15 01:45:51 +00:00
|
|
|
this.currentTint[3] = whiteColor;
|
|
|
|
|
2017-01-18 14:06:47 +00:00
|
|
|
if (!whiteColor) {
|
2017-11-08 14:55:07 +00:00
|
|
|
whiteColor = this.colors[3];
|
2017-01-18 14:06:47 +00:00
|
|
|
}
|
2017-11-08 14:55:07 +00:00
|
|
|
if (this.colors[3] === whiteColor) {
|
2017-01-18 14:06:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-08 14:55:07 +00:00
|
|
|
this.colors[3] = whiteColor;
|
|
|
|
this.tintables.forEach(function(tintable) {
|
2017-01-18 14:06:47 +00:00
|
|
|
tintable();
|
|
|
|
});
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
2017-11-04 23:50:57 +00:00
|
|
|
|
2017-11-15 10:40:07 +00:00
|
|
|
tintSvgBlack(blackColor) {
|
|
|
|
this.currentTint[4] = blackColor;
|
|
|
|
|
|
|
|
if (!blackColor) {
|
|
|
|
blackColor = this.colors[4];
|
|
|
|
}
|
|
|
|
if (this.colors[4] === blackColor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.colors[4] = blackColor;
|
|
|
|
this.tintables.forEach(function(tintable) {
|
|
|
|
tintable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setTheme(theme) {
|
2017-11-08 14:55:07 +00:00
|
|
|
this.theme = theme;
|
2017-11-05 00:40:38 +00:00
|
|
|
|
2017-11-04 23:50:57 +00:00
|
|
|
// update keyRgb from the current theme CSS itself, if it defines it
|
|
|
|
if (document.getElementById('mx_theme_accentColor')) {
|
2017-11-08 14:55:07 +00:00
|
|
|
this.keyRgb[0] = window.getComputedStyle(
|
2017-11-15 10:14:16 +00:00
|
|
|
document.getElementById('mx_theme_accentColor')).color;
|
2017-11-04 23:50:57 +00:00
|
|
|
}
|
|
|
|
if (document.getElementById('mx_theme_secondaryAccentColor')) {
|
2017-11-08 14:55:07 +00:00
|
|
|
this.keyRgb[1] = window.getComputedStyle(
|
2017-11-15 10:14:16 +00:00
|
|
|
document.getElementById('mx_theme_secondaryAccentColor')).color;
|
2017-11-04 23:50:57 +00:00
|
|
|
}
|
2017-11-15 01:01:17 +00:00
|
|
|
if (document.getElementById('mx_theme_tertiaryAccentColor')) {
|
|
|
|
this.keyRgb[2] = window.getComputedStyle(
|
2017-11-15 10:14:16 +00:00
|
|
|
document.getElementById('mx_theme_tertiaryAccentColor')).color;
|
2017-11-15 01:01:17 +00:00
|
|
|
}
|
2017-11-04 23:50:57 +00:00
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
this.calcCssFixups();
|
2017-11-14 15:27:28 +00:00
|
|
|
this.forceTint = true;
|
2017-11-15 01:45:51 +00:00
|
|
|
|
|
|
|
this.tint(this.currentTint[0], this.currentTint[1], this.currentTint[2]);
|
|
|
|
|
|
|
|
if (theme === 'dark') {
|
|
|
|
// abuse the tinter to change all the SVG's #fff to #2d2d2d
|
|
|
|
// XXX: obviously this shouldn't be hardcoded here.
|
|
|
|
this.tintSvgWhite('#2d2d2d');
|
2017-11-15 10:40:07 +00:00
|
|
|
this.tintSvgBlack('#dddddd');
|
2017-11-15 01:45:51 +00:00
|
|
|
} else {
|
|
|
|
this.tintSvgWhite('#ffffff');
|
2017-11-15 10:40:07 +00:00
|
|
|
this.tintSvgBlack('#000000');
|
2017-11-15 01:45:51 +00:00
|
|
|
}
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
calcCssFixups() {
|
|
|
|
// cache our fixups
|
|
|
|
if (this.cssFixups[this.theme]) return;
|
|
|
|
|
2017-11-15 10:14:16 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
console.debug("calcCssFixups start for " + this.theme + " (checking " +
|
|
|
|
document.styleSheets.length +
|
|
|
|
" stylesheets)");
|
|
|
|
}
|
2017-11-08 14:55:07 +00:00
|
|
|
|
|
|
|
this.cssFixups[this.theme] = [];
|
|
|
|
|
2017-11-04 23:50:57 +00:00
|
|
|
for (let i = 0; i < document.styleSheets.length; i++) {
|
|
|
|
const ss = document.styleSheets[i];
|
2018-04-11 17:31:54 +00:00
|
|
|
try {
|
|
|
|
if (!ss) continue; // well done safari >:(
|
|
|
|
// Chromium apparently sometimes returns null here; unsure why.
|
|
|
|
// see $14534907369972FRXBx:matrix.org in HQ
|
|
|
|
// ...ah, it's because there's a third party extension like
|
|
|
|
// privacybadger inserting its own stylesheet in there with a
|
|
|
|
// resource:// URI or something which results in a XSS error.
|
|
|
|
// See also #vector:matrix.org/$145357669685386ebCfr:matrix.org
|
|
|
|
// ...except some browsers apparently return stylesheets without
|
|
|
|
// hrefs, which we have no choice but ignore right now
|
|
|
|
|
|
|
|
// XXX seriously? we are hardcoding the name of vector's CSS file in
|
|
|
|
// here?
|
|
|
|
//
|
|
|
|
// Why do we need to limit it to vector's CSS file anyway - if there
|
|
|
|
// are other CSS files affecting the doc don't we want to apply the
|
|
|
|
// same transformations to them?
|
|
|
|
//
|
|
|
|
// Iterating through the CSS looking for matches to hack on feels
|
|
|
|
// pretty horrible anyway. And what if the application skin doesn't use
|
|
|
|
// Vector Green as its primary color?
|
|
|
|
// --richvdh
|
|
|
|
|
2020-08-03 15:02:26 +00:00
|
|
|
// Yes, tinting assumes that you are using the Element skin for now.
|
2018-04-11 17:31:54 +00:00
|
|
|
// The right solution will be to move the CSS over to react-sdk.
|
|
|
|
// And yes, the default assets for the base skin might as well use
|
|
|
|
// Vector Green as any other colour.
|
|
|
|
// --matthew
|
|
|
|
|
|
|
|
// stylesheets we don't have permission to access (eg. ones from extensions) have a null
|
|
|
|
// href and will throw exceptions if we try to access their rules.
|
|
|
|
if (!ss.href || !ss.href.match(new RegExp('/theme-' + this.theme + '.css$'))) continue;
|
|
|
|
if (ss.disabled) continue;
|
|
|
|
if (!ss.cssRules) continue;
|
|
|
|
|
|
|
|
if (DEBUG) console.debug("calcCssFixups checking " + ss.cssRules.length + " rules for " + ss.href);
|
|
|
|
|
|
|
|
for (let j = 0; j < ss.cssRules.length; j++) {
|
|
|
|
const rule = ss.cssRules[j];
|
|
|
|
if (!rule.style) continue;
|
|
|
|
if (rule.selectorText && rule.selectorText.match(/#mx_theme/)) continue;
|
|
|
|
for (let k = 0; k < this.cssAttrs.length; k++) {
|
|
|
|
const attr = this.cssAttrs[k];
|
|
|
|
for (let l = 0; l < this.keyRgb.length; l++) {
|
|
|
|
if (rule.style[attr] === this.keyRgb[l]) {
|
|
|
|
this.cssFixups[this.theme].push({
|
|
|
|
style: rule.style,
|
|
|
|
attr: attr,
|
|
|
|
index: l,
|
|
|
|
});
|
|
|
|
}
|
2017-11-04 23:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 17:31:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Catch any random exceptions that happen here: all sorts of things can go
|
|
|
|
// wrong with this (nulls, SecurityErrors) and mostly it's for other
|
|
|
|
// stylesheets that we don't want to proces anyway. We should not propagate an
|
|
|
|
// exception out since this will cause the app to fail to start.
|
|
|
|
console.log("Failed to calculate CSS fixups for a stylesheet: " + ss.href, e);
|
2017-11-04 23:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 10:14:16 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
console.log("calcCssFixups end (" +
|
|
|
|
this.cssFixups[this.theme].length +
|
|
|
|
" fixups)");
|
|
|
|
}
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyCssFixups() {
|
2017-11-15 10:14:16 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
console.log("applyCssFixups start (" +
|
|
|
|
this.cssFixups[this.theme].length +
|
|
|
|
" fixups)");
|
|
|
|
}
|
2017-11-08 14:55:07 +00:00
|
|
|
for (let i = 0; i < this.cssFixups[this.theme].length; i++) {
|
|
|
|
const cssFixup = this.cssFixups[this.theme][i];
|
2017-11-14 14:27:27 +00:00
|
|
|
try {
|
|
|
|
cssFixup.style[cssFixup.attr] = this.colors[cssFixup.index];
|
2017-11-15 10:14:16 +00:00
|
|
|
} catch (e) {
|
2017-11-14 14:27:27 +00:00
|
|
|
// Firefox Quantum explodes if you manually edit the CSS in the
|
|
|
|
// inspector and then try to do a tint, as apparently all the
|
|
|
|
// fixups are then stale.
|
|
|
|
console.error("Failed to apply cssFixup in Tinter! ", e.name);
|
|
|
|
}
|
2017-11-04 23:50:57 +00:00
|
|
|
}
|
|
|
|
if (DEBUG) console.log("applyCssFixups end");
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
2017-11-04 23:50:57 +00:00
|
|
|
|
2016-01-07 03:59:09 +00:00
|
|
|
// XXX: we could just move this all into TintableSvg, but as it's so similar
|
|
|
|
// to the CSS fixup stuff in Tinter (just that the fixups are stored in TintableSvg)
|
|
|
|
// keeping it here for now.
|
2019-01-03 18:55:56 +00:00
|
|
|
calcSvgFixups(svgs) {
|
2016-01-07 03:59:09 +00:00
|
|
|
// go through manually fixing up SVG colours.
|
|
|
|
// we could do this by stylesheets, but keeping the stylesheets
|
|
|
|
// updated would be a PITA, so just brute-force search for the
|
|
|
|
// key colour; cache the element and apply.
|
|
|
|
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("calcSvgFixups start for " + svgs);
|
2017-10-11 16:56:17 +00:00
|
|
|
const fixups = [];
|
|
|
|
for (let i = 0; i < svgs.length; i++) {
|
2017-11-15 10:14:16 +00:00
|
|
|
let svgDoc;
|
2016-01-23 18:59:37 +00:00
|
|
|
try {
|
2016-09-16 01:53:41 +00:00
|
|
|
svgDoc = svgs[i].contentDocument;
|
2017-11-16 13:19:36 +00:00
|
|
|
} catch (e) {
|
2017-10-11 16:56:17 +00:00
|
|
|
let msg = 'Failed to get svg.contentDocument of ' + svgs[i].toString();
|
2016-01-23 18:59:37 +00:00
|
|
|
if (e.message) {
|
|
|
|
msg += e.message;
|
|
|
|
}
|
|
|
|
if (e.stack) {
|
|
|
|
msg += ' | stack: ' + e.stack;
|
|
|
|
}
|
2017-11-15 10:14:16 +00:00
|
|
|
console.error(msg);
|
2016-01-23 18:59:37 +00:00
|
|
|
}
|
2016-01-07 03:59:09 +00:00
|
|
|
if (!svgDoc) continue;
|
2017-10-11 16:56:17 +00:00
|
|
|
const tags = svgDoc.getElementsByTagName("*");
|
|
|
|
for (let j = 0; j < tags.length; j++) {
|
|
|
|
const tag = tags[j];
|
2017-11-08 14:55:07 +00:00
|
|
|
for (let k = 0; k < this.svgAttrs.length; k++) {
|
|
|
|
const attr = this.svgAttrs[k];
|
2019-01-03 18:55:56 +00:00
|
|
|
for (let l = 0; l < this.keyHex.length; l++) {
|
|
|
|
if (tag.getAttribute(attr) &&
|
|
|
|
tag.getAttribute(attr).toUpperCase() === this.keyHex[l]) {
|
2016-01-07 03:59:09 +00:00
|
|
|
fixups.push({
|
|
|
|
node: tag,
|
|
|
|
attr: attr,
|
2019-01-03 18:55:56 +00:00
|
|
|
index: l,
|
2016-01-07 03:59:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("calcSvgFixups end");
|
2016-01-07 03:59:09 +00:00
|
|
|
|
|
|
|
return fixups;
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
2016-01-07 03:59:09 +00:00
|
|
|
|
2017-11-08 14:55:07 +00:00
|
|
|
applySvgFixups(fixups) {
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("applySvgFixups start for " + fixups);
|
2017-10-11 16:56:17 +00:00
|
|
|
for (let i = 0; i < fixups.length; i++) {
|
|
|
|
const svgFixup = fixups[i];
|
2019-01-03 18:55:56 +00:00
|
|
|
svgFixup.node.setAttribute(svgFixup.attr, this.colors[svgFixup.index]);
|
2016-01-06 02:29:08 +00:00
|
|
|
}
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("applySvgFixups end");
|
2017-11-08 14:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global.singletonTinter === undefined) {
|
|
|
|
global.singletonTinter = new Tinter();
|
|
|
|
}
|
|
|
|
export default global.singletonTinter;
|