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-25 21:48:27 +00:00
|
|
|
import UserSettingsStore from './UserSettingsStore';
|
|
|
|
|
2016-04-16 00:00:10 +00:00
|
|
|
// FIXME: these vars should be bundled up and attached to
|
2016-01-06 02:41:10 +00:00
|
|
|
// module.exports otherwise this will break when included by both
|
|
|
|
// react-sdk and apps layered on top.
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const DEBUG = 0;
|
2016-04-16 00:00:10 +00:00
|
|
|
|
2016-09-16 01:53:41 +00:00
|
|
|
// The colour keys to be replaced as referred to in CSS
|
2017-10-11 16:56:17 +00:00
|
|
|
const keyRgb = [
|
2016-01-06 02:29:08 +00:00
|
|
|
"rgb(118, 207, 166)", // Vector Green
|
|
|
|
"rgb(234, 245, 240)", // Vector Light Green
|
2016-09-16 01:53:41 +00:00
|
|
|
"rgb(211, 239, 225)", // BottomLeftMenu overlay (20% Vector Green)
|
2016-01-05 00:46:52 +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
|
2016-10-26 22:11:34 +00:00
|
|
|
// x = (255 - 234) / (255 - 118) = 0.16
|
2016-01-05 00:46:52 +00:00
|
|
|
|
2016-01-06 02:29:08 +00:00
|
|
|
// The colour keys to be replaced as referred to in SVGs
|
2017-10-11 16:56:17 +00:00
|
|
|
const keyHex = [
|
2016-01-06 02:29:08 +00:00
|
|
|
"#76CFA6", // Vector Green
|
|
|
|
"#EAF5F0", // Vector Light Green
|
|
|
|
"#D3EFE1", // BottomLeftMenu overlay (20% Vector Green overlaid on Vector Light Green)
|
2017-01-17 19:13:04 +00:00
|
|
|
"#FFFFFF", // white highlights of the SVGs (for switching to dark theme)
|
2016-01-05 03:34:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// cache of our replacement colours
|
|
|
|
// defaults to our keys.
|
2017-10-11 16:56:17 +00:00
|
|
|
const colors = [
|
2016-01-05 03:34:52 +00:00
|
|
|
keyHex[0],
|
|
|
|
keyHex[1],
|
|
|
|
keyHex[2],
|
2017-01-17 19:13:04 +00:00
|
|
|
keyHex[3],
|
2016-01-05 00:46:52 +00:00
|
|
|
];
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const cssFixups = [
|
2016-01-05 00:46:52 +00:00
|
|
|
// {
|
|
|
|
// 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
|
2017-10-11 16:56:17 +00:00
|
|
|
const cssAttrs = [
|
2016-01-05 00:46:52 +00:00
|
|
|
"color",
|
|
|
|
"backgroundColor",
|
|
|
|
"borderColor",
|
2016-01-09 01:05:43 +00:00
|
|
|
"borderTopColor",
|
2016-01-20 17:24:09 +00:00
|
|
|
"borderBottomColor",
|
2016-02-18 00:32:46 +00:00
|
|
|
"borderLeftColor",
|
2016-01-05 00:46:52 +00:00
|
|
|
];
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const svgAttrs = [
|
2016-01-05 00:46:52 +00:00
|
|
|
"fill",
|
|
|
|
"stroke",
|
|
|
|
];
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let cached = false;
|
2016-01-05 00:46:52 +00:00
|
|
|
|
|
|
|
function calcCssFixups() {
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("calcSvgFixups start");
|
2017-10-11 16:56:17 +00:00
|
|
|
for (let i = 0; i < document.styleSheets.length; i++) {
|
|
|
|
const ss = document.styleSheets[i];
|
2016-02-09 15:07:57 +00:00
|
|
|
if (!ss) continue; // well done safari >:(
|
2016-01-22 19:59:41 +00:00
|
|
|
// Chromium apparently sometimes returns null here; unsure why.
|
|
|
|
// see $14534907369972FRXBx:matrix.org in HQ
|
2016-01-23 19:46:00 +00:00
|
|
|
// ...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
|
2016-01-24 11:46:25 +00:00
|
|
|
// ...except some browsers apparently return stylesheets without
|
|
|
|
// hrefs, which we have no choice but ignore right now
|
2016-10-26 22:11:34 +00:00
|
|
|
|
|
|
|
// 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?
|
|
|
|
|
|
|
|
if (ss.href && !ss.href.match(/\/bundle.*\.css$/)) continue;
|
|
|
|
|
2016-01-22 19:59:41 +00:00
|
|
|
if (!ss.cssRules) continue;
|
2017-10-11 16:56:17 +00:00
|
|
|
for (let j = 0; j < ss.cssRules.length; j++) {
|
|
|
|
const rule = ss.cssRules[j];
|
2016-01-06 02:29:08 +00:00
|
|
|
if (!rule.style) continue;
|
2017-10-11 16:56:17 +00:00
|
|
|
for (let k = 0; k < cssAttrs.length; k++) {
|
|
|
|
const attr = cssAttrs[k];
|
|
|
|
for (let l = 0; l < keyRgb.length; l++) {
|
2016-01-06 02:29:08 +00:00
|
|
|
if (rule.style[attr] === keyRgb[l]) {
|
2016-01-05 00:46:52 +00:00
|
|
|
cssFixups.push({
|
|
|
|
style: rule.style,
|
|
|
|
attr: attr,
|
|
|
|
index: l,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("calcSvgFixups end");
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 03:34:52 +00:00
|
|
|
function applyCssFixups() {
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("applyCssFixups start");
|
2017-10-11 16:56:17 +00:00
|
|
|
for (let i = 0; i < cssFixups.length; i++) {
|
|
|
|
const cssFixup = cssFixups[i];
|
2016-01-05 00:46:52 +00:00
|
|
|
cssFixup.style[cssFixup.attr] = colors[cssFixup.index];
|
|
|
|
}
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("applyCssFixups end");
|
2016-01-05 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hexToRgb(color) {
|
|
|
|
if (color[0] === '#') color = color.slice(1);
|
|
|
|
if (color.length === 3) {
|
|
|
|
color = color[0] + color[0] +
|
|
|
|
color[1] + color[1] +
|
|
|
|
color[2] + color[2];
|
|
|
|
}
|
2017-10-11 16:56:17 +00:00
|
|
|
const val = parseInt(color, 16);
|
|
|
|
const r = (val >> 16) & 255;
|
|
|
|
const g = (val >> 8) & 255;
|
|
|
|
const b = val & 255;
|
2016-01-05 00:46:52 +00:00
|
|
|
return [r, g, b];
|
|
|
|
}
|
|
|
|
|
|
|
|
function rgbToHex(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
|
|
|
}
|
|
|
|
|
2016-11-16 14:16:51 +00:00
|
|
|
// List of functions to call when the tint changes.
|
|
|
|
const tintables = [];
|
|
|
|
|
2016-01-05 00:46:52 +00:00
|
|
|
module.exports = {
|
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.
|
|
|
|
*
|
|
|
|
* @param {Function} tintable Function to call when the tint changes.
|
|
|
|
*/
|
2017-10-11 16:56:17 +00:00
|
|
|
registerTintable: function(tintable) {
|
2016-11-16 14:16:51 +00:00
|
|
|
tintables.push(tintable);
|
|
|
|
},
|
|
|
|
|
2017-01-18 14:06:47 +00:00
|
|
|
tint: function(primaryColor, secondaryColor, tertiaryColor) {
|
2016-01-05 00:46:52 +00:00
|
|
|
if (!cached) {
|
|
|
|
calcCssFixups();
|
|
|
|
cached = true;
|
|
|
|
}
|
|
|
|
|
2016-01-08 03:22:38 +00:00
|
|
|
if (!primaryColor) {
|
2017-10-25 21:48:27 +00:00
|
|
|
const theme = UserSettingsStore.getTheme();
|
|
|
|
// FIXME: get this out of the theme CSS itself somehow?
|
|
|
|
// we could store it in a string CSS attrib somewhere we could sniff...
|
|
|
|
primaryColor = theme === 'status' ? "#6CC1F6" : "#76CFA6"; // Vector green
|
|
|
|
secondaryColor = theme === 'status' ? "#586C7B" : "#EAF5F0"; // Vector light green
|
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-10-11 16:56:17 +00:00
|
|
|
const rgb = hexToRgb(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;
|
|
|
|
secondaryColor = rgbToHex(rgb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tertiaryColor) {
|
2017-01-20 14:22:27 +00:00
|
|
|
const x = 0.19;
|
2017-10-11 16:56:17 +00:00
|
|
|
const rgb1 = hexToRgb(primaryColor);
|
|
|
|
const rgb2 = hexToRgb(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];
|
|
|
|
tertiaryColor = rgbToHex(rgb1);
|
|
|
|
}
|
|
|
|
|
2016-01-08 03:22:38 +00:00
|
|
|
if (colors[0] === primaryColor &&
|
|
|
|
colors[1] === secondaryColor &&
|
2017-10-11 16:56:17 +00:00
|
|
|
colors[2] === tertiaryColor) {
|
2016-01-08 03:22:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:06:47 +00:00
|
|
|
colors[0] = primaryColor;
|
|
|
|
colors[1] = secondaryColor;
|
|
|
|
colors[2] = tertiaryColor;
|
2016-01-05 03:34:52 +00:00
|
|
|
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("Tinter.tint");
|
|
|
|
|
2016-01-05 00:46:52 +00:00
|
|
|
// go through manually fixing up the stylesheets.
|
2016-01-05 03:34:52 +00:00
|
|
|
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
|
2016-11-16 14:16:51 +00:00
|
|
|
tintables.forEach(function(tintable) {
|
|
|
|
tintable();
|
|
|
|
});
|
2016-01-06 02:29:08 +00:00
|
|
|
},
|
|
|
|
|
2017-01-18 14:06:47 +00:00
|
|
|
tintSvgWhite: function(whiteColor) {
|
|
|
|
if (!whiteColor) {
|
|
|
|
whiteColor = colors[3];
|
|
|
|
}
|
|
|
|
if (colors[3] === whiteColor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
colors[3] = whiteColor;
|
|
|
|
tintables.forEach(function(tintable) {
|
|
|
|
tintable();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
calcSvgFixups: function(svgs) {
|
|
|
|
// 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++) {
|
2016-01-23 18:59:37 +00:00
|
|
|
var svgDoc;
|
|
|
|
try {
|
2016-09-16 01:53:41 +00:00
|
|
|
svgDoc = svgs[i].contentDocument;
|
2017-10-11 16:56:17 +00:00
|
|
|
} catch(e) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
console.error(e);
|
|
|
|
}
|
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];
|
|
|
|
for (let k = 0; k < svgAttrs.length; k++) {
|
|
|
|
const attr = svgAttrs[k];
|
|
|
|
for (let l = 0; l < keyHex.length; l++) {
|
2016-01-07 03:59:09 +00:00
|
|
|
if (tag.getAttribute(attr) && tag.getAttribute(attr).toUpperCase() === keyHex[l]) {
|
|
|
|
fixups.push({
|
|
|
|
node: tag,
|
|
|
|
attr: attr,
|
|
|
|
index: l,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 00:00:10 +00:00
|
|
|
if (DEBUG) console.log("calcSvgFixups end");
|
2016-01-07 03:59:09 +00:00
|
|
|
|
|
|
|
return fixups;
|
|
|
|
},
|
|
|
|
|
|
|
|
applySvgFixups: function(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];
|
2016-01-07 03:59:09 +00:00
|
|
|
svgFixup.node.setAttribute(svgFixup.attr, 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-10-11 16:56:17 +00:00
|
|
|
},
|
2016-01-05 00:46:52 +00:00
|
|
|
};
|