@tioniq/sceniq 0.0.0-stage → 0.1.0
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.
- package/CHANGELOG.md +38 -0
- package/LICENSE +22 -0
- package/README.md +471 -2
- package/dist/chunk-BX7YQ44O.js +2470 -0
- package/dist/chunk-CVQBUUHT.js +1328 -0
- package/dist/chunk-HKPEAHCX.cjs +279 -0
- package/dist/chunk-J4XB4OPH.js +279 -0
- package/dist/chunk-KRCM4WYZ.cjs +2470 -0
- package/dist/chunk-OCHCBJJE.cjs +1328 -0
- package/dist/color-IY199Sua.d.cts +34 -0
- package/dist/color-IY199Sua.d.ts +34 -0
- package/dist/index.cjs +5636 -0
- package/dist/index.d.cts +1627 -0
- package/dist/index.d.ts +1627 -0
- package/dist/index.js +5636 -0
- package/dist/react/index.cjs +88 -0
- package/dist/react/index.d.cts +64 -0
- package/dist/react/index.d.ts +64 -0
- package/dist/react/index.js +88 -0
- package/dist/rect-bounds-Bd7-7ERA.d.cts +726 -0
- package/dist/rect-bounds-Bd7-7ERA.d.ts +726 -0
- package/dist/scene-77s46CRy.d.ts +590 -0
- package/dist/scene-Dd7ExNOl.d.cts +590 -0
- package/dist/svg/index.cjs +1122 -0
- package/dist/svg/index.d.cts +213 -0
- package/dist/svg/index.d.ts +213 -0
- package/dist/svg/index.js +1122 -0
- package/package.json +142 -4
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _class; var _class2; var _class3;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkOCHCBJJEcjs = require('./chunk-OCHCBJJE.cjs');
|
|
5
|
+
|
|
6
|
+
// src/drawable.ts
|
|
7
|
+
var _disposiq = require('@tioniq/disposiq');
|
|
8
|
+
var _eventiq = require('@tioniq/eventiq');
|
|
9
|
+
var Drawable = (_class = class {constructor() { _class.prototype.__init.call(this); }
|
|
10
|
+
__init() {this._onChanged = new (0, _eventiq.EventDispatcher)()}
|
|
11
|
+
get onChanged() {
|
|
12
|
+
return this._onChanged;
|
|
13
|
+
}
|
|
14
|
+
canDraw(_context) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
getVisibleBounds(_context) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
toCached(size) {
|
|
21
|
+
return createCache(this, size);
|
|
22
|
+
}
|
|
23
|
+
/** This drawable drawn through a blur filter. A variable radius is followed only while the result is drawn. */
|
|
24
|
+
toBlurred(radius) {
|
|
25
|
+
return new BlurredDrawable(this, radius);
|
|
26
|
+
}
|
|
27
|
+
handleMouseAction(_action) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
invalidate() {
|
|
31
|
+
this._onChanged.dispatch();
|
|
32
|
+
}
|
|
33
|
+
}, _class);
|
|
34
|
+
var DrawableContent = (_class2 = class {constructor() { _class2.prototype.__init2.call(this); }
|
|
35
|
+
__init2() {this._onChanged = new (0, _eventiq.EventDispatcher)()}
|
|
36
|
+
get onChanged() {
|
|
37
|
+
return this._onChanged;
|
|
38
|
+
}
|
|
39
|
+
invalidate() {
|
|
40
|
+
this._onChanged.dispatch();
|
|
41
|
+
}
|
|
42
|
+
}, _class2);
|
|
43
|
+
var CanvasDrawable = class extends Drawable {
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
constructor(canvas, bounds) {
|
|
47
|
+
super();
|
|
48
|
+
this._canvas = canvas;
|
|
49
|
+
this._bounds = bounds;
|
|
50
|
+
}
|
|
51
|
+
getVisibleBounds() {
|
|
52
|
+
return this._bounds;
|
|
53
|
+
}
|
|
54
|
+
draw(context) {
|
|
55
|
+
const bounds = this._bounds;
|
|
56
|
+
context.drawImage(this._canvas, bounds.x, bounds.y, bounds.width, bounds.height);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function createCache(original, sizeParam) {
|
|
60
|
+
const canvas = _chunkOCHCBJJEcjs.createCanvasElement.call(void 0, );
|
|
61
|
+
const context = canvas.getContext("2d");
|
|
62
|
+
if (!context) {
|
|
63
|
+
console.error("Cannot cache drawable without a 2D canvas context");
|
|
64
|
+
return original;
|
|
65
|
+
}
|
|
66
|
+
if (sizeParam) {
|
|
67
|
+
canvas.width = sizeParam.width;
|
|
68
|
+
canvas.height = sizeParam.height;
|
|
69
|
+
}
|
|
70
|
+
const bounds = original.getVisibleBounds(context);
|
|
71
|
+
if (!bounds) {
|
|
72
|
+
if (!sizeParam) {
|
|
73
|
+
console.error("Cannot cache drawable without visible bounds");
|
|
74
|
+
return original;
|
|
75
|
+
}
|
|
76
|
+
original.draw(context);
|
|
77
|
+
return new CanvasDrawable(canvas, new (0, _chunkOCHCBJJEcjs.RectBounds)(0, 0, sizeParam.width, sizeParam.height));
|
|
78
|
+
}
|
|
79
|
+
const size = sizeParam || bounds.size;
|
|
80
|
+
if (!sizeParam) {
|
|
81
|
+
canvas.width = size.width;
|
|
82
|
+
canvas.height = size.height;
|
|
83
|
+
}
|
|
84
|
+
const scaleX = size.width / bounds.width;
|
|
85
|
+
const scaleY = size.height / bounds.height;
|
|
86
|
+
context.scale(scaleX, scaleY);
|
|
87
|
+
context.translate(-bounds.x, -bounds.y);
|
|
88
|
+
original.draw(context);
|
|
89
|
+
return new CanvasDrawable(canvas, bounds);
|
|
90
|
+
}
|
|
91
|
+
var BlurredDrawable = class extends Drawable {
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
constructor(drawable, radius) {
|
|
97
|
+
super();
|
|
98
|
+
this._drawable = drawable;
|
|
99
|
+
this._source = _eventiq.isVariableOf.call(void 0, radius) ? radius : null;
|
|
100
|
+
this._radius = _eventiq.createVar.call(void 0, _eventiq.isVariableOf.call(void 0, radius) ? radius.value : radius);
|
|
101
|
+
this._changes = new (0, _eventiq.LazyEventDispatcher)((dispatcher) => {
|
|
102
|
+
const store = new (0, _disposiq.DisposableStore)();
|
|
103
|
+
const source = this._source;
|
|
104
|
+
if (source) {
|
|
105
|
+
store.add(source.subscribe((value) => this._radius.value = value));
|
|
106
|
+
}
|
|
107
|
+
store.add(this._radius.subscribeSilent(() => dispatcher.dispatch()));
|
|
108
|
+
store.add(this._drawable.onChanged.subscribe(() => dispatcher.dispatch()));
|
|
109
|
+
return store;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
get onChanged() {
|
|
113
|
+
return this._changes;
|
|
114
|
+
}
|
|
115
|
+
get radius() {
|
|
116
|
+
return this._radius;
|
|
117
|
+
}
|
|
118
|
+
set radius(value) {
|
|
119
|
+
this._radius.value = value;
|
|
120
|
+
}
|
|
121
|
+
getVisibleBounds(context) {
|
|
122
|
+
return this._drawable.getVisibleBounds(context);
|
|
123
|
+
}
|
|
124
|
+
draw(context) {
|
|
125
|
+
const radius = this._source ? this._source.value : this._radius.value;
|
|
126
|
+
if (radius === 0) {
|
|
127
|
+
this._drawable.draw(context);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const previousFilter = context.filter;
|
|
131
|
+
context.filter = `blur(${radius}px)`;
|
|
132
|
+
this._drawable.draw(context);
|
|
133
|
+
context.filter = previousFilter;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/color/color.ts
|
|
138
|
+
var Color = (_class3 = class _Color {
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
constructor(r, g, b, a = 255) {
|
|
144
|
+
this.r = r;
|
|
145
|
+
this.g = g;
|
|
146
|
+
this.b = b;
|
|
147
|
+
this.a = a;
|
|
148
|
+
}
|
|
149
|
+
grayscale() {
|
|
150
|
+
const gray = Math.round(0.299 * this.r + 0.587 * this.g + 0.114 * this.b);
|
|
151
|
+
return new _Color(gray, gray, gray, this.a);
|
|
152
|
+
}
|
|
153
|
+
toString() {
|
|
154
|
+
return this.toHexString();
|
|
155
|
+
}
|
|
156
|
+
toRgbString() {
|
|
157
|
+
return `rgb(${this.r}, ${this.g}, ${this.b})`;
|
|
158
|
+
}
|
|
159
|
+
toHexString() {
|
|
160
|
+
return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}${this.a.toString(16).padStart(2, "0")}`;
|
|
161
|
+
}
|
|
162
|
+
toHexWithoutAlphaString() {
|
|
163
|
+
return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}`;
|
|
164
|
+
}
|
|
165
|
+
lighter(percent) {
|
|
166
|
+
const r = Math.min(255, this.r + Math.round(2.55 * percent));
|
|
167
|
+
const g = Math.min(255, this.g + Math.round(2.55 * percent));
|
|
168
|
+
const b = Math.min(255, this.b + Math.round(2.55 * percent));
|
|
169
|
+
return new _Color(r, g, b, this.a);
|
|
170
|
+
}
|
|
171
|
+
darker(percent) {
|
|
172
|
+
const r = Math.max(0, this.r - Math.round(2.55 * percent));
|
|
173
|
+
const g = Math.max(0, this.g - Math.round(2.55 * percent));
|
|
174
|
+
const b = Math.max(0, this.b - Math.round(2.55 * percent));
|
|
175
|
+
return new _Color(r, g, b, this.a);
|
|
176
|
+
}
|
|
177
|
+
withRed(red) {
|
|
178
|
+
return new _Color(red, this.g, this.b, this.a);
|
|
179
|
+
}
|
|
180
|
+
withGreen(green) {
|
|
181
|
+
return new _Color(this.r, green, this.b, this.a);
|
|
182
|
+
}
|
|
183
|
+
withBlue(blue) {
|
|
184
|
+
return new _Color(this.r, this.g, blue, this.a);
|
|
185
|
+
}
|
|
186
|
+
withAlpha(alpha) {
|
|
187
|
+
return new _Color(this.r, this.g, this.b, alpha);
|
|
188
|
+
}
|
|
189
|
+
withOpacity(opacity) {
|
|
190
|
+
return new _Color(this.r, this.g, this.b, Math.round(this.a * opacity));
|
|
191
|
+
}
|
|
192
|
+
// Should blend the color with background color to make it fully opaque
|
|
193
|
+
blendWithBackground(background) {
|
|
194
|
+
const alpha = this.a / 255;
|
|
195
|
+
const r = Math.round(this.r * alpha + background.r * (1 - alpha));
|
|
196
|
+
const g = Math.round(this.g * alpha + background.g * (1 - alpha));
|
|
197
|
+
const b = Math.round(this.b * alpha + background.b * (1 - alpha));
|
|
198
|
+
return new _Color(r, g, b, 255);
|
|
199
|
+
}
|
|
200
|
+
equals(color) {
|
|
201
|
+
return this.r === color.r && this.g === color.g && this.b === color.b && this.a === color.a;
|
|
202
|
+
}
|
|
203
|
+
static parse(color) {
|
|
204
|
+
const str = color.replace("#", "");
|
|
205
|
+
if (str.length === 3) {
|
|
206
|
+
return new _Color(parseInt(str[0] + str[0], 16), parseInt(str[1] + str[1], 16), parseInt(str[2] + str[2], 16), 255);
|
|
207
|
+
}
|
|
208
|
+
if (str.length === 4) {
|
|
209
|
+
return new _Color(
|
|
210
|
+
parseInt(str[0] + str[0], 16),
|
|
211
|
+
parseInt(str[1] + str[1], 16),
|
|
212
|
+
parseInt(str[2] + str[2], 16),
|
|
213
|
+
parseInt(str[3] + str[3], 16)
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (str.length === 6) {
|
|
217
|
+
return new _Color(parseInt(str.slice(0, 2), 16), parseInt(str.slice(2, 4), 16), parseInt(str.slice(4, 6), 16), 255);
|
|
218
|
+
}
|
|
219
|
+
if (str.length === 8) {
|
|
220
|
+
return new _Color(
|
|
221
|
+
parseInt(str.slice(0, 2), 16),
|
|
222
|
+
parseInt(str.slice(2, 4), 16),
|
|
223
|
+
parseInt(str.slice(4, 6), 16),
|
|
224
|
+
parseInt(str.slice(6, 8), 16)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
throw new Error("Invalid color format");
|
|
228
|
+
}
|
|
229
|
+
static __initStatic() {this.black = /* @__PURE__ */ new _Color(0, 0, 0, 255)}
|
|
230
|
+
static __initStatic2() {this.white = /* @__PURE__ */ new _Color(255, 255, 255, 255)}
|
|
231
|
+
static __initStatic3() {this.red = /* @__PURE__ */ new _Color(255, 0, 0, 255)}
|
|
232
|
+
static __initStatic4() {this.green = /* @__PURE__ */ new _Color(0, 255, 0, 255)}
|
|
233
|
+
static __initStatic5() {this.blue = /* @__PURE__ */ new _Color(0, 0, 255, 255)}
|
|
234
|
+
static __initStatic6() {this.transparent = /* @__PURE__ */ new _Color(0, 0, 0, 0)}
|
|
235
|
+
static fromRgb(r, g, b, a = 255) {
|
|
236
|
+
return new _Color(r, g, b, a);
|
|
237
|
+
}
|
|
238
|
+
static fromHsl(h, s, l, a = 255) {
|
|
239
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
240
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
241
|
+
const m = l - c / 2;
|
|
242
|
+
let r = 0;
|
|
243
|
+
let g = 0;
|
|
244
|
+
let b = 0;
|
|
245
|
+
if (h < 60) {
|
|
246
|
+
r = c;
|
|
247
|
+
g = x;
|
|
248
|
+
} else if (h < 120) {
|
|
249
|
+
r = x;
|
|
250
|
+
g = c;
|
|
251
|
+
} else if (h < 180) {
|
|
252
|
+
g = c;
|
|
253
|
+
b = x;
|
|
254
|
+
} else if (h < 240) {
|
|
255
|
+
g = x;
|
|
256
|
+
b = c;
|
|
257
|
+
} else if (h < 300) {
|
|
258
|
+
r = x;
|
|
259
|
+
b = c;
|
|
260
|
+
} else {
|
|
261
|
+
r = c;
|
|
262
|
+
b = x;
|
|
263
|
+
}
|
|
264
|
+
return new _Color(Math.round((r + m) * 255), Math.round((g + m) * 255), Math.round((b + m) * 255), a);
|
|
265
|
+
}
|
|
266
|
+
static interpolate(from, to, progress) {
|
|
267
|
+
const r = from.r + Math.round((to.r - from.r) * progress);
|
|
268
|
+
const g = from.g + Math.round((to.g - from.g) * progress);
|
|
269
|
+
const b = from.b + Math.round((to.b - from.b) * progress);
|
|
270
|
+
const a = from.a + Math.round((to.a - from.a) * progress);
|
|
271
|
+
return new _Color(r, g, b, a);
|
|
272
|
+
}
|
|
273
|
+
}, _class3.__initStatic(), _class3.__initStatic2(), _class3.__initStatic3(), _class3.__initStatic4(), _class3.__initStatic5(), _class3.__initStatic6(), _class3);
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
exports.Drawable = Drawable; exports.DrawableContent = DrawableContent; exports.Color = Color;
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RectBounds,
|
|
3
|
+
createCanvasElement
|
|
4
|
+
} from "./chunk-CVQBUUHT.js";
|
|
5
|
+
|
|
6
|
+
// src/drawable.ts
|
|
7
|
+
import { DisposableStore } from "@tioniq/disposiq";
|
|
8
|
+
import { createVar, EventDispatcher, isVariableOf, LazyEventDispatcher } from "@tioniq/eventiq";
|
|
9
|
+
var Drawable = class {
|
|
10
|
+
_onChanged = new EventDispatcher();
|
|
11
|
+
get onChanged() {
|
|
12
|
+
return this._onChanged;
|
|
13
|
+
}
|
|
14
|
+
canDraw(_context) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
getVisibleBounds(_context) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
toCached(size) {
|
|
21
|
+
return createCache(this, size);
|
|
22
|
+
}
|
|
23
|
+
/** This drawable drawn through a blur filter. A variable radius is followed only while the result is drawn. */
|
|
24
|
+
toBlurred(radius) {
|
|
25
|
+
return new BlurredDrawable(this, radius);
|
|
26
|
+
}
|
|
27
|
+
handleMouseAction(_action) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
invalidate() {
|
|
31
|
+
this._onChanged.dispatch();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var DrawableContent = class {
|
|
35
|
+
_onChanged = new EventDispatcher();
|
|
36
|
+
get onChanged() {
|
|
37
|
+
return this._onChanged;
|
|
38
|
+
}
|
|
39
|
+
invalidate() {
|
|
40
|
+
this._onChanged.dispatch();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var CanvasDrawable = class extends Drawable {
|
|
44
|
+
_canvas;
|
|
45
|
+
_bounds;
|
|
46
|
+
constructor(canvas, bounds) {
|
|
47
|
+
super();
|
|
48
|
+
this._canvas = canvas;
|
|
49
|
+
this._bounds = bounds;
|
|
50
|
+
}
|
|
51
|
+
getVisibleBounds() {
|
|
52
|
+
return this._bounds;
|
|
53
|
+
}
|
|
54
|
+
draw(context) {
|
|
55
|
+
const bounds = this._bounds;
|
|
56
|
+
context.drawImage(this._canvas, bounds.x, bounds.y, bounds.width, bounds.height);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
function createCache(original, sizeParam) {
|
|
60
|
+
const canvas = createCanvasElement();
|
|
61
|
+
const context = canvas.getContext("2d");
|
|
62
|
+
if (!context) {
|
|
63
|
+
console.error("Cannot cache drawable without a 2D canvas context");
|
|
64
|
+
return original;
|
|
65
|
+
}
|
|
66
|
+
if (sizeParam) {
|
|
67
|
+
canvas.width = sizeParam.width;
|
|
68
|
+
canvas.height = sizeParam.height;
|
|
69
|
+
}
|
|
70
|
+
const bounds = original.getVisibleBounds(context);
|
|
71
|
+
if (!bounds) {
|
|
72
|
+
if (!sizeParam) {
|
|
73
|
+
console.error("Cannot cache drawable without visible bounds");
|
|
74
|
+
return original;
|
|
75
|
+
}
|
|
76
|
+
original.draw(context);
|
|
77
|
+
return new CanvasDrawable(canvas, new RectBounds(0, 0, sizeParam.width, sizeParam.height));
|
|
78
|
+
}
|
|
79
|
+
const size = sizeParam || bounds.size;
|
|
80
|
+
if (!sizeParam) {
|
|
81
|
+
canvas.width = size.width;
|
|
82
|
+
canvas.height = size.height;
|
|
83
|
+
}
|
|
84
|
+
const scaleX = size.width / bounds.width;
|
|
85
|
+
const scaleY = size.height / bounds.height;
|
|
86
|
+
context.scale(scaleX, scaleY);
|
|
87
|
+
context.translate(-bounds.x, -bounds.y);
|
|
88
|
+
original.draw(context);
|
|
89
|
+
return new CanvasDrawable(canvas, bounds);
|
|
90
|
+
}
|
|
91
|
+
var BlurredDrawable = class extends Drawable {
|
|
92
|
+
_drawable;
|
|
93
|
+
_radius;
|
|
94
|
+
_source;
|
|
95
|
+
_changes;
|
|
96
|
+
constructor(drawable, radius) {
|
|
97
|
+
super();
|
|
98
|
+
this._drawable = drawable;
|
|
99
|
+
this._source = isVariableOf(radius) ? radius : null;
|
|
100
|
+
this._radius = createVar(isVariableOf(radius) ? radius.value : radius);
|
|
101
|
+
this._changes = new LazyEventDispatcher((dispatcher) => {
|
|
102
|
+
const store = new DisposableStore();
|
|
103
|
+
const source = this._source;
|
|
104
|
+
if (source) {
|
|
105
|
+
store.add(source.subscribe((value) => this._radius.value = value));
|
|
106
|
+
}
|
|
107
|
+
store.add(this._radius.subscribeSilent(() => dispatcher.dispatch()));
|
|
108
|
+
store.add(this._drawable.onChanged.subscribe(() => dispatcher.dispatch()));
|
|
109
|
+
return store;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
get onChanged() {
|
|
113
|
+
return this._changes;
|
|
114
|
+
}
|
|
115
|
+
get radius() {
|
|
116
|
+
return this._radius;
|
|
117
|
+
}
|
|
118
|
+
set radius(value) {
|
|
119
|
+
this._radius.value = value;
|
|
120
|
+
}
|
|
121
|
+
getVisibleBounds(context) {
|
|
122
|
+
return this._drawable.getVisibleBounds(context);
|
|
123
|
+
}
|
|
124
|
+
draw(context) {
|
|
125
|
+
const radius = this._source ? this._source.value : this._radius.value;
|
|
126
|
+
if (radius === 0) {
|
|
127
|
+
this._drawable.draw(context);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const previousFilter = context.filter;
|
|
131
|
+
context.filter = `blur(${radius}px)`;
|
|
132
|
+
this._drawable.draw(context);
|
|
133
|
+
context.filter = previousFilter;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/color/color.ts
|
|
138
|
+
var Color = class _Color {
|
|
139
|
+
r;
|
|
140
|
+
g;
|
|
141
|
+
b;
|
|
142
|
+
a;
|
|
143
|
+
constructor(r, g, b, a = 255) {
|
|
144
|
+
this.r = r;
|
|
145
|
+
this.g = g;
|
|
146
|
+
this.b = b;
|
|
147
|
+
this.a = a;
|
|
148
|
+
}
|
|
149
|
+
grayscale() {
|
|
150
|
+
const gray = Math.round(0.299 * this.r + 0.587 * this.g + 0.114 * this.b);
|
|
151
|
+
return new _Color(gray, gray, gray, this.a);
|
|
152
|
+
}
|
|
153
|
+
toString() {
|
|
154
|
+
return this.toHexString();
|
|
155
|
+
}
|
|
156
|
+
toRgbString() {
|
|
157
|
+
return `rgb(${this.r}, ${this.g}, ${this.b})`;
|
|
158
|
+
}
|
|
159
|
+
toHexString() {
|
|
160
|
+
return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}${this.a.toString(16).padStart(2, "0")}`;
|
|
161
|
+
}
|
|
162
|
+
toHexWithoutAlphaString() {
|
|
163
|
+
return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}`;
|
|
164
|
+
}
|
|
165
|
+
lighter(percent) {
|
|
166
|
+
const r = Math.min(255, this.r + Math.round(2.55 * percent));
|
|
167
|
+
const g = Math.min(255, this.g + Math.round(2.55 * percent));
|
|
168
|
+
const b = Math.min(255, this.b + Math.round(2.55 * percent));
|
|
169
|
+
return new _Color(r, g, b, this.a);
|
|
170
|
+
}
|
|
171
|
+
darker(percent) {
|
|
172
|
+
const r = Math.max(0, this.r - Math.round(2.55 * percent));
|
|
173
|
+
const g = Math.max(0, this.g - Math.round(2.55 * percent));
|
|
174
|
+
const b = Math.max(0, this.b - Math.round(2.55 * percent));
|
|
175
|
+
return new _Color(r, g, b, this.a);
|
|
176
|
+
}
|
|
177
|
+
withRed(red) {
|
|
178
|
+
return new _Color(red, this.g, this.b, this.a);
|
|
179
|
+
}
|
|
180
|
+
withGreen(green) {
|
|
181
|
+
return new _Color(this.r, green, this.b, this.a);
|
|
182
|
+
}
|
|
183
|
+
withBlue(blue) {
|
|
184
|
+
return new _Color(this.r, this.g, blue, this.a);
|
|
185
|
+
}
|
|
186
|
+
withAlpha(alpha) {
|
|
187
|
+
return new _Color(this.r, this.g, this.b, alpha);
|
|
188
|
+
}
|
|
189
|
+
withOpacity(opacity) {
|
|
190
|
+
return new _Color(this.r, this.g, this.b, Math.round(this.a * opacity));
|
|
191
|
+
}
|
|
192
|
+
// Should blend the color with background color to make it fully opaque
|
|
193
|
+
blendWithBackground(background) {
|
|
194
|
+
const alpha = this.a / 255;
|
|
195
|
+
const r = Math.round(this.r * alpha + background.r * (1 - alpha));
|
|
196
|
+
const g = Math.round(this.g * alpha + background.g * (1 - alpha));
|
|
197
|
+
const b = Math.round(this.b * alpha + background.b * (1 - alpha));
|
|
198
|
+
return new _Color(r, g, b, 255);
|
|
199
|
+
}
|
|
200
|
+
equals(color) {
|
|
201
|
+
return this.r === color.r && this.g === color.g && this.b === color.b && this.a === color.a;
|
|
202
|
+
}
|
|
203
|
+
static parse(color) {
|
|
204
|
+
const str = color.replace("#", "");
|
|
205
|
+
if (str.length === 3) {
|
|
206
|
+
return new _Color(parseInt(str[0] + str[0], 16), parseInt(str[1] + str[1], 16), parseInt(str[2] + str[2], 16), 255);
|
|
207
|
+
}
|
|
208
|
+
if (str.length === 4) {
|
|
209
|
+
return new _Color(
|
|
210
|
+
parseInt(str[0] + str[0], 16),
|
|
211
|
+
parseInt(str[1] + str[1], 16),
|
|
212
|
+
parseInt(str[2] + str[2], 16),
|
|
213
|
+
parseInt(str[3] + str[3], 16)
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (str.length === 6) {
|
|
217
|
+
return new _Color(parseInt(str.slice(0, 2), 16), parseInt(str.slice(2, 4), 16), parseInt(str.slice(4, 6), 16), 255);
|
|
218
|
+
}
|
|
219
|
+
if (str.length === 8) {
|
|
220
|
+
return new _Color(
|
|
221
|
+
parseInt(str.slice(0, 2), 16),
|
|
222
|
+
parseInt(str.slice(2, 4), 16),
|
|
223
|
+
parseInt(str.slice(4, 6), 16),
|
|
224
|
+
parseInt(str.slice(6, 8), 16)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
throw new Error("Invalid color format");
|
|
228
|
+
}
|
|
229
|
+
static black = /* @__PURE__ */ new _Color(0, 0, 0, 255);
|
|
230
|
+
static white = /* @__PURE__ */ new _Color(255, 255, 255, 255);
|
|
231
|
+
static red = /* @__PURE__ */ new _Color(255, 0, 0, 255);
|
|
232
|
+
static green = /* @__PURE__ */ new _Color(0, 255, 0, 255);
|
|
233
|
+
static blue = /* @__PURE__ */ new _Color(0, 0, 255, 255);
|
|
234
|
+
static transparent = /* @__PURE__ */ new _Color(0, 0, 0, 0);
|
|
235
|
+
static fromRgb(r, g, b, a = 255) {
|
|
236
|
+
return new _Color(r, g, b, a);
|
|
237
|
+
}
|
|
238
|
+
static fromHsl(h, s, l, a = 255) {
|
|
239
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
240
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
241
|
+
const m = l - c / 2;
|
|
242
|
+
let r = 0;
|
|
243
|
+
let g = 0;
|
|
244
|
+
let b = 0;
|
|
245
|
+
if (h < 60) {
|
|
246
|
+
r = c;
|
|
247
|
+
g = x;
|
|
248
|
+
} else if (h < 120) {
|
|
249
|
+
r = x;
|
|
250
|
+
g = c;
|
|
251
|
+
} else if (h < 180) {
|
|
252
|
+
g = c;
|
|
253
|
+
b = x;
|
|
254
|
+
} else if (h < 240) {
|
|
255
|
+
g = x;
|
|
256
|
+
b = c;
|
|
257
|
+
} else if (h < 300) {
|
|
258
|
+
r = x;
|
|
259
|
+
b = c;
|
|
260
|
+
} else {
|
|
261
|
+
r = c;
|
|
262
|
+
b = x;
|
|
263
|
+
}
|
|
264
|
+
return new _Color(Math.round((r + m) * 255), Math.round((g + m) * 255), Math.round((b + m) * 255), a);
|
|
265
|
+
}
|
|
266
|
+
static interpolate(from, to, progress) {
|
|
267
|
+
const r = from.r + Math.round((to.r - from.r) * progress);
|
|
268
|
+
const g = from.g + Math.round((to.g - from.g) * progress);
|
|
269
|
+
const b = from.b + Math.round((to.b - from.b) * progress);
|
|
270
|
+
const a = from.a + Math.round((to.a - from.a) * progress);
|
|
271
|
+
return new _Color(r, g, b, a);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
export {
|
|
276
|
+
Drawable,
|
|
277
|
+
DrawableContent,
|
|
278
|
+
Color
|
|
279
|
+
};
|