UNPKG

2.5 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = rgba;
5var _parseToRgb = _interopRequireDefault(require("./parseToRgb"));
6var _rgb = _interopRequireDefault(require("./rgb"));
7var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9/**
10 * Returns a string value for the color. The returned result is the smallest possible rgba or hex notation.
11 *
12 * Can also be used to fade a color by passing a hex value or named CSS color along with an alpha value.
13 *
14 * @example
15 * // Styles as object usage
16 * const styles = {
17 * background: rgba(255, 205, 100, 0.7),
18 * background: rgba({ red: 255, green: 205, blue: 100, alpha: 0.7 }),
19 * background: rgba(255, 205, 100, 1),
20 * background: rgba('#ffffff', 0.4),
21 * background: rgba('black', 0.7),
22 * }
23 *
24 * // styled-components usage
25 * const div = styled.div`
26 * background: ${rgba(255, 205, 100, 0.7)};
27 * background: ${rgba({ red: 255, green: 205, blue: 100, alpha: 0.7 })};
28 * background: ${rgba(255, 205, 100, 1)};
29 * background: ${rgba('#ffffff', 0.4)};
30 * background: ${rgba('black', 0.7)};
31 * `
32 *
33 * // CSS in JS Output
34 *
35 * element {
36 * background: "rgba(255,205,100,0.7)";
37 * background: "rgba(255,205,100,0.7)";
38 * background: "#ffcd64";
39 * background: "rgba(255,255,255,0.4)";
40 * background: "rgba(0,0,0,0.7)";
41 * }
42 */
43function rgba(firstValue, secondValue, thirdValue, fourthValue) {
44 if (typeof firstValue === 'string' && typeof secondValue === 'number') {
45 var rgbValue = (0, _parseToRgb["default"])(firstValue);
46 return "rgba(" + rgbValue.red + "," + rgbValue.green + "," + rgbValue.blue + "," + secondValue + ")";
47 } else if (typeof firstValue === 'number' && typeof secondValue === 'number' && typeof thirdValue === 'number' && typeof fourthValue === 'number') {
48 return fourthValue >= 1 ? (0, _rgb["default"])(firstValue, secondValue, thirdValue) : "rgba(" + firstValue + "," + secondValue + "," + thirdValue + "," + fourthValue + ")";
49 } else if (typeof firstValue === 'object' && secondValue === undefined && thirdValue === undefined && fourthValue === undefined) {
50 return firstValue.alpha >= 1 ? (0, _rgb["default"])(firstValue.red, firstValue.green, firstValue.blue) : "rgba(" + firstValue.red + "," + firstValue.green + "," + firstValue.blue + "," + firstValue.alpha + ")";
51 }
52 throw new _errors["default"](7);
53}
54module.exports = exports.default;
\No newline at end of file