UNPKG

1.7 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = rgbToColorString;
5var _rgb = _interopRequireDefault(require("./rgb"));
6var _rgba = _interopRequireDefault(require("./rgba"));
7var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9/**
10 * Converts a RgbColor or RgbaColor 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 `rgb` or `rgba`.
13 *
14 * @example
15 * // Styles as object usage
16 * const styles = {
17 * background: rgbToColorString({ red: 255, green: 205, blue: 100 }),
18 * background: rgbToColorString({ red: 255, green: 205, blue: 100, alpha: 0.72 }),
19 * }
20 *
21 * // styled-components usage
22 * const div = styled.div`
23 * background: ${rgbToColorString({ red: 255, green: 205, blue: 100 })};
24 * background: ${rgbToColorString({ red: 255, green: 205, blue: 100, alpha: 0.72 })};
25 * `
26 *
27 * // CSS in JS Output
28 * element {
29 * background: "#ffcd64";
30 * background: "rgba(255,205,100,0.72)";
31 * }
32 */
33function rgbToColorString(color) {
34 if (typeof color === 'object' && typeof color.red === 'number' && typeof color.green === 'number' && typeof color.blue === 'number') {
35 if (typeof color.alpha === 'number') {
36 return (0, _rgba["default"])({
37 red: color.red,
38 green: color.green,
39 blue: color.blue,
40 alpha: color.alpha
41 });
42 }
43 return (0, _rgb["default"])({
44 red: color.red,
45 green: color.green,
46 blue: color.blue
47 });
48 }
49 throw new _errors["default"](46);
50}
51module.exports = exports.default;
\No newline at end of file