UNPKG

2.12 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5var _curry = _interopRequireDefault(require("../internalHelpers/_curry"));
6var _guard = _interopRequireDefault(require("../internalHelpers/_guard"));
7var _rgba = _interopRequireDefault(require("./rgba"));
8var _parseToRgb = _interopRequireDefault(require("./parseToRgb"));
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10function _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); }
11/**
12 * Increases the opacity of a color. Its range for the amount is between 0 to 1.
13 *
14 *
15 * @example
16 * // Styles as object usage
17 * const styles = {
18 * background: opacify(0.1, 'rgba(255, 255, 255, 0.9)');
19 * background: opacify(0.2, 'hsla(0, 0%, 100%, 0.5)'),
20 * background: opacify('0.5', 'rgba(255, 0, 0, 0.2)'),
21 * }
22 *
23 * // styled-components usage
24 * const div = styled.div`
25 * background: ${opacify(0.1, 'rgba(255, 255, 255, 0.9)')};
26 * background: ${opacify(0.2, 'hsla(0, 0%, 100%, 0.5)')},
27 * background: ${opacify('0.5', 'rgba(255, 0, 0, 0.2)')},
28 * `
29 *
30 * // CSS in JS Output
31 *
32 * element {
33 * background: "#fff";
34 * background: "rgba(255,255,255,0.7)";
35 * background: "rgba(255,0,0,0.7)";
36 * }
37 */
38function opacify(amount, color) {
39 if (color === 'transparent') return color;
40 var parsedColor = (0, _parseToRgb["default"])(color);
41 var alpha = typeof parsedColor.alpha === 'number' ? parsedColor.alpha : 1;
42 var colorWithAlpha = _extends({}, parsedColor, {
43 alpha: (0, _guard["default"])(0, 1, (alpha * 100 + parseFloat(amount) * 100) / 100)
44 });
45 return (0, _rgba["default"])(colorWithAlpha);
46}
47
48// prettier-ignore
49var curriedOpacify = (0, _curry["default"] /* ::<number | string, string, string> */)(opacify);
50var _default = exports["default"] = curriedOpacify;
51module.exports = exports.default;
\No newline at end of file