UNPKG

1.92 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = hsla;
5var _hslToHex = _interopRequireDefault(require("../internalHelpers/_hslToHex"));
6var _hslToRgb = _interopRequireDefault(require("../internalHelpers/_hslToRgb"));
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 * @example
13 * // Styles as object usage
14 * const styles = {
15 * background: hsla(359, 0.75, 0.4, 0.7),
16 * background: hsla({ hue: 360, saturation: 0.75, lightness: 0.4, alpha: 0,7 }),
17 * background: hsla(359, 0.75, 0.4, 1),
18 * }
19 *
20 * // styled-components usage
21 * const div = styled.div`
22 * background: ${hsla(359, 0.75, 0.4, 0.7)};
23 * background: ${hsla({ hue: 360, saturation: 0.75, lightness: 0.4, alpha: 0,7 })};
24 * background: ${hsla(359, 0.75, 0.4, 1)};
25 * `
26 *
27 * // CSS in JS Output
28 *
29 * element {
30 * background: "rgba(179,25,28,0.7)";
31 * background: "rgba(179,25,28,0.7)";
32 * background: "#b3191c";
33 * }
34 */
35function hsla(value, saturation, lightness, alpha) {
36 if (typeof value === 'number' && typeof saturation === 'number' && typeof lightness === 'number' && typeof alpha === 'number') {
37 return alpha >= 1 ? (0, _hslToHex["default"])(value, saturation, lightness) : "rgba(" + (0, _hslToRgb["default"])(value, saturation, lightness) + "," + alpha + ")";
38 } else if (typeof value === 'object' && saturation === undefined && lightness === undefined && alpha === undefined) {
39 return value.alpha >= 1 ? (0, _hslToHex["default"])(value.hue, value.saturation, value.lightness) : "rgba(" + (0, _hslToRgb["default"])(value.hue, value.saturation, value.lightness) + "," + value.alpha + ")";
40 }
41 throw new _errors["default"](2);
42}
43module.exports = exports.default;
\No newline at end of file