mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
83 lines • 3.23 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.setThemeDefaults = exports.blendColors = exports.changeOpacity = exports.getComponents = exports.makeStyleFromTheme = void 0;
var constants_1 = require("../constants");
function makeStyleFromTheme(getStyleFromTheme) {
var lastTheme;
var style;
return function (theme) {
if (!style || theme !== lastTheme) {
style = getStyleFromTheme(theme);
lastTheme = theme;
}
return style;
};
}
exports.makeStyleFromTheme = makeStyleFromTheme;
var rgbPattern = /^rgba?\((\d+),(\d+),(\d+)(?:,([\d.]+))?\)$/;
function getComponents(inColor) {
var color = inColor;
// RGB color
var match = rgbPattern.exec(color);
if (match) {
return {
red: parseInt(match[1], 10),
green: parseInt(match[2], 10),
blue: parseInt(match[3], 10),
alpha: match[4] ? parseFloat(match[4]) : 1,
};
}
// Hex color
if (color[0] === '#') {
color = color.slice(1);
}
if (color.length === 3) {
var tempColor = color;
color = '';
color += tempColor[0] + tempColor[0];
color += tempColor[1] + tempColor[1];
color += tempColor[2] + tempColor[2];
}
return {
red: parseInt(color.substring(0, 2), 16),
green: parseInt(color.substring(2, 4), 16),
blue: parseInt(color.substring(4, 6), 16),
alpha: 1,
};
}
exports.getComponents = getComponents;
function changeOpacity(oldColor, opacity) {
var _a = getComponents(oldColor), red = _a.red, green = _a.green, blue = _a.blue, alpha = _a.alpha;
return "rgba(" + red + "," + green + "," + blue + "," + alpha * opacity + ")";
}
exports.changeOpacity = changeOpacity;
function blendComponent(background, foreground, opacity) {
return ((1 - opacity) * background) + (opacity * foreground);
}
function blendColors(background, foreground, opacity) {
var backgroundComponents = getComponents(background);
var foregroundComponents = getComponents(foreground);
var red = Math.floor(blendComponent(backgroundComponents.red, foregroundComponents.red, opacity));
var green = Math.floor(blendComponent(backgroundComponents.green, foregroundComponents.green, opacity));
var blue = Math.floor(blendComponent(backgroundComponents.blue, foregroundComponents.blue, opacity));
var alpha = blendComponent(backgroundComponents.alpha, foregroundComponents.alpha, opacity);
return "rgba(" + red + "," + green + "," + blue + "," + alpha + ")";
}
exports.blendColors = blendColors;
// setThemeDefaults will set defaults on the theme for any unset properties.
function setThemeDefaults(theme) {
var defaultTheme = constants_1.Preferences.THEMES.default;
for (var property in defaultTheme) {
if (property === 'type') {
continue;
}
if (theme[property] == null) {
theme[property] = defaultTheme[property];
}
}
return theme;
}
exports.setThemeDefaults = setThemeDefaults;
//# sourceMappingURL=theme_utils.js.map