x4js 2.0.12 → 2.0.13
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/README.md +15 -15
- package/lib/README.txt +15 -15
- package/lib/cjs/x4.css +1 -1
- package/lib/cjs/x4.js +1 -1
- package/lib/esm/x4.css +1 -1
- package/lib/esm/x4.mjs +1 -1
- package/lib/src/components/base.scss +25 -26
- package/lib/src/components/boxes/boxes.module.scss +37 -37
- package/lib/src/components/boxes/boxes.ts +129 -125
- package/lib/src/components/breadcrumb/breadcrumb.scss +28 -0
- package/lib/src/components/breadcrumb/breadcrumb.ts +84 -0
- package/lib/src/components/breadcrumb/chevron-right.svg +1 -0
- package/lib/src/components/btngroup/btngroup.module.scss +28 -28
- package/lib/src/components/btngroup/btngroup.ts +119 -101
- package/lib/src/components/button/button.module.scss +154 -153
- package/lib/src/components/button/button.ts +117 -117
- package/lib/src/components/calendar/calendar.module.scss +162 -162
- package/lib/src/components/calendar/calendar.ts +326 -325
- package/lib/src/components/checkbox/check.svg +3 -3
- package/lib/src/components/checkbox/checkbox.module.scss +141 -141
- package/lib/src/components/checkbox/checkbox.ts +125 -124
- package/lib/src/components/colorinput/colorinput.module.scss +64 -64
- package/lib/src/components/colorinput/colorinput.ts +90 -87
- package/lib/src/components/colorpicker/colorpicker.module.scss +132 -132
- package/lib/src/components/colorpicker/colorpicker.ts +481 -476
- package/lib/src/components/combobox/combobox.module.scss +123 -120
- package/lib/src/components/combobox/combobox.ts +192 -190
- package/lib/src/components/combobox/updown.svg +3 -3
- package/lib/src/components/components.ts +34 -0
- package/lib/src/components/dialog/dialog.module.scss +71 -71
- package/lib/src/components/dialog/dialog.ts +94 -92
- package/lib/src/components/form/form.module.scss +34 -34
- package/lib/src/components/form/form.ts +41 -36
- package/lib/src/components/grid/datastore.ts +1298 -0
- package/lib/src/components/grid/gridview.ts +1108 -0
- package/lib/src/components/grid/memdb.ts +325 -0
- package/lib/src/components/header/header.module.scss +39 -39
- package/lib/src/components/header/header.ts +129 -123
- package/lib/src/components/icon/icon.module.scss +29 -29
- package/lib/src/components/icon/icon.ts +136 -134
- package/lib/src/components/image/image.module.scss +20 -20
- package/lib/src/components/image/image.ts +68 -66
- package/lib/src/components/input/input.module.scss +69 -69
- package/lib/src/components/input/input.ts +275 -274
- package/lib/src/components/label/label.module.scss +58 -52
- package/lib/src/components/label/label.ts +64 -55
- package/lib/src/components/link/link.ts +78 -0
- package/lib/src/components/listbox/listbox.module.scss +103 -103
- package/lib/src/components/listbox/listbox.ts +431 -427
- package/lib/src/components/menu/menu.module.scss +107 -107
- package/lib/src/components/menu/menu.ts +171 -168
- package/lib/src/components/messages/messages.module.scss +48 -47
- package/lib/src/components/messages/messages.ts +68 -63
- package/lib/src/components/normalize.scss +386 -386
- package/lib/src/components/notification/notification.module.scss +81 -81
- package/lib/src/components/notification/notification.ts +109 -108
- package/lib/src/components/panel/panel.module.scss +47 -47
- package/lib/src/components/panel/panel.ts +57 -56
- package/lib/src/components/popup/popup.module.scss +43 -43
- package/lib/src/components/popup/popup.ts +396 -395
- package/lib/src/components/progress/progress.module.scss +56 -56
- package/lib/src/components/progress/progress.ts +43 -42
- package/lib/src/components/rating/rating.module.scss +22 -22
- package/lib/src/components/rating/rating.ts +131 -125
- package/lib/src/components/shared.scss +90 -76
- package/lib/src/components/sizers/sizer.module.scss +89 -89
- package/lib/src/components/sizers/sizer.ts +123 -119
- package/lib/src/components/slider/slider.module.scss +70 -70
- package/lib/src/components/slider/slider.ts +147 -142
- package/lib/src/components/switch/switch.module.scss +126 -126
- package/lib/src/components/switch/switch.ts +61 -55
- package/lib/src/components/tabs/tabs.module.scss +46 -46
- package/lib/src/components/tabs/tabs.ts +168 -157
- package/lib/src/components/textarea/textarea.module.scss +59 -59
- package/lib/src/components/textarea/textarea.ts +60 -54
- package/lib/src/components/textedit/textedit.module.scss +113 -113
- package/lib/src/components/textedit/textedit.ts +83 -82
- package/lib/src/components/themes.scss +81 -77
- package/lib/src/components/tooltips/tooltips.scss +50 -50
- package/lib/src/components/tooltips/tooltips.ts +103 -102
- package/lib/src/components/treeview/treeview.module.scss +115 -115
- package/lib/src/components/treeview/treeview.ts +410 -403
- package/lib/src/components/viewport/viewport.module.scss +24 -24
- package/lib/src/components/viewport/viewport.ts +41 -38
- package/lib/src/core/component.ts +1002 -979
- package/lib/src/core/core_application.ts +44 -0
- package/lib/src/core/core_colors.ts +249 -249
- package/lib/src/core/core_dom.ts +471 -471
- package/lib/src/core/core_dragdrop.ts +200 -200
- package/lib/src/core/core_element.ts +97 -97
- package/lib/src/core/core_events.ts +149 -149
- package/lib/src/core/core_i18n.ts +377 -377
- package/lib/src/core/core_router.ts +221 -221
- package/lib/src/core/core_styles.ts +214 -214
- package/lib/src/core/core_svg.ts +550 -550
- package/lib/src/core/core_tools.ts +688 -673
- package/lib/src/demo/assets/radio.svg +3 -3
- package/lib/src/demo/index.html +11 -11
- package/lib/src/demo/main.scss +21 -21
- package/lib/src/demo/main.tsx +323 -323
- package/lib/src/types/scss.d.ts +4 -4
- package/lib/src/types/x4react.d.ts +8 -8
- package/lib/src/x4.scss +18 -18
- package/lib/src/x4.ts +31 -62
- package/lib/styles/x4.css +1 -1
- package/lib/types/x4js.d.ts +100 -49
- package/package.json +2 -3
- package/src/x4.ts +31 -62
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file core_application.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
*
|
|
11
|
+
* @copyright (c) 2024 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* Use of this source code is governed by an MIT-style license
|
|
14
|
+
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
+
**/
|
|
16
|
+
|
|
17
|
+
import { Component } from './component.js';
|
|
18
|
+
import { CoreElement } from './core_element.js';
|
|
19
|
+
import { EventMap } from './core_events';
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export interface ApplicationEvents extends EventMap {
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// signleton
|
|
26
|
+
let main_app: Application = null;
|
|
27
|
+
|
|
28
|
+
export class Application<E extends ApplicationEvents = ApplicationEvents> extends CoreElement<E> {
|
|
29
|
+
|
|
30
|
+
constructor( ) {
|
|
31
|
+
super( );
|
|
32
|
+
|
|
33
|
+
console.assert( main_app==null, "Application must be a singleton." );
|
|
34
|
+
main_app = this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setMainView( view: Component ) {
|
|
38
|
+
document.body.appendChild( view.dom );
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static instance<P extends Application = Application>( ): P {
|
|
42
|
+
return main_app as P;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,250 +1,250 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ___ ___ __
|
|
3
|
-
* \ \/ / / _
|
|
4
|
-
* \ / /_| |_
|
|
5
|
-
* / \____ _|
|
|
6
|
-
* /__/\__\ |_|
|
|
7
|
-
*
|
|
8
|
-
* @file core_colors.ts
|
|
9
|
-
* @author Etienne Cochard
|
|
10
|
-
*
|
|
11
|
-
* @copyright (c) 2024 R-libre ingenierie
|
|
12
|
-
*
|
|
13
|
-
* Use of this source code is governed by an MIT-style license
|
|
14
|
-
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
-
**/
|
|
16
|
-
|
|
17
|
-
import { clamp, isString } from './core_tools';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function hx( v: number ) {
|
|
21
|
-
const hex = v.toString( 16 );
|
|
22
|
-
return hex.padStart( 2, '0' );
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function round( v: number ) {
|
|
26
|
-
return Math.round(v);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export interface Rgb {
|
|
31
|
-
red: number;
|
|
32
|
-
green: number;
|
|
33
|
-
blue: number;
|
|
34
|
-
alpha: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface Hsv {
|
|
38
|
-
hue: number;
|
|
39
|
-
saturation: number;
|
|
40
|
-
value: number;
|
|
41
|
-
alpha: number;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
export class Color {
|
|
46
|
-
|
|
47
|
-
private rgb: [red:number,green:number,blue:number,alpha:number] = [0,0,0,1];
|
|
48
|
-
private invalid = false;
|
|
49
|
-
|
|
50
|
-
constructor( value: string );
|
|
51
|
-
constructor( r: number, g: number, b: number, a?: number );
|
|
52
|
-
constructor( ...args: any[] ) {
|
|
53
|
-
if( isString(args[0] ) ) {
|
|
54
|
-
this.setValue( args[0] );
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
this.setRgb( args[0], args[1], args[2], args[3] );
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* accepts:
|
|
63
|
-
* #aaa
|
|
64
|
-
* #ababab
|
|
65
|
-
* #ababab55
|
|
66
|
-
* rgb(a,b,c)
|
|
67
|
-
* rgba(a,b,c,d)
|
|
68
|
-
* var( --color-5 )
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
setValue( value: string ): this {
|
|
72
|
-
|
|
73
|
-
this.invalid = false;
|
|
74
|
-
|
|
75
|
-
if( value.length==4 && /#[0-9a-fA-F]{3}/.test(value) ) {
|
|
76
|
-
const r1 = parseInt( value[1], 16 );
|
|
77
|
-
const g1 = parseInt( value[2], 16 );
|
|
78
|
-
const b1 = parseInt( value[3], 16 );
|
|
79
|
-
return this.setRgb( r1<<4|r1, g1<<4|g1, b1<<4|b1, 1.0 );
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if( value.length==7 && /#[0-9a-fA-F]{6}/.test(value) ) {
|
|
83
|
-
const r1 = parseInt( value[1], 16 );
|
|
84
|
-
const r2 = parseInt( value[2], 16 );
|
|
85
|
-
const g1 = parseInt( value[3], 16 );
|
|
86
|
-
const g2 = parseInt( value[4], 16 );
|
|
87
|
-
const b1 = parseInt( value[5], 16 );
|
|
88
|
-
const b2 = parseInt( value[6], 16 );
|
|
89
|
-
return this.setRgb( r1<<4|r2, g1<<4|g2, b1<<4|b2, 1.0 );
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if( value.length==9 && /#[0-9a-fA-F]{8}/.test(value) ) {
|
|
93
|
-
const r1 = parseInt( value[1], 16 );
|
|
94
|
-
const r2 = parseInt( value[2], 16 );
|
|
95
|
-
const g1 = parseInt( value[3], 16 );
|
|
96
|
-
const g2 = parseInt( value[4], 16 );
|
|
97
|
-
const b1 = parseInt( value[5], 16 );
|
|
98
|
-
const b2 = parseInt( value[6], 16 );
|
|
99
|
-
const a1 = parseInt( value[7], 16 );
|
|
100
|
-
const a2 = parseInt( value[8], 16 );
|
|
101
|
-
return this.setRgb( r1<<4|r2, g1<<4|g2, b1<<4|b2, (a1<<4|a2) / 255.0 );
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if( value.startsWith('rgba') ) {
|
|
105
|
-
const re = /rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*((\d+)|(\d*\.\d+)|(\.\d+))\s*\)/;
|
|
106
|
-
const m = re.exec( value );
|
|
107
|
-
if( m ) {
|
|
108
|
-
return this.setRgb( parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), parseFloat(m[4]) );
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
else if( value.startsWith('rgb') ) {
|
|
112
|
-
const re = /rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/;
|
|
113
|
-
const m = re.exec( value );
|
|
114
|
-
if( m ) {
|
|
115
|
-
return this.setRgb( parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), 1.0 );
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else if( value.startsWith("var") ) {
|
|
119
|
-
const re = /var\s*\(([^)]*)\)/;
|
|
120
|
-
const m = re.exec( value );
|
|
121
|
-
if( m ) {
|
|
122
|
-
const expr = m[1].trim( );
|
|
123
|
-
const style = getComputedStyle( document.documentElement );
|
|
124
|
-
const value = style.getPropertyValue( expr );
|
|
125
|
-
return this.setValue( value );
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
this.invalid = true;
|
|
130
|
-
return this.setRgb(255,0,0,1);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
setHsv( h: number, s: number, v: number, a = 1.0 ): this {
|
|
134
|
-
|
|
135
|
-
let i = Math.min(5, Math.floor(h * 6)),
|
|
136
|
-
f = h * 6 - i,
|
|
137
|
-
p = v * (1 - s),
|
|
138
|
-
q = v * (1 - f * s),
|
|
139
|
-
t = v * (1 - (1 - f) * s);
|
|
140
|
-
|
|
141
|
-
let R, G, B;
|
|
142
|
-
|
|
143
|
-
switch (i) {
|
|
144
|
-
case 0:
|
|
145
|
-
R = v;
|
|
146
|
-
G = t;
|
|
147
|
-
B = p;
|
|
148
|
-
break;
|
|
149
|
-
case 1:
|
|
150
|
-
R = q;
|
|
151
|
-
G = v;
|
|
152
|
-
B = p;
|
|
153
|
-
break;
|
|
154
|
-
case 2:
|
|
155
|
-
R = p;
|
|
156
|
-
G = v;
|
|
157
|
-
B = t;
|
|
158
|
-
break;
|
|
159
|
-
case 3:
|
|
160
|
-
R = p;
|
|
161
|
-
G = q;
|
|
162
|
-
B = v;
|
|
163
|
-
break;
|
|
164
|
-
case 4:
|
|
165
|
-
R = t;
|
|
166
|
-
G = p;
|
|
167
|
-
B = v;
|
|
168
|
-
break;
|
|
169
|
-
case 5:
|
|
170
|
-
R = v;
|
|
171
|
-
G = p;
|
|
172
|
-
B = q;
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return this.setRgb( R*255, G*255, B*255, a );
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
setRgb( r: number, g: number, b: number, a: number ): this {
|
|
181
|
-
this.rgb = [clamp(r,0,255),clamp(g,0,255),clamp(b,0,255),clamp(a,0,1)];
|
|
182
|
-
return this;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
toRgbString( withAlpha?: boolean ): string {
|
|
186
|
-
const _ = this.rgb;
|
|
187
|
-
return withAlpha===false || _[3]==1 ? `rgb(${round(_[0])},${round(_[1])},${round(_[2])})` : `rgba(${round(_[0])},${round(_[1])},${round(_[2])},${_[3].toFixed(3)})`
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
toHexString( ): string {
|
|
191
|
-
const _ = this.rgb;
|
|
192
|
-
return _[3]==1 ? `#(${hx(_[0])},${hx(_[1])},${hx(_[2])})` : `rgba(${hx(_[0])},${hx(_[1])},${hx(_[2])},${hx(_[3]*255)})`
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
toRgb( ): Rgb {
|
|
196
|
-
const _ = this.rgb;
|
|
197
|
-
return { red: _[0], green: _[1], blue: _[2], alpha: _[3] };
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
toHsv( ): Hsv {
|
|
201
|
-
|
|
202
|
-
let el = this.toRgb( );
|
|
203
|
-
|
|
204
|
-
el.red /= 255.0;
|
|
205
|
-
el.green /= 255.0;
|
|
206
|
-
el.blue /= 255.0;
|
|
207
|
-
|
|
208
|
-
const max = Math.max(el.red, el.green, el.blue);
|
|
209
|
-
const min = Math.min(el.red, el.green, el.blue);
|
|
210
|
-
const delta = max - min;
|
|
211
|
-
const saturation = (max === 0) ? 0 : (delta / max);
|
|
212
|
-
const value = max;
|
|
213
|
-
|
|
214
|
-
let hue;
|
|
215
|
-
|
|
216
|
-
if (delta === 0) {
|
|
217
|
-
hue = 0;
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
switch (max) {
|
|
221
|
-
case el.red:
|
|
222
|
-
hue = (el.green - el.blue) / delta / 6 + (el.green < el.blue ? 1 : 0);
|
|
223
|
-
break;
|
|
224
|
-
|
|
225
|
-
case el.green:
|
|
226
|
-
hue = (el.blue - el.red) / delta / 6 + 1 / 3;
|
|
227
|
-
break;
|
|
228
|
-
|
|
229
|
-
case el.blue:
|
|
230
|
-
hue = (el.red - el.green) / delta / 6 + 2 / 3;
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return { hue, saturation, value, alpha: el.alpha };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
getAlpha( ) {
|
|
239
|
-
return this.rgb[3];
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
setAlpha( a: number ): this {
|
|
243
|
-
this.rgb[3] = clamp( a, 0, 1 );
|
|
244
|
-
return this;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
isInvalid( ) {
|
|
248
|
-
return this.invalid;
|
|
249
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file core_colors.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
*
|
|
11
|
+
* @copyright (c) 2024 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* Use of this source code is governed by an MIT-style license
|
|
14
|
+
* that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
|
|
15
|
+
**/
|
|
16
|
+
|
|
17
|
+
import { clamp, isString } from './core_tools';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
function hx( v: number ) {
|
|
21
|
+
const hex = v.toString( 16 );
|
|
22
|
+
return hex.padStart( 2, '0' );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function round( v: number ) {
|
|
26
|
+
return Math.round(v);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export interface Rgb {
|
|
31
|
+
red: number;
|
|
32
|
+
green: number;
|
|
33
|
+
blue: number;
|
|
34
|
+
alpha: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface Hsv {
|
|
38
|
+
hue: number;
|
|
39
|
+
saturation: number;
|
|
40
|
+
value: number;
|
|
41
|
+
alpha: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export class Color {
|
|
46
|
+
|
|
47
|
+
private rgb: [red:number,green:number,blue:number,alpha:number] = [0,0,0,1];
|
|
48
|
+
private invalid = false;
|
|
49
|
+
|
|
50
|
+
constructor( value: string );
|
|
51
|
+
constructor( r: number, g: number, b: number, a?: number );
|
|
52
|
+
constructor( ...args: any[] ) {
|
|
53
|
+
if( isString(args[0] ) ) {
|
|
54
|
+
this.setValue( args[0] );
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.setRgb( args[0], args[1], args[2], args[3] );
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* accepts:
|
|
63
|
+
* #aaa
|
|
64
|
+
* #ababab
|
|
65
|
+
* #ababab55
|
|
66
|
+
* rgb(a,b,c)
|
|
67
|
+
* rgba(a,b,c,d)
|
|
68
|
+
* var( --color-5 )
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
setValue( value: string ): this {
|
|
72
|
+
|
|
73
|
+
this.invalid = false;
|
|
74
|
+
|
|
75
|
+
if( value.length==4 && /#[0-9a-fA-F]{3}/.test(value) ) {
|
|
76
|
+
const r1 = parseInt( value[1], 16 );
|
|
77
|
+
const g1 = parseInt( value[2], 16 );
|
|
78
|
+
const b1 = parseInt( value[3], 16 );
|
|
79
|
+
return this.setRgb( r1<<4|r1, g1<<4|g1, b1<<4|b1, 1.0 );
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if( value.length==7 && /#[0-9a-fA-F]{6}/.test(value) ) {
|
|
83
|
+
const r1 = parseInt( value[1], 16 );
|
|
84
|
+
const r2 = parseInt( value[2], 16 );
|
|
85
|
+
const g1 = parseInt( value[3], 16 );
|
|
86
|
+
const g2 = parseInt( value[4], 16 );
|
|
87
|
+
const b1 = parseInt( value[5], 16 );
|
|
88
|
+
const b2 = parseInt( value[6], 16 );
|
|
89
|
+
return this.setRgb( r1<<4|r2, g1<<4|g2, b1<<4|b2, 1.0 );
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if( value.length==9 && /#[0-9a-fA-F]{8}/.test(value) ) {
|
|
93
|
+
const r1 = parseInt( value[1], 16 );
|
|
94
|
+
const r2 = parseInt( value[2], 16 );
|
|
95
|
+
const g1 = parseInt( value[3], 16 );
|
|
96
|
+
const g2 = parseInt( value[4], 16 );
|
|
97
|
+
const b1 = parseInt( value[5], 16 );
|
|
98
|
+
const b2 = parseInt( value[6], 16 );
|
|
99
|
+
const a1 = parseInt( value[7], 16 );
|
|
100
|
+
const a2 = parseInt( value[8], 16 );
|
|
101
|
+
return this.setRgb( r1<<4|r2, g1<<4|g2, b1<<4|b2, (a1<<4|a2) / 255.0 );
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if( value.startsWith('rgba') ) {
|
|
105
|
+
const re = /rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*((\d+)|(\d*\.\d+)|(\.\d+))\s*\)/;
|
|
106
|
+
const m = re.exec( value );
|
|
107
|
+
if( m ) {
|
|
108
|
+
return this.setRgb( parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), parseFloat(m[4]) );
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if( value.startsWith('rgb') ) {
|
|
112
|
+
const re = /rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/;
|
|
113
|
+
const m = re.exec( value );
|
|
114
|
+
if( m ) {
|
|
115
|
+
return this.setRgb( parseInt(m[1]), parseInt(m[2]), parseInt(m[3]), 1.0 );
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if( value.startsWith("var") ) {
|
|
119
|
+
const re = /var\s*\(([^)]*)\)/;
|
|
120
|
+
const m = re.exec( value );
|
|
121
|
+
if( m ) {
|
|
122
|
+
const expr = m[1].trim( );
|
|
123
|
+
const style = getComputedStyle( document.documentElement );
|
|
124
|
+
const value = style.getPropertyValue( expr );
|
|
125
|
+
return this.setValue( value );
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.invalid = true;
|
|
130
|
+
return this.setRgb(255,0,0,1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
setHsv( h: number, s: number, v: number, a = 1.0 ): this {
|
|
134
|
+
|
|
135
|
+
let i = Math.min(5, Math.floor(h * 6)),
|
|
136
|
+
f = h * 6 - i,
|
|
137
|
+
p = v * (1 - s),
|
|
138
|
+
q = v * (1 - f * s),
|
|
139
|
+
t = v * (1 - (1 - f) * s);
|
|
140
|
+
|
|
141
|
+
let R, G, B;
|
|
142
|
+
|
|
143
|
+
switch (i) {
|
|
144
|
+
case 0:
|
|
145
|
+
R = v;
|
|
146
|
+
G = t;
|
|
147
|
+
B = p;
|
|
148
|
+
break;
|
|
149
|
+
case 1:
|
|
150
|
+
R = q;
|
|
151
|
+
G = v;
|
|
152
|
+
B = p;
|
|
153
|
+
break;
|
|
154
|
+
case 2:
|
|
155
|
+
R = p;
|
|
156
|
+
G = v;
|
|
157
|
+
B = t;
|
|
158
|
+
break;
|
|
159
|
+
case 3:
|
|
160
|
+
R = p;
|
|
161
|
+
G = q;
|
|
162
|
+
B = v;
|
|
163
|
+
break;
|
|
164
|
+
case 4:
|
|
165
|
+
R = t;
|
|
166
|
+
G = p;
|
|
167
|
+
B = v;
|
|
168
|
+
break;
|
|
169
|
+
case 5:
|
|
170
|
+
R = v;
|
|
171
|
+
G = p;
|
|
172
|
+
B = q;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return this.setRgb( R*255, G*255, B*255, a );
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
setRgb( r: number, g: number, b: number, a: number ): this {
|
|
181
|
+
this.rgb = [clamp(r,0,255),clamp(g,0,255),clamp(b,0,255),clamp(a,0,1)];
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
toRgbString( withAlpha?: boolean ): string {
|
|
186
|
+
const _ = this.rgb;
|
|
187
|
+
return withAlpha===false || _[3]==1 ? `rgb(${round(_[0])},${round(_[1])},${round(_[2])})` : `rgba(${round(_[0])},${round(_[1])},${round(_[2])},${_[3].toFixed(3)})`
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toHexString( ): string {
|
|
191
|
+
const _ = this.rgb;
|
|
192
|
+
return _[3]==1 ? `#(${hx(_[0])},${hx(_[1])},${hx(_[2])})` : `rgba(${hx(_[0])},${hx(_[1])},${hx(_[2])},${hx(_[3]*255)})`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
toRgb( ): Rgb {
|
|
196
|
+
const _ = this.rgb;
|
|
197
|
+
return { red: _[0], green: _[1], blue: _[2], alpha: _[3] };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
toHsv( ): Hsv {
|
|
201
|
+
|
|
202
|
+
let el = this.toRgb( );
|
|
203
|
+
|
|
204
|
+
el.red /= 255.0;
|
|
205
|
+
el.green /= 255.0;
|
|
206
|
+
el.blue /= 255.0;
|
|
207
|
+
|
|
208
|
+
const max = Math.max(el.red, el.green, el.blue);
|
|
209
|
+
const min = Math.min(el.red, el.green, el.blue);
|
|
210
|
+
const delta = max - min;
|
|
211
|
+
const saturation = (max === 0) ? 0 : (delta / max);
|
|
212
|
+
const value = max;
|
|
213
|
+
|
|
214
|
+
let hue;
|
|
215
|
+
|
|
216
|
+
if (delta === 0) {
|
|
217
|
+
hue = 0;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
switch (max) {
|
|
221
|
+
case el.red:
|
|
222
|
+
hue = (el.green - el.blue) / delta / 6 + (el.green < el.blue ? 1 : 0);
|
|
223
|
+
break;
|
|
224
|
+
|
|
225
|
+
case el.green:
|
|
226
|
+
hue = (el.blue - el.red) / delta / 6 + 1 / 3;
|
|
227
|
+
break;
|
|
228
|
+
|
|
229
|
+
case el.blue:
|
|
230
|
+
hue = (el.red - el.green) / delta / 6 + 2 / 3;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return { hue, saturation, value, alpha: el.alpha };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
getAlpha( ) {
|
|
239
|
+
return this.rgb[3];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
setAlpha( a: number ): this {
|
|
243
|
+
this.rgb[3] = clamp( a, 0, 1 );
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
isInvalid( ) {
|
|
248
|
+
return this.invalid;
|
|
249
|
+
}
|
|
250
250
|
}
|