UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = hslToColorString;
5var _hsl = _interopRequireDefault(require("./hsl"));
6var _hsla = _interopRequireDefault(require("./hsla"));
7var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9/**
10 * Converts a HslColor or HslaColor object to a color string.
11 * This util is useful in case you only know on runtime which color object is
12 * used. Otherwise we recommend to rely on `hsl` or `hsla`.
13 *
14 * @example
15 * // Styles as object usage
16 * const styles = {
17 * background: hslToColorString({ hue: 240, saturation: 1, lightness: 0.5 }),
18 * background: hslToColorString({ hue: 360, saturation: 0.75, lightness: 0.4, alpha: 0.72 }),
19 * }
20 *
21 * // styled-components usage
22 * const div = styled.div`
23 * background: ${hslToColorString({ hue: 240, saturation: 1, lightness: 0.5 })};
24 * background: ${hslToColorString({ hue: 360, saturation: 0.75, lightness: 0.4, alpha: 0.72 })};
25 * `
26 *
27 * // CSS in JS Output
28 * element {
29 * background: "#00f";
30 * background: "rgba(179,25,25,0.72)";
31 * }
32 */
33function hslToColorString(color) {
34 if (typeof color === 'object' && typeof color.hue === 'number' && typeof color.saturation === 'number' && typeof color.lightness === 'number') {
35 if (color.alpha && typeof color.alpha === 'number') {
36 return (0, _hsla["default"])({
37 hue: color.hue,
38 saturation: color.saturation,
39 lightness: color.lightness,
40 alpha: color.alpha
41 });
42 }
43 return (0, _hsl["default"])({
44 hue: color.hue,
45 saturation: color.saturation,
46 lightness: color.lightness
47 });
48 }
49 throw new _errors["default"](45);
50}
51module.exports = exports.default;
\No newline at end of file