UNPKG

5.31 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = parseToRgb;
5var _hslToRgb = _interopRequireDefault(require("../internalHelpers/_hslToRgb"));
6var _nameToHex = _interopRequireDefault(require("../internalHelpers/_nameToHex"));
7var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9var hexRegex = /^#[a-fA-F0-9]{6}$/;
10var hexRgbaRegex = /^#[a-fA-F0-9]{8}$/;
11var reducedHexRegex = /^#[a-fA-F0-9]{3}$/;
12var reducedRgbaHexRegex = /^#[a-fA-F0-9]{4}$/;
13var rgbRegex = /^rgb\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*\)$/i;
14var rgbaRegex = /^rgb(?:a)?\(\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,)?\s*(\d{1,3})\s*(?:,|\/)\s*([-+]?\d*[.]?\d+[%]?)\s*\)$/i;
15var hslRegex = /^hsl\(\s*(\d{0,3}[.]?[0-9]+(?:deg)?)\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*\)$/i;
16var hslaRegex = /^hsl(?:a)?\(\s*(\d{0,3}[.]?[0-9]+(?:deg)?)\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,)?\s*(\d{1,3}[.]?[0-9]?)%\s*(?:,|\/)\s*([-+]?\d*[.]?\d+[%]?)\s*\)$/i;
17
18/**
19 * Returns an RgbColor or RgbaColor object. This utility function is only useful
20 * if want to extract a color component. With the color util `toColorString` you
21 * can convert a RgbColor or RgbaColor object back to a string.
22 *
23 * @example
24 * // Assigns `{ red: 255, green: 0, blue: 0 }` to color1
25 * const color1 = parseToRgb('rgb(255, 0, 0)');
26 * // Assigns `{ red: 92, green: 102, blue: 112, alpha: 0.75 }` to color2
27 * const color2 = parseToRgb('hsla(210, 10%, 40%, 0.75)');
28 */
29function parseToRgb(color) {
30 if (typeof color !== 'string') {
31 throw new _errors["default"](3);
32 }
33 var normalizedColor = (0, _nameToHex["default"])(color);
34 if (normalizedColor.match(hexRegex)) {
35 return {
36 red: parseInt("" + normalizedColor[1] + normalizedColor[2], 16),
37 green: parseInt("" + normalizedColor[3] + normalizedColor[4], 16),
38 blue: parseInt("" + normalizedColor[5] + normalizedColor[6], 16)
39 };
40 }
41 if (normalizedColor.match(hexRgbaRegex)) {
42 var alpha = parseFloat((parseInt("" + normalizedColor[7] + normalizedColor[8], 16) / 255).toFixed(2));
43 return {
44 red: parseInt("" + normalizedColor[1] + normalizedColor[2], 16),
45 green: parseInt("" + normalizedColor[3] + normalizedColor[4], 16),
46 blue: parseInt("" + normalizedColor[5] + normalizedColor[6], 16),
47 alpha: alpha
48 };
49 }
50 if (normalizedColor.match(reducedHexRegex)) {
51 return {
52 red: parseInt("" + normalizedColor[1] + normalizedColor[1], 16),
53 green: parseInt("" + normalizedColor[2] + normalizedColor[2], 16),
54 blue: parseInt("" + normalizedColor[3] + normalizedColor[3], 16)
55 };
56 }
57 if (normalizedColor.match(reducedRgbaHexRegex)) {
58 var _alpha = parseFloat((parseInt("" + normalizedColor[4] + normalizedColor[4], 16) / 255).toFixed(2));
59 return {
60 red: parseInt("" + normalizedColor[1] + normalizedColor[1], 16),
61 green: parseInt("" + normalizedColor[2] + normalizedColor[2], 16),
62 blue: parseInt("" + normalizedColor[3] + normalizedColor[3], 16),
63 alpha: _alpha
64 };
65 }
66 var rgbMatched = rgbRegex.exec(normalizedColor);
67 if (rgbMatched) {
68 return {
69 red: parseInt("" + rgbMatched[1], 10),
70 green: parseInt("" + rgbMatched[2], 10),
71 blue: parseInt("" + rgbMatched[3], 10)
72 };
73 }
74 var rgbaMatched = rgbaRegex.exec(normalizedColor.substring(0, 50));
75 if (rgbaMatched) {
76 return {
77 red: parseInt("" + rgbaMatched[1], 10),
78 green: parseInt("" + rgbaMatched[2], 10),
79 blue: parseInt("" + rgbaMatched[3], 10),
80 alpha: parseFloat("" + rgbaMatched[4]) > 1 ? parseFloat("" + rgbaMatched[4]) / 100 : parseFloat("" + rgbaMatched[4])
81 };
82 }
83 var hslMatched = hslRegex.exec(normalizedColor);
84 if (hslMatched) {
85 var hue = parseInt("" + hslMatched[1], 10);
86 var saturation = parseInt("" + hslMatched[2], 10) / 100;
87 var lightness = parseInt("" + hslMatched[3], 10) / 100;
88 var rgbColorString = "rgb(" + (0, _hslToRgb["default"])(hue, saturation, lightness) + ")";
89 var hslRgbMatched = rgbRegex.exec(rgbColorString);
90 if (!hslRgbMatched) {
91 throw new _errors["default"](4, normalizedColor, rgbColorString);
92 }
93 return {
94 red: parseInt("" + hslRgbMatched[1], 10),
95 green: parseInt("" + hslRgbMatched[2], 10),
96 blue: parseInt("" + hslRgbMatched[3], 10)
97 };
98 }
99 var hslaMatched = hslaRegex.exec(normalizedColor.substring(0, 50));
100 if (hslaMatched) {
101 var _hue = parseInt("" + hslaMatched[1], 10);
102 var _saturation = parseInt("" + hslaMatched[2], 10) / 100;
103 var _lightness = parseInt("" + hslaMatched[3], 10) / 100;
104 var _rgbColorString = "rgb(" + (0, _hslToRgb["default"])(_hue, _saturation, _lightness) + ")";
105 var _hslRgbMatched = rgbRegex.exec(_rgbColorString);
106 if (!_hslRgbMatched) {
107 throw new _errors["default"](4, normalizedColor, _rgbColorString);
108 }
109 return {
110 red: parseInt("" + _hslRgbMatched[1], 10),
111 green: parseInt("" + _hslRgbMatched[2], 10),
112 blue: parseInt("" + _hslRgbMatched[3], 10),
113 alpha: parseFloat("" + hslaMatched[4]) > 1 ? parseFloat("" + hslaMatched[4]) / 100 : parseFloat("" + hslaMatched[4])
114 };
115 }
116 throw new _errors["default"](5);
117}
118module.exports = exports.default;
\No newline at end of file