UNPKG

2.95 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5var _curry = _interopRequireDefault(require("../internalHelpers/_curry"));
6var _rgba = _interopRequireDefault(require("./rgba"));
7var _parseToRgb = _interopRequireDefault(require("./parseToRgb"));
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
10/**
11 * Mixes the two provided colors together by calculating the average of each of the RGB components weighted to the first color by the provided weight.
12 *
13 * @example
14 * // Styles as object usage
15 * const styles = {
16 * background: mix(0.5, '#f00', '#00f')
17 * background: mix(0.25, '#f00', '#00f')
18 * background: mix('0.5', 'rgba(255, 0, 0, 0.5)', '#00f')
19 * }
20 *
21 * // styled-components usage
22 * const div = styled.div`
23 * background: ${mix(0.5, '#f00', '#00f')};
24 * background: ${mix(0.25, '#f00', '#00f')};
25 * background: ${mix('0.5', 'rgba(255, 0, 0, 0.5)', '#00f')};
26 * `
27 *
28 * // CSS in JS Output
29 *
30 * element {
31 * background: "#7f007f";
32 * background: "#3f00bf";
33 * background: "rgba(63, 0, 191, 0.75)";
34 * }
35 */
36function mix(weight, color, otherColor) {
37 if (color === 'transparent') return otherColor;
38 if (otherColor === 'transparent') return color;
39 if (weight === 0) return otherColor;
40 var parsedColor1 = (0, _parseToRgb["default"])(color);
41 var color1 = _extends({}, parsedColor1, {
42 alpha: typeof parsedColor1.alpha === 'number' ? parsedColor1.alpha : 1
43 });
44 var parsedColor2 = (0, _parseToRgb["default"])(otherColor);
45 var color2 = _extends({}, parsedColor2, {
46 alpha: typeof parsedColor2.alpha === 'number' ? parsedColor2.alpha : 1
47 });
48
49 // The formula is copied from the original Sass implementation:
50 // http://sass-lang.com/documentation/Sass/Script/Functions.html#mix-instance_method
51 var alphaDelta = color1.alpha - color2.alpha;
52 var x = parseFloat(weight) * 2 - 1;
53 var y = x * alphaDelta === -1 ? x : x + alphaDelta;
54 var z = 1 + x * alphaDelta;
55 var weight1 = (y / z + 1) / 2.0;
56 var weight2 = 1 - weight1;
57 var mixedColor = {
58 red: Math.floor(color1.red * weight1 + color2.red * weight2),
59 green: Math.floor(color1.green * weight1 + color2.green * weight2),
60 blue: Math.floor(color1.blue * weight1 + color2.blue * weight2),
61 alpha: color1.alpha * parseFloat(weight) + color2.alpha * (1 - parseFloat(weight))
62 };
63 return (0, _rgba["default"])(mixedColor);
64}
65
66// prettier-ignore
67var curriedMix = (0, _curry["default"] /* ::<number | string, string, string, string> */)(mix);
68var _default = exports["default"] = curriedMix;
69module.exports = exports.default;
\No newline at end of file