x4js 2.0.18 → 2.0.21
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 +1 -0
- package/lib/cjs/x4.css +1 -1
- package/lib/cjs/x4.js +2 -2
- package/lib/esm/x4.css +1 -1
- package/lib/esm/x4.mjs +2 -2
- package/lib/styles/x4.css +1 -1
- package/lib/types/x4js.d.ts +92 -77
- package/package.json +1 -1
- package/src/components/boxes/boxes.ts +35 -3
- package/src/components/breadcrumb/breadcrumb.ts +4 -4
- package/src/components/btngroup/btngroup.ts +4 -4
- package/src/components/button/button.ts +2 -2
- package/src/components/calendar/calendar.ts +1 -1
- package/src/components/canvas/canvas.ts +2 -2
- package/src/components/checkbox/checkbox.ts +4 -4
- package/src/components/colorinput/colorinput.ts +3 -3
- package/src/components/combobox/combobox.module.scss +13 -0
- package/src/components/combobox/combobox.ts +10 -3
- package/src/components/dialog/dialog.ts +7 -7
- package/src/components/form/form.ts +5 -5
- package/src/components/gridview/gridview.ts +12 -9
- package/src/components/header/header.module.scss +2 -1
- package/src/components/header/header.ts +22 -10
- package/src/components/icon/icon.module.scss +1 -0
- package/src/components/icon/icon.ts +1 -1
- package/src/components/image/image.ts +3 -3
- package/src/components/input/input.ts +2 -2
- package/src/components/keyboard/keyboard.ts +5 -5
- package/src/components/label/label.module.scss +6 -1
- package/src/components/label/label.ts +1 -1
- package/src/components/listbox/listbox.module.scss +35 -31
- package/src/components/listbox/listbox.ts +53 -15
- package/src/components/messages/messages.ts +15 -1
- package/src/components/notification/notification.ts +8 -8
- package/src/components/popup/popup.ts +14 -3
- package/src/components/progress/progress.ts +1 -1
- package/src/components/propgrid/propgrid.ts +5 -3
- package/src/components/radio/radio.ts +2 -2
- package/src/components/rating/rating.ts +6 -6
- package/src/components/select/select.ts +1 -1
- package/src/components/sizers/sizer.ts +5 -5
- package/src/components/slider/slider.ts +3 -3
- package/src/components/switch/switch.ts +3 -3
- package/src/components/tabs/tabs.module.scss +2 -2
- package/src/components/tabs/tabs.ts +29 -5
- package/src/components/textarea/textarea.ts +1 -1
- package/src/components/textedit/textedit.ts +2 -2
- package/src/components/tickline/tickline.ts +1 -1
- package/src/components/tooltips/tooltips.ts +2 -2
- package/src/components/treeview/treeview.module.scss +9 -1
- package/src/components/treeview/treeview.ts +53 -14
- package/src/components/viewport/viewport.ts +1 -1
- package/src/core/component.ts +46 -9
- package/src/core/core_application.ts +3 -3
- package/src/core/core_data.ts +12 -12
- package/src/core/core_element.ts +1 -1
- package/src/core/core_react.ts +2 -2
- package/src/core/core_router.ts +2 -2
- package/src/core/core_state.ts +1 -1
- package/src/core/core_styles.ts +1 -1
- package/src/core/core_tools.ts +2 -0
- package/lib/src/components/base.scss +0 -25
- package/lib/src/components/boxes/boxes.module.scss +0 -54
- package/lib/src/components/boxes/boxes.ts +0 -370
- package/lib/src/components/breadcrumb/breadcrumb.scss +0 -56
- package/lib/src/components/breadcrumb/breadcrumb.ts +0 -93
- package/lib/src/components/breadcrumb/chevron-right.svg +0 -1
- package/lib/src/components/btngroup/btngroup.module.scss +0 -41
- package/lib/src/components/btngroup/btngroup.ts +0 -153
- package/lib/src/components/button/button.module.scss +0 -173
- package/lib/src/components/button/button.ts +0 -185
- package/lib/src/components/calendar/calendar-check-sharp-light.svg +0 -1
- package/lib/src/components/calendar/calendar.module.scss +0 -163
- package/lib/src/components/calendar/calendar.ts +0 -327
- package/lib/src/components/calendar/chevron-left-sharp-light.svg +0 -1
- package/lib/src/components/calendar/chevron-right-sharp-light.svg +0 -1
- package/lib/src/components/canvas/canvas.module.scss +0 -25
- package/lib/src/components/canvas/canvas.ts +0 -189
- package/lib/src/components/canvas/canvas_ex.ts +0 -276
- package/lib/src/components/checkbox/check.svg +0 -4
- package/lib/src/components/checkbox/checkbox.module.scss +0 -142
- package/lib/src/components/checkbox/checkbox.ts +0 -140
- package/lib/src/components/colorinput/colorinput.module.scss +0 -65
- package/lib/src/components/colorinput/colorinput.ts +0 -91
- package/lib/src/components/colorinput/crosshairs-simple-sharp-light.svg +0 -1
- package/lib/src/components/colorpicker/colorpicker.module.scss +0 -133
- package/lib/src/components/colorpicker/colorpicker.ts +0 -482
- package/lib/src/components/combobox/combobox.module.scss +0 -133
- package/lib/src/components/combobox/combobox.ts +0 -275
- package/lib/src/components/combobox/updown.svg +0 -4
- package/lib/src/components/components.ts +0 -42
- package/lib/src/components/dialog/dialog.module.scss +0 -104
- package/lib/src/components/dialog/dialog.ts +0 -229
- package/lib/src/components/dialog/xmark-sharp-light.svg +0 -1
- package/lib/src/components/filedrop/cloud-arrow-up.svg +0 -1
- package/lib/src/components/filedrop/filedrop.module.scss +0 -70
- package/lib/src/components/filedrop/filedrop.ts +0 -131
- package/lib/src/components/form/form.module.scss +0 -38
- package/lib/src/components/form/form.ts +0 -172
- package/lib/src/components/gridview/arrow-down-light.svg +0 -1
- package/lib/src/components/gridview/arrow-up-light.svg +0 -1
- package/lib/src/components/gridview/gridview.module.scss +0 -324
- package/lib/src/components/gridview/gridview.ts +0 -1175
- package/lib/src/components/header/header.module.scss +0 -40
- package/lib/src/components/header/header.ts +0 -130
- package/lib/src/components/icon/icon.module.scss +0 -31
- package/lib/src/components/icon/icon.ts +0 -137
- package/lib/src/components/image/image.module.scss +0 -28
- package/lib/src/components/image/image.ts +0 -168
- package/lib/src/components/input/input.module.scss +0 -74
- package/lib/src/components/input/input.ts +0 -422
- package/lib/src/components/keyboard/arrow-up.svg +0 -1
- package/lib/src/components/keyboard/delete-left.svg +0 -1
- package/lib/src/components/keyboard/eye-slash.svg +0 -1
- package/lib/src/components/keyboard/keyboard.module.scss +0 -134
- package/lib/src/components/keyboard/keyboard.ts +0 -526
- package/lib/src/components/label/label.module.scss +0 -76
- package/lib/src/components/label/label.ts +0 -97
- package/lib/src/components/link/link.ts +0 -81
- package/lib/src/components/listbox/listbox.module.scss +0 -161
- package/lib/src/components/listbox/listbox.ts +0 -539
- package/lib/src/components/menu/caret-right-solid.svg +0 -1
- package/lib/src/components/menu/menu.module.scss +0 -117
- package/lib/src/components/menu/menu.ts +0 -174
- package/lib/src/components/messages/circle-exclamation.svg +0 -1
- package/lib/src/components/messages/messages.module.scss +0 -92
- package/lib/src/components/messages/messages.ts +0 -215
- package/lib/src/components/messages/pen-field.svg +0 -1
- package/lib/src/components/normalize.scss +0 -391
- package/lib/src/components/notification/circle-check-solid.svg +0 -1
- package/lib/src/components/notification/circle-exclamation-solid.svg +0 -1
- package/lib/src/components/notification/circle-notch-light.svg +0 -1
- package/lib/src/components/notification/notification.module.scss +0 -84
- package/lib/src/components/notification/notification.ts +0 -107
- package/lib/src/components/notification/xmark-sharp-light.svg +0 -1
- package/lib/src/components/panel/panel.module.scss +0 -60
- package/lib/src/components/panel/panel.ts +0 -58
- package/lib/src/components/popup/popup.module.scss +0 -51
- package/lib/src/components/popup/popup.ts +0 -442
- package/lib/src/components/progress/progress.module.scss +0 -57
- package/lib/src/components/progress/progress.ts +0 -44
- package/lib/src/components/propgrid/folder-closed.svg +0 -1
- package/lib/src/components/propgrid/folder-open.svg +0 -1
- package/lib/src/components/propgrid/progrid.module.scss +0 -112
- package/lib/src/components/propgrid/propgrid.ts +0 -288
- package/lib/src/components/propgrid/updown.svg +0 -4
- package/lib/src/components/radio/radio.module.scss +0 -147
- package/lib/src/components/radio/radio.svg +0 -4
- package/lib/src/components/radio/radio.ts +0 -142
- package/lib/src/components/rating/rating.module.scss +0 -23
- package/lib/src/components/rating/rating.ts +0 -131
- package/lib/src/components/rating/star-sharp-light.svg +0 -1
- package/lib/src/components/rating/star-sharp-solid.svg +0 -1
- package/lib/src/components/select/select.module.scss +0 -9
- package/lib/src/components/select/select.ts +0 -134
- package/lib/src/components/shared.scss +0 -137
- package/lib/src/components/sizers/sizer.module.scss +0 -90
- package/lib/src/components/sizers/sizer.ts +0 -132
- package/lib/src/components/slider/slider.module.scss +0 -118
- package/lib/src/components/slider/slider.ts +0 -198
- package/lib/src/components/switch/switch.module.scss +0 -127
- package/lib/src/components/switch/switch.ts +0 -62
- package/lib/src/components/tabs/tabs.module.scss +0 -45
- package/lib/src/components/tabs/tabs.ts +0 -205
- package/lib/src/components/textarea/textarea.module.scss +0 -63
- package/lib/src/components/textarea/textarea.ts +0 -125
- package/lib/src/components/textedit/textedit.module.scss +0 -116
- package/lib/src/components/textedit/textedit.ts +0 -115
- package/lib/src/components/themes.scss +0 -88
- package/lib/src/components/tickline/tickline.module.scss +0 -26
- package/lib/src/components/tickline/tickline.ts +0 -82
- package/lib/src/components/tooltips/circle-info-sharp-light.svg +0 -1
- package/lib/src/components/tooltips/comments-question.svg +0 -1
- package/lib/src/components/tooltips/tooltips.scss +0 -72
- package/lib/src/components/tooltips/tooltips.ts +0 -109
- package/lib/src/components/treeview/chevron-down-light.svg +0 -1
- package/lib/src/components/treeview/treeview.module.scss +0 -185
- package/lib/src/components/treeview/treeview.ts +0 -445
- package/lib/src/components/viewport/viewport.module.scss +0 -32
- package/lib/src/components/viewport/viewport.ts +0 -41
- package/lib/src/core/component.ts +0 -1066
- package/lib/src/core/core_application.ts +0 -265
- package/lib/src/core/core_colors.ts +0 -250
- package/lib/src/core/core_data.ts +0 -1310
- package/lib/src/core/core_dom.ts +0 -471
- package/lib/src/core/core_dragdrop.ts +0 -201
- package/lib/src/core/core_element.ts +0 -115
- package/lib/src/core/core_events.ts +0 -177
- package/lib/src/core/core_i18n.ts +0 -393
- package/lib/src/core/core_react.ts +0 -79
- package/lib/src/core/core_router.ts +0 -237
- package/lib/src/core/core_state.ts +0 -62
- package/lib/src/core/core_styles.ts +0 -214
- package/lib/src/core/core_svg.ts +0 -712
- package/lib/src/core/core_tools.ts +0 -906
- package/lib/src/types/scss.d.ts +0 -4
- package/lib/src/types/svg.d.ts +0 -1
- package/lib/src/types/x4react.d.ts +0 -9
- package/lib/src/x4.scss +0 -19
- package/lib/src/x4.ts +0 -35
- package/lib/src/x4tsx.d.ts +0 -25
|
@@ -1,265 +0,0 @@
|
|
|
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, componentFromDOM } from './component.js';
|
|
18
|
-
import { CoreElement } from './core_element.js';
|
|
19
|
-
import { CoreEvent, EventMap } from './core_events';
|
|
20
|
-
import { getFocusableElements, ITabHandler } from './core_tools.js';
|
|
21
|
-
|
|
22
|
-
const socket_sent = Symbol( 'socket' );
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export interface EvMessage extends CoreEvent {
|
|
26
|
-
msg: string;
|
|
27
|
-
params: any;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface ApplicationEvents extends EventMap {
|
|
31
|
-
global: EvMessage;
|
|
32
|
-
message: EvMessage;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// signleton
|
|
36
|
-
let main_app: Application = null;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
class Process {
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* can be use to see if we have some tactile input
|
|
44
|
-
* @returns max touch point count
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
getMaxTouchPoints( ) {
|
|
48
|
-
return navigator.maxTouchPoints;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export class Application<E extends ApplicationEvents = ApplicationEvents> extends CoreElement<E> {
|
|
55
|
-
|
|
56
|
-
private env = new Map<string,any>( );
|
|
57
|
-
private mainview: Component;
|
|
58
|
-
|
|
59
|
-
static readonly process = new Process( );
|
|
60
|
-
|
|
61
|
-
constructor( ) {
|
|
62
|
-
super( );
|
|
63
|
-
|
|
64
|
-
console.assert( main_app==null, "Application must be a singleton." );
|
|
65
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
66
|
-
main_app = this;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
setMainView( view: Component ) {
|
|
70
|
-
this.mainview = view;
|
|
71
|
-
document.body.appendChild( view.dom );
|
|
72
|
-
this._setupKeyboard( );
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
static instance<P extends Application = Application>( ): P {
|
|
76
|
-
return main_app as P;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
*
|
|
81
|
-
*/
|
|
82
|
-
|
|
83
|
-
getMainView( ) {
|
|
84
|
-
return this.mainview;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
*
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
setEnv( name: string, value: any ) {
|
|
92
|
-
this.env.set( name, value );
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
*
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
getEnv( name: string, def_value?: any ) {
|
|
100
|
-
return this.env.get( name ) ?? def_value;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* small shortcut for Application.instance().fire( "global", ... );
|
|
105
|
-
*/
|
|
106
|
-
|
|
107
|
-
static fireGlobal( msg: string, params?: any ) {
|
|
108
|
-
Application.instance().fire( "global", { msg, params } );
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
*
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
private _setupKeyboard( ) {
|
|
116
|
-
|
|
117
|
-
document.addEventListener( "keydown", (ev) => {
|
|
118
|
-
if( ev.key=="Tab" || ev.key=="Enter" ) {
|
|
119
|
-
if( this.focusNext( !ev.shiftKey ) ) {
|
|
120
|
-
ev.preventDefault( );
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
} );
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
focusNext( next: boolean ) {
|
|
127
|
-
let act = document.activeElement;
|
|
128
|
-
let topmost: HTMLElement;
|
|
129
|
-
|
|
130
|
-
while( act!=document.body ) {
|
|
131
|
-
const comp = componentFromDOM(act);
|
|
132
|
-
const ifx = comp.queryInterface( "tab-handler") as ITabHandler;
|
|
133
|
-
|
|
134
|
-
if( ifx ) {
|
|
135
|
-
return ifx.focusNext( next );
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if( act.classList.contains("x4box") ) { // todo: that is too dirty
|
|
139
|
-
topmost = act as HTMLElement;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
act = act.parentElement;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if( topmost ) {
|
|
146
|
-
const focusable = getFocusableElements( topmost );
|
|
147
|
-
if( !focusable.length ) {
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
const first = focusable[0];
|
|
152
|
-
const last = focusable[focusable.length - 1];
|
|
153
|
-
|
|
154
|
-
let newf: HTMLElement;
|
|
155
|
-
if (!next && document.activeElement === first) {
|
|
156
|
-
newf = last as HTMLElement;
|
|
157
|
-
}
|
|
158
|
-
else if (next && document.activeElement === last) {
|
|
159
|
-
newf = first as HTMLElement;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if( newf ) {
|
|
163
|
-
newf.focus();
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
*
|
|
175
|
-
*/
|
|
176
|
-
|
|
177
|
-
setupSocketMessaging( path?: string, looseCallback?: ( ) => void ) {
|
|
178
|
-
|
|
179
|
-
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
|
|
180
|
-
const address = path ? protocol+path : `${protocol}${window.location.hostname}:${window.location.port}/ws`;
|
|
181
|
-
|
|
182
|
-
let opened = 0;
|
|
183
|
-
let msg_socket:WebSocket = null;
|
|
184
|
-
|
|
185
|
-
// we trap all 'global' messages send via application
|
|
186
|
-
// then we send them on websocket
|
|
187
|
-
|
|
188
|
-
this.on( 'global', ( e: EvMessage ) => {
|
|
189
|
-
if( Object.prototype.hasOwnProperty.call( e, socket_sent) ) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if( msg_socket ) {
|
|
194
|
-
msg_socket.send( JSON.stringify( {
|
|
195
|
-
msg: e.msg,
|
|
196
|
-
params: e.params,
|
|
197
|
-
} ) );
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
msg_socket = new WebSocket(address, 'messaging' );
|
|
202
|
-
|
|
203
|
-
msg_socket.onopen = ( ) => {
|
|
204
|
-
console.log( 'websocket opened' );
|
|
205
|
-
opened = 1;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// receive a message
|
|
209
|
-
msg_socket.onmessage = ( e ) => {
|
|
210
|
-
if( e.data!='ping' ) {
|
|
211
|
-
const message = JSON.parse(e.data);
|
|
212
|
-
message[socket_sent] = true;
|
|
213
|
-
this.fire( 'global', message );
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// loose socket
|
|
218
|
-
msg_socket.onclose = ( ev ) => {
|
|
219
|
-
console.log( 'websocket closed:', ev );
|
|
220
|
-
msg_socket = null;
|
|
221
|
-
|
|
222
|
-
if( opened ) {
|
|
223
|
-
looseCallback( );
|
|
224
|
-
opened = 0;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
//msg_socket.onerror = (ev )=> {
|
|
229
|
-
// console.log( 'websocket error:', ev );
|
|
230
|
-
//}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* get a local storage value
|
|
235
|
-
* @param name name of the value
|
|
236
|
-
* @returns the value (string) or undefined
|
|
237
|
-
*/
|
|
238
|
-
|
|
239
|
-
getStorage( name: string ) : string {
|
|
240
|
-
return localStorage.getItem( name );
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
getStorageJSON( name: string ) : any {
|
|
244
|
-
try {
|
|
245
|
-
return JSON.parse( localStorage.getItem( name ) );
|
|
246
|
-
}
|
|
247
|
-
catch( e ) {
|
|
248
|
-
return undefined;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* change a loclastorage value
|
|
254
|
-
* @param name name of the value
|
|
255
|
-
* @param value the value to store
|
|
256
|
-
*/
|
|
257
|
-
|
|
258
|
-
setStorage( name: string, value: string | number ) {
|
|
259
|
-
localStorage.setItem( name, value+'' );
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
setStorageJSON( name: string, value: any ) {
|
|
263
|
-
localStorage.setItem( name, JSON.stringify( value ) );
|
|
264
|
-
}
|
|
265
|
-
}
|
|
@@ -1,250 +0,0 @@
|
|
|
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,0,255),clamp(g|0,0,255),clamp(b|0,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])}` : `#${hx(_[0])}${hx(_[1])}${hx(_[2])}${(hx((_[3]*255)|0))}`
|
|
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
|
-
}
|