@colorye/react-native-css 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,378 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = CssCalc;
7
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
10
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
11
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
12
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
+ var calcRe = /calc\(([^)]+)\)/g;
14
+ var colorRe1 = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)/g;
15
+ var colorRe2 = /rgba?\(\s*(\d+)\s+(\d+)\s+(\d+)\s*\/\s*([\d.]+)\s*\)/g;
16
+ var itohex = function itohex(component) {
17
+ var hex = Number(component).toString(16);
18
+ return hex.length === 1 ? "0".concat(hex) : hex;
19
+ };
20
+ function splitTopLevelCommas(str) {
21
+ var parts = [];
22
+ var current = "";
23
+ var depth = 0;
24
+ for (var i = 0; i < str.length; i++) {
25
+ var _char = str[i];
26
+ if (_char === "(") {
27
+ depth++;
28
+ current += _char;
29
+ } else if (_char === ")") {
30
+ depth--;
31
+ current += _char;
32
+ } else if (_char === "," && depth === 0) {
33
+ parts.push(current.trim());
34
+ current = "";
35
+ } else {
36
+ current += _char;
37
+ }
38
+ }
39
+ if (current) {
40
+ parts.push(current.trim());
41
+ }
42
+ return parts;
43
+ }
44
+ function parseColorMixArg(arg) {
45
+ arg = arg.trim();
46
+ // Check if there is a percentage at the end
47
+ var percentEndMatch = arg.match(/\s+([\d.]+)%$/i);
48
+ if (percentEndMatch) {
49
+ var percentage = parseFloat(percentEndMatch[1]);
50
+ var colorStr = arg.substring(0, arg.length - percentEndMatch[0].length).trim();
51
+ return {
52
+ colorStr: colorStr,
53
+ percentage: percentage
54
+ };
55
+ }
56
+
57
+ // Check if there is a percentage at the beginning
58
+ var percentStartMatch = arg.match(/^([\d.]+)%\s+/i);
59
+ if (percentStartMatch) {
60
+ var _percentage = parseFloat(percentStartMatch[1]);
61
+ var _colorStr = arg.substring(percentStartMatch[0].length).trim();
62
+ return {
63
+ colorStr: _colorStr,
64
+ percentage: _percentage
65
+ };
66
+ }
67
+ return {
68
+ colorStr: arg,
69
+ percentage: null
70
+ };
71
+ }
72
+ function hslToRgb(h, s, l) {
73
+ h = h / 360;
74
+ s = s / 100;
75
+ l = l / 100;
76
+ var r, g, b;
77
+ if (s === 0) {
78
+ r = g = b = l; // achromatic
79
+ } else {
80
+ var hue2rgb = function hue2rgb(p, q, t) {
81
+ if (t < 0) t += 1;
82
+ if (t > 1) t -= 1;
83
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
84
+ if (t < 1 / 2) return q;
85
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
86
+ return p;
87
+ };
88
+ var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
89
+ var p = 2 * l - q;
90
+ r = hue2rgb(p, q, h + 1 / 3);
91
+ g = hue2rgb(p, q, h);
92
+ b = hue2rgb(p, q, h - 1 / 3);
93
+ }
94
+ return {
95
+ r: Math.round(r * 255),
96
+ g: Math.round(g * 255),
97
+ b: Math.round(b * 255)
98
+ };
99
+ }
100
+ function parseColor(colorStr) {
101
+ if (!colorStr) return null;
102
+ colorStr = colorStr.trim().toLowerCase();
103
+ if (colorStr === "transparent") {
104
+ return {
105
+ r: 0,
106
+ g: 0,
107
+ b: 0,
108
+ a: 0
109
+ };
110
+ }
111
+
112
+ // Hex
113
+ if (colorStr.startsWith("#")) {
114
+ var hex = colorStr.substring(1);
115
+ if (hex.length === 3 || hex.length === 4) {
116
+ var r = parseInt(hex[0] + hex[0], 16);
117
+ var g = parseInt(hex[1] + hex[1], 16);
118
+ var b = parseInt(hex[2] + hex[2], 16);
119
+ var a = hex.length === 4 ? parseInt(hex[3] + hex[3], 16) / 255 : 1;
120
+ return {
121
+ r: r,
122
+ g: g,
123
+ b: b,
124
+ a: a
125
+ };
126
+ }
127
+ if (hex.length === 6 || hex.length === 8) {
128
+ var _r = parseInt(hex.substring(0, 2), 16);
129
+ var _g = parseInt(hex.substring(2, 4), 16);
130
+ var _b = parseInt(hex.substring(4, 6), 16);
131
+ var _a = hex.length === 8 ? parseInt(hex.substring(6, 8), 16) / 255 : 1;
132
+ return {
133
+ r: _r,
134
+ g: _g,
135
+ b: _b,
136
+ a: _a
137
+ };
138
+ }
139
+ return null;
140
+ }
141
+
142
+ // rgb / rgba
143
+ var rgbMatch = colorStr.match(/rgba?\(\s*([\d.]+)(%?)\s*[\s,]\s*([\d.]+)(%?)\s*[\s,]\s*([\d.]+)(%?)(?:\s*[\s,/]\s*([\d.]+)(%?))?\s*\)/);
144
+ if (rgbMatch) {
145
+ var _r2 = parseFloat(rgbMatch[1]);
146
+ if (rgbMatch[2] === "%") _r2 = _r2 / 100 * 255;
147
+ var _g2 = parseFloat(rgbMatch[3]);
148
+ if (rgbMatch[4] === "%") _g2 = _g2 / 100 * 255;
149
+ var _b2 = parseFloat(rgbMatch[5]);
150
+ if (rgbMatch[6] === "%") _b2 = _b2 / 100 * 255;
151
+ var _a2 = 1;
152
+ if (rgbMatch[7] !== undefined) {
153
+ _a2 = parseFloat(rgbMatch[7]);
154
+ if (rgbMatch[8] === "%") _a2 = _a2 / 100;
155
+ }
156
+ return {
157
+ r: Math.round(_r2),
158
+ g: Math.round(_g2),
159
+ b: Math.round(_b2),
160
+ a: _a2
161
+ };
162
+ }
163
+
164
+ // hsl / hsla
165
+ var hslMatch = colorStr.match(/hsla?\(\s*([\d.]+)(?:deg)?\s*[\s,]\s*([\d.]+)%\s*[\s,]\s*([\d.]+)%\s*(?:\s*[\s,/]\s*([\d.]+)(%?))?\s*\)/);
166
+ if (hslMatch) {
167
+ var h = parseFloat(hslMatch[1]);
168
+ var s = parseFloat(hslMatch[2]);
169
+ var l = parseFloat(hslMatch[3]);
170
+ var _a3 = 1;
171
+ if (hslMatch[4] !== undefined) {
172
+ _a3 = parseFloat(hslMatch[4]);
173
+ if (hslMatch[5] === "%") _a3 = _a3 / 100;
174
+ }
175
+ var rgb = hslToRgb(h, s, l);
176
+ return _objectSpread(_objectSpread({}, rgb), {}, {
177
+ a: _a3
178
+ });
179
+ }
180
+
181
+ // Basic named colors fallback
182
+ var basicColors = {
183
+ white: {
184
+ r: 255,
185
+ g: 255,
186
+ b: 255,
187
+ a: 1
188
+ },
189
+ black: {
190
+ r: 0,
191
+ g: 0,
192
+ b: 0,
193
+ a: 1
194
+ },
195
+ red: {
196
+ r: 255,
197
+ g: 0,
198
+ b: 0,
199
+ a: 1
200
+ },
201
+ green: {
202
+ r: 0,
203
+ g: 128,
204
+ b: 0,
205
+ a: 1
206
+ },
207
+ blue: {
208
+ r: 0,
209
+ g: 0,
210
+ b: 255,
211
+ a: 1
212
+ },
213
+ yellow: {
214
+ r: 255,
215
+ g: 255,
216
+ b: 0,
217
+ a: 1
218
+ },
219
+ magenta: {
220
+ r: 255,
221
+ g: 0,
222
+ b: 255,
223
+ a: 1
224
+ },
225
+ cyan: {
226
+ r: 0,
227
+ g: 255,
228
+ b: 255,
229
+ a: 1
230
+ }
231
+ };
232
+ if (basicColors[colorStr]) {
233
+ return basicColors[colorStr];
234
+ }
235
+ return null;
236
+ }
237
+ function resolveColorMix(value) {
238
+ if (typeof value !== "string") return value;
239
+ var index;
240
+ while ((index = value.toLowerCase().indexOf("color-mix(")) !== -1) {
241
+ // Find matching closing parenthesis
242
+ var depth = 1;
243
+ var j = index + "color-mix(".length;
244
+ while (j < value.length && depth > 0) {
245
+ if (value[j] === "(") {
246
+ depth++;
247
+ } else if (value[j] === ")") {
248
+ depth--;
249
+ }
250
+ j++;
251
+ }
252
+ if (depth > 0) {
253
+ // Unmatched parenthesis, break to avoid infinite loop
254
+ break;
255
+ }
256
+ var innerContent = value.substring(index + "color-mix(".length, j - 1);
257
+
258
+ // Parse innerContent: "in <color-space>, <args>"
259
+ var commaIndex = innerContent.indexOf(",");
260
+ if (commaIndex === -1) {
261
+ // Invalid syntax, skip this one by replacing "color-mix(" with a placeholder temporarily
262
+ value = value.substring(0, index) + "COLOR_MIX_TEMP(" + value.substring(index + "color-mix(".length);
263
+ continue;
264
+ }
265
+ var argsPart = innerContent.substring(commaIndex + 1).trim();
266
+
267
+ // Check if argsPart contains another color-mix. If so, resolve the inner one first.
268
+ if (argsPart.toLowerCase().includes("color-mix(")) {
269
+ var resolvedArgsPart = resolveColorMix(argsPart);
270
+ value = value.substring(0, index + "color-mix(".length + commaIndex + 1) + " " + resolvedArgsPart + value.substring(j - 1);
271
+ continue;
272
+ }
273
+
274
+ // Now we can resolve this color-mix
275
+ var args = splitTopLevelCommas(argsPart);
276
+ if (args.length !== 2) {
277
+ // Invalid syntax
278
+ value = value.substring(0, index) + "COLOR_MIX_TEMP(" + value.substring(index + "color-mix(".length);
279
+ continue;
280
+ }
281
+ var arg1 = parseColorMixArg(args[0]);
282
+ var arg2 = parseColorMixArg(args[1]);
283
+ var c1 = parseColor(arg1.colorStr);
284
+ var c2 = parseColor(arg2.colorStr);
285
+ if (!c1 || !c2) {
286
+ // Parsing failed, skip
287
+ value = value.substring(0, index) + "COLOR_MIX_TEMP(" + value.substring(index + "color-mix(".length);
288
+ continue;
289
+ }
290
+ var p1 = arg1.percentage;
291
+ var p2 = arg2.percentage;
292
+ var w1 = void 0,
293
+ w2 = void 0;
294
+ if (p1 !== null && p2 !== null) {
295
+ var sum = p1 + p2;
296
+ if (sum > 100) {
297
+ w1 = p1 / sum * 100;
298
+ w2 = p2 / sum * 100;
299
+ } else {
300
+ w1 = p1;
301
+ w2 = p2;
302
+ }
303
+ } else if (p1 !== null) {
304
+ w1 = p1;
305
+ w2 = 100 - p1;
306
+ } else if (p2 !== null) {
307
+ w2 = p2;
308
+ w1 = 100 - p2;
309
+ } else {
310
+ w1 = 50;
311
+ w2 = 50;
312
+ }
313
+ var a1 = c1.a;
314
+ var a2 = c2.a;
315
+ var totalWeight = w1 + w2;
316
+ var resolvedColor = void 0;
317
+ if (totalWeight === 0) {
318
+ resolvedColor = "transparent";
319
+ } else {
320
+ var f1 = w1 / totalWeight;
321
+ var f2 = w2 / totalWeight;
322
+ var mixedA = a1 * f1 + a2 * f2;
323
+ var mixedR = void 0,
324
+ mixedG = void 0,
325
+ mixedB = void 0;
326
+ if (mixedA === 0) {
327
+ mixedR = 0;
328
+ mixedG = 0;
329
+ mixedB = 0;
330
+ } else {
331
+ mixedR = Math.round((c1.r * a1 * f1 + c2.r * a2 * f2) / mixedA);
332
+ mixedG = Math.round((c1.g * a1 * f1 + c2.g * a2 * f2) / mixedA);
333
+ mixedB = Math.round((c1.b * a1 * f1 + c2.b * a2 * f2) / mixedA);
334
+ }
335
+ var finalA = totalWeight < 100 ? mixedA * (totalWeight / 100) : mixedA;
336
+ var roundedA = Math.round(finalA * 10000) / 10000;
337
+ resolvedColor = "rgba(".concat(mixedR, ", ").concat(mixedG, ", ").concat(mixedB, ", ").concat(roundedA, ")");
338
+ }
339
+ value = value.substring(0, index) + resolvedColor + value.substring(j);
340
+ }
341
+
342
+ // Restore placeholders
343
+ value = value.replace(/COLOR_MIX_TEMP\(/gi, "color-mix(");
344
+ return value;
345
+ }
346
+ function CssCalc() {
347
+ this.calc = function (value) {
348
+ if (value === undefined || typeof value !== "string") return value;
349
+ return value.replace(calcRe, function (_, calc) {
350
+ try {
351
+ // eslint-disable-next-line
352
+ var calcFunc = new Function("return ".concat(calc));
353
+ var calcValue = calcFunc();
354
+ return calcValue;
355
+ } catch (_unused) {
356
+ return 0;
357
+ }
358
+ });
359
+ };
360
+ this.calcColor = function (value) {
361
+ if (value === undefined || typeof value !== "string") return value;
362
+ value = resolveColorMix(value);
363
+ value = value.replace(colorRe1, function (_, r, g, b, a) {
364
+ a = parseFloat(a);
365
+ if (isNaN(a) || a >= 1) return "#".concat(itohex(r)).concat(itohex(g)).concat(itohex(b));
366
+ a = Math.round(a * 255);
367
+ return "#".concat(itohex(r)).concat(itohex(g)).concat(itohex(b)).concat(itohex(a));
368
+ });
369
+ value = value.replace(colorRe2, function (_, r, g, b, a) {
370
+ a = parseFloat(a);
371
+ if (isNaN(a) || a >= 1) return "#".concat(itohex(r)).concat(itohex(g)).concat(itohex(b));
372
+ a = Math.round(a * 255);
373
+ return "#".concat(itohex(r)).concat(itohex(g)).concat(itohex(b)).concat(itohex(a));
374
+ });
375
+ return value;
376
+ };
377
+ return this;
378
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = CssMedia;
7
+ var _helper = require("../utils/helper");
8
+ var _cssTransform = _interopRequireDefault(require("./css-transform"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
10
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
11
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
13
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
14
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
15
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
16
+ var SUPPORTED_MEDIA_TYPE = ["min-width", "min-height", "max-width", "max-height", "prefers-color-scheme"];
17
+ function CssMedia() {
18
+ var transform = new _cssTransform["default"]();
19
+ this.match = function (media) {
20
+ var _media$match;
21
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
22
+ width = _ref.width,
23
+ height = _ref.height,
24
+ colorScheme = _ref.colorScheme;
25
+ var isMedia = media.startsWith("@media");
26
+ if (!isMedia) return [false];
27
+ var isValidMedia = true;
28
+ var widthRange = [Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY];
29
+ var heightRange = [Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY];
30
+ var mediaGroupsRe = /\([^)]*\)/g;
31
+ var mediaGroups = (_media$match = media.match(mediaGroupsRe)) === null || _media$match === void 0 ? void 0 : _media$match.map(function (rawGroup) {
32
+ var mediaGroupRe = /\(\s*([^:)\s]+)\s*:\s*([^:)\s]+)\s*\)/g;
33
+ var _ref2 = mediaGroupRe.exec(rawGroup) || [],
34
+ _ref3 = _slicedToArray(_ref2, 3),
35
+ mediaType = _ref3[1],
36
+ mediaValue = _ref3[2];
37
+ if (!SUPPORTED_MEDIA_TYPE.includes(mediaType)) {
38
+ isValidMedia = false;
39
+ return null;
40
+ }
41
+ mediaType = (0, _helper.camelize)(mediaType);
42
+ mediaValue = transform.transformUnsupportedUnit(mediaValue);
43
+ mediaValue = transform.transformViewportUnit(mediaValue, {
44
+ width: width,
45
+ height: height
46
+ });
47
+ mediaValue = transform.removeUnit(mediaValue);
48
+ if (mediaType === "minWidth") {
49
+ widthRange[0] = mediaValue;
50
+ } else if (mediaType === "minHeight") {
51
+ heightRange[0] = mediaValue;
52
+ } else if (mediaType === "maxWidth") {
53
+ widthRange[1] = mediaValue;
54
+ } else if (mediaType === "maxHeight") {
55
+ heightRange[1] = mediaValue;
56
+ }
57
+ return [mediaType, mediaValue];
58
+ });
59
+ if (!Array.isArray(mediaGroups) || mediaGroups.length === 0) {
60
+ isValidMedia = false;
61
+ }
62
+ if (!isValidMedia) return [true, false];
63
+ if (mediaGroups[0][0] === "prefersColorScheme" && mediaGroups[0][1] === "dark") {
64
+ if (colorScheme === "dark") {
65
+ return [true, true];
66
+ } else {
67
+ return [true, false];
68
+ }
69
+ }
70
+ var isValidRange = widthRange[0] <= widthRange[1] && heightRange[0] <= heightRange[1];
71
+ if (!isValidRange) return [true, false];
72
+ var isMatchedMedia = widthRange[0] <= width && width <= widthRange[1] && heightRange[0] <= height && height <= heightRange[1];
73
+ if (!isMatchedMedia) return [true, false];
74
+ return [true, true];
75
+ };
76
+ return this;
77
+ }