ozdenui 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.
- checksums.yaml +7 -0
- data/LICENSE +14 -0
- data/NOTICE +5 -0
- data/README.md +242 -0
- data/exe/ozdenui +17 -0
- data/lib/ozdenui/AGENTS.md +210 -0
- data/lib/ozdenui/COMPONENTS.md +3017 -0
- data/lib/ozdenui/app.rb +749 -0
- data/lib/ozdenui/command.rb +107 -0
- data/lib/ozdenui/components.rb +368 -0
- data/lib/ozdenui/dialogs.rb +212 -0
- data/lib/ozdenui/files.rb +124 -0
- data/lib/ozdenui/icon.png +0 -0
- data/lib/ozdenui/kit.json +1 -0
- data/lib/ozdenui/kit.rb +115 -0
- data/lib/ozdenui/look.rb +229 -0
- data/lib/ozdenui/objects.rb +652 -0
- data/lib/ozdenui/server.rb +270 -0
- data/lib/ozdenui/values.rb +273 -0
- data/lib/ozdenui/version.rb +15 -0
- data/lib/ozdenui/web/basic/LICENSE +14 -0
- data/lib/ozdenui/web/basic/basic.min.css +1 -0
- data/lib/ozdenui/web/basic/basic.min.js +78 -0
- data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
- data/lib/ozdenui/web/bridge.js +584 -0
- data/lib/ozdenui/web/comp/accordion.js +659 -0
- data/lib/ozdenui/web/comp/action-button/add.png +0 -0
- data/lib/ozdenui/web/comp/action-button.js +521 -0
- data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
- data/lib/ozdenui/web/comp/app-bar.js +237 -0
- data/lib/ozdenui/web/comp/badge.js +292 -0
- data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
- data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
- data/lib/ozdenui/web/comp/canvas.js +652 -0
- data/lib/ozdenui/web/comp/chat-view.js +827 -0
- data/lib/ozdenui/web/comp/check-box.js +490 -0
- data/lib/ozdenui/web/comp/code-editor.js +965 -0
- data/lib/ozdenui/web/comp/color-picker.js +636 -0
- data/lib/ozdenui/web/comp/context-menu.js +951 -0
- data/lib/ozdenui/web/comp/currency-field.js +209 -0
- data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/data-table.js +937 -0
- data/lib/ozdenui/web/comp/date-picker.js +1055 -0
- data/lib/ozdenui/web/comp/dialog.js +992 -0
- data/lib/ozdenui/web/comp/email-field.js +65 -0
- data/lib/ozdenui/web/comp/file-picker.js +920 -0
- data/lib/ozdenui/web/comp/filters.js +807 -0
- data/lib/ozdenui/web/comp/form.js +602 -0
- data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
- data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
- data/lib/ozdenui/web/comp/gauge.js +439 -0
- data/lib/ozdenui/web/comp/graph-box.js +3911 -0
- data/lib/ozdenui/web/comp/grid-group.js +185 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
- data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
- data/lib/ozdenui/web/comp/image-view.js +598 -0
- data/lib/ozdenui/web/comp/item-list.js +1740 -0
- data/lib/ozdenui/web/comp/json-editor.js +3221 -0
- data/lib/ozdenui/web/comp/json-view.js +597 -0
- data/lib/ozdenui/web/comp/label-editor.js +1140 -0
- data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
- data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
- data/lib/ozdenui/web/comp/left-menu.js +548 -0
- data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
- data/lib/ozdenui/web/comp/loading-screen.js +413 -0
- data/lib/ozdenui/web/comp/log-view.js +530 -0
- data/lib/ozdenui/web/comp/login-page.js +685 -0
- data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
- data/lib/ozdenui/web/comp/menu-bar.js +624 -0
- data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
- data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
- data/lib/ozdenui/web/comp/notice.js +229 -0
- data/lib/ozdenui/web/comp/number-field.js +128 -0
- data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
- data/lib/ozdenui/web/comp/page-control.js +762 -0
- data/lib/ozdenui/web/comp/page-header.js +191 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
- data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
- data/lib/ozdenui/web/comp/password-field.js +192 -0
- data/lib/ozdenui/web/comp/period-bar.js +436 -0
- data/lib/ozdenui/web/comp/phone-field.js +322 -0
- data/lib/ozdenui/web/comp/progress-bar.js +992 -0
- data/lib/ozdenui/web/comp/property-panel.js +3639 -0
- data/lib/ozdenui/web/comp/radio-button.js +527 -0
- data/lib/ozdenui/web/comp/range-slider.js +569 -0
- data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
- data/lib/ozdenui/web/comp/scroll-area.js +308 -0
- data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
- data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
- data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
- data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
- data/lib/ozdenui/web/comp/search-input.js +725 -0
- data/lib/ozdenui/web/comp/search-results.js +596 -0
- data/lib/ozdenui/web/comp/select-box.js +1862 -0
- data/lib/ozdenui/web/comp/settings-row.js +299 -0
- data/lib/ozdenui/web/comp/side-panel.js +616 -0
- data/lib/ozdenui/web/comp/slider-field.js +952 -0
- data/lib/ozdenui/web/comp/slider.js +838 -0
- data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
- data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-button.js +981 -0
- data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
- data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
- data/lib/ozdenui/web/comp/smart-table.js +1883 -0
- data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
- data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
- data/lib/ozdenui/web/comp/splitter.js +411 -0
- data/lib/ozdenui/web/comp/status-bar.js +408 -0
- data/lib/ozdenui/web/comp/step-bar.js +345 -0
- data/lib/ozdenui/web/comp/stepper.js +753 -0
- data/lib/ozdenui/web/comp/tabs.js +1366 -0
- data/lib/ozdenui/web/comp/tag-input.js +1107 -0
- data/lib/ozdenui/web/comp/tap-effect.js +170 -0
- data/lib/ozdenui/web/comp/text-area-field.js +233 -0
- data/lib/ozdenui/web/comp/text-field.js +1006 -0
- data/lib/ozdenui/web/comp/time-line.js +398 -0
- data/lib/ozdenui/web/comp/time-picker.js +1032 -0
- data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
- data/lib/ozdenui/web/comp/tiny-select.js +322 -0
- data/lib/ozdenui/web/comp/tiny-table.js +283 -0
- data/lib/ozdenui/web/comp/toast.js +891 -0
- data/lib/ozdenui/web/comp/toggle.js +757 -0
- data/lib/ozdenui/web/comp/tool-bar.js +760 -0
- data/lib/ozdenui/web/comp/tooltip.js +534 -0
- data/lib/ozdenui/web/comp/tree-view.js +1179 -0
- data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
- data/lib/ozdenui/web/comp/ui-standards.js +403 -0
- data/lib/ozdenui/web/comp/url-field.js +190 -0
- data/lib/ozdenui/web/comp/web-view.js +588 -0
- data/lib/ozdenui/web/components.js +123 -0
- data/lib/ozdenui/web/index.html +39 -0
- data/lib/ozdenui/window.rb +188 -0
- data/lib/ozdenui/window_cocoa.rb +221 -0
- data/lib/ozdenui.rb +32 -0
- metadata +200 -0
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
MiniGraphBox - v26.09
|
|
6
|
+
|
|
7
|
+
UI COMPONENT TEMPLATE
|
|
8
|
+
- A small box with a title, a value text, an icon and a bar graph.
|
|
9
|
+
- The title, value text and icon can be changed. (setTitleText, setValueText, setIcon)
|
|
10
|
+
- The bar color can be changed. (setBarColor) A value can have its own color too. (addValue(12, "red"))
|
|
11
|
+
- New values are added to the right. When the graph is full, the first value is removed
|
|
12
|
+
and all the bars move one step to the left.
|
|
13
|
+
- WHY: Bar objects are created one time. Only their heights and colors change. (Fast for live data)
|
|
14
|
+
- Bar count: As many bars as fit in the width. It is calculated again when the size changes.
|
|
15
|
+
- TOOLTIP ("at once, no delay"): the mouse over the bars shows the value of the bar under it in a
|
|
16
|
+
small box over the bar. The bar is found from the mouse x (the bars are thin), the box follows live values.
|
|
17
|
+
valueFormat(value, index, self) makes its text; showTooltip: 0 turns it off.
|
|
18
|
+
|
|
19
|
+
VALUE: A number, or { value, color }
|
|
20
|
+
|
|
21
|
+
USAGE:
|
|
22
|
+
const graph = MiniGraphBox({ title: "Sold Items", valueText: "1.3K", iconFile: "carrot.png", barColor: "tomato" });
|
|
23
|
+
graph.addValue(24);
|
|
24
|
+
graph.addValue(30, "orange");
|
|
25
|
+
graph.setValues([10, 20, 15]);
|
|
26
|
+
MiniGraphBox({ valueFormat: function (value) { return value + " items"; } });
|
|
27
|
+
|
|
28
|
+
Started Date: September 2026
|
|
29
|
+
Webpage: https://bug7a.github.io/js-components/
|
|
30
|
+
|
|
31
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
32
|
+
Licensed under the Apache License, Version 2.0
|
|
33
|
+
SPDX-License-Identifier: Apache-2.0
|
|
34
|
+
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
"use strict";
|
|
38
|
+
|
|
39
|
+
// Default values:
|
|
40
|
+
const MiniGraphBoxDefaults = {
|
|
41
|
+
key: "0",
|
|
42
|
+
width: 200,
|
|
43
|
+
height: 80,
|
|
44
|
+
titleText: "Sold Items",
|
|
45
|
+
valueText: "1.3K",
|
|
46
|
+
allowHtml: 0, // 1: titleText and valueText are HTML (only for your own texts)
|
|
47
|
+
iconFile: "", // "": No icon
|
|
48
|
+
barColor: "tomato",
|
|
49
|
+
values: [], // [10, 20, { value: 15, color: "orange" }]
|
|
50
|
+
maxValue: 0, // 0: Auto (the biggest visible value is the top of the graph)
|
|
51
|
+
animated: 1, // 1: Bar heights change with a short motion.
|
|
52
|
+
showTooltip: 1, // 1: the value of the bar under the mouse, in a small box (at once)
|
|
53
|
+
valueFormat: function (value, index, self) { return String(value); }, // The text of the tooltip
|
|
54
|
+
onClick: function (self) { },
|
|
55
|
+
style: {
|
|
56
|
+
box: {
|
|
57
|
+
color: "whitesmoke",
|
|
58
|
+
border: 1,
|
|
59
|
+
borderColor: Black(0.6),
|
|
60
|
+
round: 4,
|
|
61
|
+
},
|
|
62
|
+
text: {
|
|
63
|
+
padding: 8,
|
|
64
|
+
},
|
|
65
|
+
title: {
|
|
66
|
+
fontSize: 12,
|
|
67
|
+
textColor: Black(0.5),
|
|
68
|
+
},
|
|
69
|
+
valueText: {
|
|
70
|
+
fontSize: 20,
|
|
71
|
+
textColor: Black(0.75),
|
|
72
|
+
},
|
|
73
|
+
icon: {
|
|
74
|
+
width: 24,
|
|
75
|
+
height: 24,
|
|
76
|
+
},
|
|
77
|
+
bars: {
|
|
78
|
+
height: 30, // The tallest bar height
|
|
79
|
+
minHeight: 2, // Height for small values (bigger than 0)
|
|
80
|
+
barWidth: 3,
|
|
81
|
+
gap: 1,
|
|
82
|
+
padding: 2, // Left and right space
|
|
83
|
+
round: 1,
|
|
84
|
+
},
|
|
85
|
+
tooltip: {
|
|
86
|
+
color: Black(0.8),
|
|
87
|
+
textColor: "white",
|
|
88
|
+
fontSize: 11,
|
|
89
|
+
padding: [6, 2],
|
|
90
|
+
round: 4,
|
|
91
|
+
gap: 3, // between the tooltip and the top of the bar
|
|
92
|
+
barOpacity: 0.55, // the other bars while one is under the mouse
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const MiniGraphBox = function (params = {}) {
|
|
98
|
+
|
|
99
|
+
// Merge params:
|
|
100
|
+
mergeIntoIfMissing(params, MiniGraphBoxDefaults);
|
|
101
|
+
|
|
102
|
+
// Edit params, if needed:
|
|
103
|
+
const _bs = params.style.box;
|
|
104
|
+
params.color = _bs.color;
|
|
105
|
+
params.border = _bs.border;
|
|
106
|
+
params.borderColor = _bs.borderColor;
|
|
107
|
+
params.round = _bs.round;
|
|
108
|
+
|
|
109
|
+
// BOX: Component container
|
|
110
|
+
let box = startObject(params);
|
|
111
|
+
|
|
112
|
+
// *** PRIVATE VARIABLES:
|
|
113
|
+
const _s = box.style;
|
|
114
|
+
let barList = []; // Bar objects (created one time for the bar count)
|
|
115
|
+
let cancelResize = null; // (box.after: stopped by remove())
|
|
116
|
+
const BAR_MOTION = "height 0.2s, background-color 0.2s";
|
|
117
|
+
const NEW_BAR_MOTION = "height 0.2s";
|
|
118
|
+
const RESIZE_WAIT = 30; // ms
|
|
119
|
+
let hoverIndex = -1; // The bar under the mouse (-1: none)
|
|
120
|
+
|
|
121
|
+
// *** PUBLIC VARIABLES:
|
|
122
|
+
// [var] Visible values, from left to right: [{ value, color }]
|
|
123
|
+
box.values = [];
|
|
124
|
+
// [var] How many bars fit in the width.
|
|
125
|
+
box.barCount = 0;
|
|
126
|
+
|
|
127
|
+
// *** PRIVATE FUNCTIONS:
|
|
128
|
+
|
|
129
|
+
const setText = function (label, text) {
|
|
130
|
+
const value = (text === undefined || text === null) ? "" : String(text);
|
|
131
|
+
if (box.allowHtml == 1) label.text = value;
|
|
132
|
+
else label.plainText = value;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const normalizeValue = function (item) {
|
|
136
|
+
if (item !== null && typeof item == "object") {
|
|
137
|
+
return { value: basic.num(item.value) || 0, color: item.color || "" };
|
|
138
|
+
}
|
|
139
|
+
return { value: basic.num(item) || 0, color: "" };
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// How many bars fit in the inner width of the box.
|
|
143
|
+
const calcBarCount = function () {
|
|
144
|
+
const innerWidth = box.elem.clientWidth - (_s.bars.padding * 2);
|
|
145
|
+
const step = _s.bars.barWidth + _s.bars.gap;
|
|
146
|
+
// WHY: The last bar does not need a gap after it.
|
|
147
|
+
return Math.max(0, Math.floor((innerWidth + _s.bars.gap) / step));
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Keep only the last values that fit in the graph.
|
|
151
|
+
const trimValues = function () {
|
|
152
|
+
if (box.barCount <= 0) return; // WHY: made at width 0 (in a hidden group): the values wait for the bars
|
|
153
|
+
if (box.values.length > box.barCount) {
|
|
154
|
+
box.values = box.values.slice(box.values.length - box.barCount);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const getMaxValue = function () {
|
|
159
|
+
if (box.maxValue > 0) return box.maxValue;
|
|
160
|
+
let max = 0;
|
|
161
|
+
box.values.forEach(function (item) {
|
|
162
|
+
if (item.value > max) max = item.value;
|
|
163
|
+
});
|
|
164
|
+
return max;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// Create the bar objects. (Only when the bar count changes.)
|
|
168
|
+
const createBars = function () {
|
|
169
|
+
|
|
170
|
+
barList.forEach(function (bar) { bar.remove(); });
|
|
171
|
+
barList = [];
|
|
172
|
+
|
|
173
|
+
createIn(box.barGroup, function () {
|
|
174
|
+
|
|
175
|
+
for (let i = 0; i < box.barCount; i++) {
|
|
176
|
+
|
|
177
|
+
// BOX: Bar
|
|
178
|
+
const bar = Box({
|
|
179
|
+
width: _s.bars.barWidth,
|
|
180
|
+
height: 0,
|
|
181
|
+
color: box.barColor,
|
|
182
|
+
round: _s.bars.round,
|
|
183
|
+
});
|
|
184
|
+
bar.shrink = 0;
|
|
185
|
+
bar.motion = "";
|
|
186
|
+
barList.push(bar);
|
|
187
|
+
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// Change the motion only if it is different. (Fewer style changes on every update)
|
|
195
|
+
const setBarMotion = function (bar, motion) {
|
|
196
|
+
if (bar.motion === motion) return;
|
|
197
|
+
bar.motion = motion;
|
|
198
|
+
bar.setMotionNow(motion); // WHY: setMotion() waits a little (timeout); the motion must change before the height.
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const getBarHeight = function (item, max) {
|
|
202
|
+
if (item.value <= 0 || max <= 0) return 0;
|
|
203
|
+
const height = Math.round(Math.min(item.value, max) / max * _s.bars.height);
|
|
204
|
+
return Math.max(_s.bars.minHeight, height);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Write the values to the bars. (Bars are not moved; only their height and color change.)
|
|
208
|
+
// newBarIndex: The bar of the new value (addValue). Only this bar is animated.
|
|
209
|
+
// WHY: When the graph is full, every bar gets its right neighbour's value. If all of them are animated, the graph looks like a wave, not like moving to the left.
|
|
210
|
+
const updateBars = function (newBarIndex = -1) {
|
|
211
|
+
|
|
212
|
+
const max = getMaxValue();
|
|
213
|
+
|
|
214
|
+
barList.forEach(function (bar, index) {
|
|
215
|
+
|
|
216
|
+
const item = box.values[index];
|
|
217
|
+
const isNewBar = (index == newBarIndex);
|
|
218
|
+
|
|
219
|
+
if (box.animated != 1 || (newBarIndex >= 0 && !isNewBar)) {
|
|
220
|
+
setBarMotion(bar, "none");
|
|
221
|
+
} else if (!isNewBar) {
|
|
222
|
+
setBarMotion(bar, BAR_MOTION);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (!item) {
|
|
226
|
+
bar.height = 0;
|
|
227
|
+
bar.elem.title = "";
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const height = getBarHeight(item, max);
|
|
232
|
+
|
|
233
|
+
if (isNewBar && box.animated == 1) {
|
|
234
|
+
// WHY: This bar showed another value before (graph is full). Start from 0, so it rises like a new bar.
|
|
235
|
+
setBarMotion(bar, "none");
|
|
236
|
+
bar.height = 0;
|
|
237
|
+
bar.color = item.color || box.barColor;
|
|
238
|
+
void bar.elem.offsetHeight; // WHY: Apply height 0 now, before the motion starts.
|
|
239
|
+
setBarMotion(bar, NEW_BAR_MOTION);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
bar.height = height;
|
|
243
|
+
bar.color = item.color || box.barColor;
|
|
244
|
+
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
showTooltip(); // WHY: a live value under the mouse changes its text and its place too
|
|
248
|
+
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// *** TOOLTIP:
|
|
252
|
+
|
|
253
|
+
// The bar under the mouse: from its x over the bar group (the bars are too thin to point at).
|
|
254
|
+
// WHY the scale: page.fit() zooms the page with a transform; clientX is in screen pixels.
|
|
255
|
+
const findBarIndex = function (event) {
|
|
256
|
+
const rect = box.barGroup.elem.getBoundingClientRect();
|
|
257
|
+
const scale = rect.width / (box.barGroup.elem.offsetWidth || 1);
|
|
258
|
+
const x = (event.clientX - rect.left) / scale - _s.bars.padding;
|
|
259
|
+
const step = _s.bars.barWidth + _s.bars.gap;
|
|
260
|
+
const index = Math.floor((x + _s.bars.gap / 2) / step);
|
|
261
|
+
if (index < 0 || index >= box.values.length) return -1;
|
|
262
|
+
return index;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// The other bars are fainter while one is under the mouse.
|
|
266
|
+
const paintBars = function () {
|
|
267
|
+
barList.forEach(function (bar, index) {
|
|
268
|
+
const isFaint = (hoverIndex >= 0 && index != hoverIndex);
|
|
269
|
+
bar.opacity = isFaint ? _s.tooltip.barOpacity : 1;
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// The box over the bar of hoverIndex, inside the graph (it is not cut at the sides).
|
|
274
|
+
const showTooltip = function () {
|
|
275
|
+
|
|
276
|
+
const item = box.values[hoverIndex];
|
|
277
|
+
const bar = barList[hoverIndex];
|
|
278
|
+
if (!item || !bar || box.showTooltip != 1) {
|
|
279
|
+
// WHY: fewer values under a still mouse: the other bars must not stay faint
|
|
280
|
+
if (hoverIndex >= 0 && (!item || !bar)) {
|
|
281
|
+
hoverIndex = -1;
|
|
282
|
+
paintBars();
|
|
283
|
+
}
|
|
284
|
+
box.lblTooltip.visible = 0;
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
box.lblTooltip.plainText = box.valueFormat(item.value, hoverIndex, box);
|
|
289
|
+
box.lblTooltip.visible = 1;
|
|
290
|
+
|
|
291
|
+
const barCenter = box.barGroup.elem.offsetLeft + bar.elem.offsetLeft + bar.elem.offsetWidth / 2;
|
|
292
|
+
const tipWidth = box.lblTooltip.elem.offsetWidth;
|
|
293
|
+
const tipHeight = box.lblTooltip.elem.offsetHeight;
|
|
294
|
+
const maxLeft = box.elem.clientWidth - tipWidth;
|
|
295
|
+
const barTop = box.barGroup.elem.offsetTop + box.barGroup.elem.offsetHeight - getBarHeight(item, getMaxValue());
|
|
296
|
+
box.lblTooltip.left = Math.round(Math.max(0, Math.min(maxLeft, barCenter - tipWidth / 2)));
|
|
297
|
+
box.lblTooltip.top = Math.round(Math.max(0, barTop - tipHeight - _s.tooltip.gap));
|
|
298
|
+
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const setHoverIndex = function (index) {
|
|
302
|
+
if (index == hoverIndex) return;
|
|
303
|
+
hoverIndex = index;
|
|
304
|
+
paintBars();
|
|
305
|
+
showTooltip();
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
// Check the bar count again. (The box size can change: width "100%")
|
|
309
|
+
const refreshSize = function () {
|
|
310
|
+
|
|
311
|
+
if (!box) return;
|
|
312
|
+
|
|
313
|
+
const barCount = calcBarCount();
|
|
314
|
+
|
|
315
|
+
if (barCount != box.barCount) {
|
|
316
|
+
box.barCount = barCount;
|
|
317
|
+
createBars();
|
|
318
|
+
trimValues();
|
|
319
|
+
hoverIndex = -1;
|
|
320
|
+
paintBars();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
updateBars();
|
|
324
|
+
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// *** PUBLIC FUNCTIONS:
|
|
328
|
+
|
|
329
|
+
// Add a value to the right. If the graph is full, the first value is removed.
|
|
330
|
+
box.addValue = function (value, color) {
|
|
331
|
+
|
|
332
|
+
const item = normalizeValue(value);
|
|
333
|
+
if (color) item.color = color;
|
|
334
|
+
|
|
335
|
+
box.values.push(item);
|
|
336
|
+
trimValues();
|
|
337
|
+
updateBars(box.values.length - 1);
|
|
338
|
+
|
|
339
|
+
};
|
|
340
|
+
// USAGE: graph.addValue(24) or graph.addValue(24, "orange")
|
|
341
|
+
|
|
342
|
+
box.setValues = function (values) {
|
|
343
|
+
box.values = (values || []).map(normalizeValue);
|
|
344
|
+
trimValues();
|
|
345
|
+
updateBars();
|
|
346
|
+
};
|
|
347
|
+
// USAGE: graph.setValues([10, 20, { value: 15, color: "orange" }])
|
|
348
|
+
|
|
349
|
+
box.getValues = function () {
|
|
350
|
+
return box.values.map(function (item) { return item.value; });
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
box.getLastValue = function () {
|
|
354
|
+
const item = box.values[box.values.length - 1];
|
|
355
|
+
return (item) ? item.value : null;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
box.clear = function () {
|
|
359
|
+
box.values = [];
|
|
360
|
+
updateBars();
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
box.setTitleText = function (text) {
|
|
364
|
+
box.titleText = text;
|
|
365
|
+
setText(box.lblTitle, text);
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
box.setValueText = function (text) {
|
|
369
|
+
box.valueText = text;
|
|
370
|
+
setText(box.lblValueText, text);
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
box.setIcon = function (iconFile) {
|
|
374
|
+
box.iconFile = iconFile || "";
|
|
375
|
+
box.icon.visible = (box.iconFile) ? 1 : 0;
|
|
376
|
+
if (box.iconFile) box.icon.load(box.iconFile);
|
|
377
|
+
};
|
|
378
|
+
// USAGE: graph.setIcon("assets/carrot.png") or graph.setIcon("") to hide it
|
|
379
|
+
|
|
380
|
+
// Color of all the bars. (Values with their own color keep it.)
|
|
381
|
+
box.setBarColor = function (color) {
|
|
382
|
+
box.barColor = color;
|
|
383
|
+
updateBars();
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
// 0: Auto
|
|
387
|
+
box.setMaxValue = function (maxValue) {
|
|
388
|
+
box.maxValue = basic.num(maxValue) || 0;
|
|
389
|
+
updateBars();
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
box.refresh = function () {
|
|
393
|
+
refreshSize();
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
// *** OBJECT VIEW:
|
|
398
|
+
|
|
399
|
+
// GROUP: Bars (at the bottom)
|
|
400
|
+
box.barGroup = HGroup({
|
|
401
|
+
align: "left bottom",
|
|
402
|
+
gap: _s.bars.gap,
|
|
403
|
+
padding: [_s.bars.padding, 0],
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
endGroup();
|
|
407
|
+
|
|
408
|
+
// GROUP: Title and value text (left top)
|
|
409
|
+
VGroup({
|
|
410
|
+
align: "left top",
|
|
411
|
+
padding: _s.text.padding,
|
|
412
|
+
gap: 0,
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
box.lblTitle = Label({
|
|
416
|
+
fontSize: _s.title.fontSize,
|
|
417
|
+
textColor: _s.title.textColor,
|
|
418
|
+
});
|
|
419
|
+
setText(box.lblTitle, box.titleText);
|
|
420
|
+
// WHY: marginBottom: Line heights of the two texts leave a big space between them.
|
|
421
|
+
box.lblTitle.css = { whiteSpace: "nowrap", marginBottom: "-6px" };
|
|
422
|
+
|
|
423
|
+
box.lblValueText = Label({
|
|
424
|
+
fontSize: _s.valueText.fontSize,
|
|
425
|
+
textColor: _s.valueText.textColor,
|
|
426
|
+
});
|
|
427
|
+
setText(box.lblValueText, box.valueText);
|
|
428
|
+
box.lblValueText.css.whiteSpace = "nowrap";
|
|
429
|
+
|
|
430
|
+
endGroup();
|
|
431
|
+
|
|
432
|
+
// GROUP: Icon (right top)
|
|
433
|
+
HGroup({
|
|
434
|
+
align: "right top",
|
|
435
|
+
padding: _s.text.padding,
|
|
436
|
+
gap: 0,
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
box.icon = Icon({
|
|
440
|
+
width: _s.icon.width,
|
|
441
|
+
height: _s.icon.height,
|
|
442
|
+
});
|
|
443
|
+
box.icon.elem.alt = "";
|
|
444
|
+
|
|
445
|
+
endGroup();
|
|
446
|
+
|
|
447
|
+
// LABEL: Tooltip (the value of the bar under the mouse; no motion: it is seen at once)
|
|
448
|
+
box.lblTooltip = Label({
|
|
449
|
+
left: 0,
|
|
450
|
+
top: 0,
|
|
451
|
+
width: "auto",
|
|
452
|
+
height: "auto",
|
|
453
|
+
color: _s.tooltip.color,
|
|
454
|
+
textColor: _s.tooltip.textColor,
|
|
455
|
+
fontSize: _s.tooltip.fontSize,
|
|
456
|
+
padding: _s.tooltip.padding,
|
|
457
|
+
round: _s.tooltip.round,
|
|
458
|
+
visible: 0,
|
|
459
|
+
css: { whiteSpace: "nowrap", pointerEvents: "none" },
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
// *** OBJECT INIT CODE:
|
|
463
|
+
|
|
464
|
+
box.on("click", function () { box.onClick(box); });
|
|
465
|
+
box.cursor = "default";
|
|
466
|
+
box.clickable = 1; // WHY: the mouse events of the tooltip (a basic.js box gets none by default)
|
|
467
|
+
box.on("mousemove", function (self, event) {
|
|
468
|
+
setHoverIndex(findBarIndex(event));
|
|
469
|
+
});
|
|
470
|
+
box.on("mouseleave", function () {
|
|
471
|
+
setHoverIndex(-1);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
box.setIcon(box.iconFile);
|
|
475
|
+
|
|
476
|
+
box.values = (params.values || []).map(normalizeValue); // WHY: Copy. The default [] must not be shared between components.
|
|
477
|
+
box.barCount = calcBarCount();
|
|
478
|
+
createBars();
|
|
479
|
+
trimValues();
|
|
480
|
+
updateBars();
|
|
481
|
+
|
|
482
|
+
box.onResize(function () {
|
|
483
|
+
if (cancelResize) cancelResize();
|
|
484
|
+
cancelResize = box.after(RESIZE_WAIT, function () {
|
|
485
|
+
cancelResize = null;
|
|
486
|
+
refreshSize();
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
return endObject(box);
|
|
491
|
+
|
|
492
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/* Bismillah */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
MinimalAccent - v26.11
|
|
6
|
+
|
|
7
|
+
HELPER
|
|
8
|
+
- The minimal theme (_designs/minimal-theme.htm) in a color of the page. MinimalAccent.addPackages(name, color) gives
|
|
9
|
+
every minimal component loaded before it two new packages: name (a light window) and name + "Dark" (a dark window).
|
|
10
|
+
- The color goes where the minimal theme draws its ink: a toggle that is on, a checked box, the fill of a slider and
|
|
11
|
+
of a progress bar, the selected tab, row and day, the edge of a field while typing, the main button. The lines, the
|
|
12
|
+
texts and the focus ring stay as they are.
|
|
13
|
+
- The text on the color (a selected day, the value of a slider): white or dark, the one that reads better on it.
|
|
14
|
+
- The dark window takes darkColor; without it, the same color made lighter until it reads on the dark ground (4.5:1).
|
|
15
|
+
The light window takes the color as it is: choose one that reads on white (4.5:1, Ex: "#2A78D6"), it is the text of
|
|
16
|
+
a selected tab too.
|
|
17
|
+
- SmartButton: the templates name / name + "Dark" (the minimal ones) and the tones name / name + "Dark" (the main
|
|
18
|
+
action in the color). Toast: the themes (the action text in the color). Tooltip: the looks. GraphBox keeps its
|
|
19
|
+
minimal theme (its series have their own colors).
|
|
20
|
+
- A component makes its minimal packages with X.createMinimalStyle(X.MINIMAL_COLORS) (and X.MINIMAL_DARK_COLORS): the
|
|
21
|
+
new ones come from the same function. A component with minimal packages and without those colors gets them as
|
|
22
|
+
they are under the new names.
|
|
23
|
+
- Call it after the components are loaded (Ex: at the start of start()); a component loaded later does not have them.
|
|
24
|
+
|
|
25
|
+
USAGE:
|
|
26
|
+
const look = MinimalAccent.addPackages("minimalBlue", "#2A78D6"); // { light: "minimalBlue", dark: "minimalBlueDark" }
|
|
27
|
+
Toggle({ styleName: "minimalBlue", value: 1 });
|
|
28
|
+
CheckBox({ styleName: "minimalBlue", text: "Remember me" });
|
|
29
|
+
SmartButton({ styleName: "minimalBlue", tone: "minimalBlue", labelText: "Save" }); // the main action
|
|
30
|
+
Toast.show({ messageText: "Saved", theme: "minimalBlue" });
|
|
31
|
+
MinimalAccent.addPackages("minimalGreen", "#1F7A5C", "#7FE3B5"); // the color of the dark window too
|
|
32
|
+
|
|
33
|
+
Started Date: November 2026
|
|
34
|
+
Webpage: https://bug7a.github.io/js-components/
|
|
35
|
+
|
|
36
|
+
Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
37
|
+
Licensed under the Apache License, Version 2.0
|
|
38
|
+
SPDX-License-Identifier: Apache-2.0
|
|
39
|
+
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
"use strict";
|
|
43
|
+
|
|
44
|
+
const MinimalAccent = {};
|
|
45
|
+
|
|
46
|
+
// The components that have minimal packages, by name: one that is not loaded is skipped.
|
|
47
|
+
MinimalAccent.COMPONENTS = [
|
|
48
|
+
"Accordion", "ChatView", "CheckBox", "ContextMenu", "DataTable", "Dialog", "TextField", "ItemList", "JsonEditor", "LoadingScreen",
|
|
49
|
+
"MenuBar", "ProgressBar", "RadioButton", "RangeSlider", "ScrollArea", "SearchInput", "SelectBox", "ColorPicker",
|
|
50
|
+
"DatePicker", "FilePicker", "TimePicker", "SidePanel", "Slider", "SliderField", "SmartTable", "Splitter", "StatusBar",
|
|
51
|
+
"StepBar", "Stepper", "Tabs", "TagInput", "TimeLine", "Toggle", "ToolBar", "TreeView",
|
|
52
|
+
];
|
|
53
|
+
MinimalAccent.WHITE = "#FFFFFF";
|
|
54
|
+
MinimalAccent.DARK_GROUND = "#1A1A19"; // the ground of a dark minimal window, and the dark text on a light color
|
|
55
|
+
MinimalAccent.READABLE_CONTRAST = 4.5; // WCAG for a text
|
|
56
|
+
MinimalAccent.HOVER_SHADE = 0.12; // the color under the mouse: 12 % darker on a light window, lighter on a dark one
|
|
57
|
+
MinimalAccent.VERY_DARK = 0.03; // a luminance under it gets lighter under the mouse (darker would not be seen)
|
|
58
|
+
MinimalAccent.LIGHTEN_STEP = 0.05;
|
|
59
|
+
|
|
60
|
+
// *** PUBLIC FUNCTIONS:
|
|
61
|
+
|
|
62
|
+
// Makes the packages name and name + "Dark" and returns their names: { light, dark }.
|
|
63
|
+
MinimalAccent.addPackages = function (name, color, darkColor = "") {
|
|
64
|
+
const names = { light: name, dark: name + "Dark" };
|
|
65
|
+
const lightInk = MinimalAccent.createInk(color, "light");
|
|
66
|
+
const darkInk = MinimalAccent.createInk(darkColor || MinimalAccent.lightenForDarkGround(color), "dark");
|
|
67
|
+
for (const componentName of MinimalAccent.COMPONENTS) {
|
|
68
|
+
const component = MinimalAccent.findComponent(componentName);
|
|
69
|
+
if (!component || !component.styles || !component.styles.minimal) continue;
|
|
70
|
+
component.styles[names.light] = MinimalAccent.createPackage(component, "light", lightInk, names);
|
|
71
|
+
component.styles[names.dark] = MinimalAccent.createPackage(component, "dark", darkInk, names);
|
|
72
|
+
}
|
|
73
|
+
MinimalAccent.addButtonLooks(names, lightInk, darkInk);
|
|
74
|
+
MinimalAccent.addToastThemes(names, lightInk, darkInk);
|
|
75
|
+
MinimalAccent.addTooltipLooks(names);
|
|
76
|
+
return names;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// The colors a package takes from one color: { ink, inkHover, onInk }.
|
|
80
|
+
MinimalAccent.createInk = function (color, theme) {
|
|
81
|
+
const rgb = MinimalAccent.toRgb(color);
|
|
82
|
+
const isVeryDark = (MinimalAccent.luminance(rgb) < MinimalAccent.VERY_DARK);
|
|
83
|
+
const isLighterUnderMouse = (theme === "dark") || isVeryDark;
|
|
84
|
+
const hoverTarget = MinimalAccent.toRgb(isLighterUnderMouse ? MinimalAccent.WHITE : "#000000");
|
|
85
|
+
return {
|
|
86
|
+
ink: MinimalAccent.toHex(rgb),
|
|
87
|
+
inkHover: MinimalAccent.toHex(MinimalAccent.mix(rgb, hoverTarget, MinimalAccent.HOVER_SHADE)),
|
|
88
|
+
onInk: MinimalAccent.textOn(rgb),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// The color of a dark window from the color of a light one: lighter, until it reads on the dark ground.
|
|
93
|
+
MinimalAccent.lightenForDarkGround = function (color) {
|
|
94
|
+
const rgb = MinimalAccent.toRgb(color);
|
|
95
|
+
const ground = MinimalAccent.toRgb(MinimalAccent.DARK_GROUND);
|
|
96
|
+
const white = MinimalAccent.toRgb(MinimalAccent.WHITE);
|
|
97
|
+
for (let amount = 0; amount < 1; amount += MinimalAccent.LIGHTEN_STEP) {
|
|
98
|
+
const lighter = MinimalAccent.mix(rgb, white, amount);
|
|
99
|
+
if (MinimalAccent.contrast(lighter, ground) >= MinimalAccent.READABLE_CONTRAST) return MinimalAccent.toHex(lighter);
|
|
100
|
+
}
|
|
101
|
+
return MinimalAccent.WHITE;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// *** PRIVATE FUNCTIONS:
|
|
105
|
+
|
|
106
|
+
// A component by its name, or null when it is not loaded.
|
|
107
|
+
// WHY Function(): the components are top-level "const" (const Toggle = function...), so they are not on window; only
|
|
108
|
+
// the fixed names of this file go in.
|
|
109
|
+
MinimalAccent.findComponent = function (name) {
|
|
110
|
+
return Function("return (typeof " + name + " !== \"undefined\") ? " + name + " : null;")();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
MinimalAccent.createPackage = function (component, theme, ink, names) {
|
|
114
|
+
const isDark = (theme === "dark");
|
|
115
|
+
const colors = isDark ? component.MINIMAL_DARK_COLORS : component.MINIMAL_COLORS;
|
|
116
|
+
const minimalPackage = isDark ? component.styles.minimalDark : component.styles.minimal;
|
|
117
|
+
if (!colors || !component.createMinimalStyle) return minimalPackage;
|
|
118
|
+
return component.createMinimalStyle(MinimalAccent.withInk(colors, ink, isDark, names));
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// The colors of a component with the new ink. A package or a tone of the theme that they name (the fields of a Dialog,
|
|
122
|
+
// its main button "ink", the search box of a list) becomes the new one; "minimalDanger" stays.
|
|
123
|
+
MinimalAccent.withInk = function (colors, ink, isDark, names) {
|
|
124
|
+
const newName = isDark ? names.dark : names.light;
|
|
125
|
+
const oldNames = isDark ? ["minimalDark", "inkDark"] : ["minimal", "ink"];
|
|
126
|
+
const result = Object.assign({}, colors, ink);
|
|
127
|
+
for (const key of Object.keys(colors)) {
|
|
128
|
+
if (oldNames.includes(colors[key])) result[key] = newName;
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
MinimalAccent.addButtonLooks = function (names, lightInk, darkInk) {
|
|
134
|
+
const button = MinimalAccent.findComponent("SmartButton");
|
|
135
|
+
if (!button || !button.styles.minimal) return;
|
|
136
|
+
button.styles[names.light] = button.styles.minimal;
|
|
137
|
+
button.styles[names.dark] = button.styles.minimalDark;
|
|
138
|
+
button.tones[names.light] = MinimalAccent.createButtonTone(button, lightInk);
|
|
139
|
+
button.tones[names.dark] = MinimalAccent.createButtonTone(button, darkInk);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// The tone of the main action: filled with the color, its edge in it too (as the tone "ink").
|
|
143
|
+
MinimalAccent.createButtonTone = function (button, ink) {
|
|
144
|
+
// WHY LIGHT_TEXT: with it SmartButton turns the icon white too
|
|
145
|
+
const text = (ink.onInk === MinimalAccent.WHITE) ? button.LIGHT_TEXT : ink.onInk;
|
|
146
|
+
return { color: ink.ink, hover: ink.inkHover, text: text, edge: ink.ink, hoverEdge: ink.inkHover };
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
MinimalAccent.addToastThemes = function (names, lightInk, darkInk) {
|
|
150
|
+
const toast = MinimalAccent.findComponent("Toast");
|
|
151
|
+
if (!toast || !toast.THEMES || !toast.THEMES.minimal) return;
|
|
152
|
+
toast.THEMES[names.light] = Object.assign({}, toast.THEMES.minimal, { actionText: lightInk.ink });
|
|
153
|
+
toast.THEMES[names.dark] = Object.assign({}, toast.THEMES.minimalDark, { actionText: darkInk.ink });
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
MinimalAccent.addTooltipLooks = function (names) {
|
|
157
|
+
const tooltip = MinimalAccent.findComponent("Tooltip");
|
|
158
|
+
if (!tooltip || !tooltip.looks || !tooltip.looks.minimal) return;
|
|
159
|
+
tooltip.looks[names.light] = tooltip.looks.minimal;
|
|
160
|
+
tooltip.looks[names.dark] = tooltip.looks.minimalDark;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// *** COLORS:
|
|
164
|
+
|
|
165
|
+
// [r, g, b] of a CSS color ("#2A78D6", "teal", "rgb(42, 120, 214)").
|
|
166
|
+
// WHY a canvas: it reads every CSS color and writes it as "#rrggbb" ("rgba(...)" when it is not opaque).
|
|
167
|
+
MinimalAccent.toRgb = function (color) {
|
|
168
|
+
if (!MinimalAccent.colorReader) MinimalAccent.colorReader = document.createElement("canvas").getContext("2d");
|
|
169
|
+
const reader = MinimalAccent.colorReader;
|
|
170
|
+
const unread = "#010203";
|
|
171
|
+
reader.fillStyle = unread;
|
|
172
|
+
reader.fillStyle = String(color);
|
|
173
|
+
const value = reader.fillStyle;
|
|
174
|
+
if (value === unread && String(color).toLowerCase() !== unread) console.warn("MinimalAccent: not a color: " + color);
|
|
175
|
+
if (value.startsWith("#")) return [1, 3, 5].map((at) => parseInt(value.slice(at, at + 2), 16));
|
|
176
|
+
return value.match(/[\d.]+/g).slice(0, 3).map(Number);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
MinimalAccent.toHex = function (rgb) {
|
|
180
|
+
return "#" + rgb.map((part) => Math.round(part).toString(16).padStart(2, "0")).join("").toUpperCase();
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// rgb moved toward target by amount (0: rgb, 1: target).
|
|
184
|
+
MinimalAccent.mix = function (rgb, target, amount) {
|
|
185
|
+
return rgb.map((part, index) => part + (target[index] - part) * amount);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// The relative luminance of WCAG (0: black, 1: white).
|
|
189
|
+
MinimalAccent.luminance = function (rgb) {
|
|
190
|
+
const linear = rgb.map(function (part) {
|
|
191
|
+
const value = part / 255;
|
|
192
|
+
return (value <= 0.03928) ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
|
|
193
|
+
});
|
|
194
|
+
return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2];
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
MinimalAccent.contrast = function (rgb, otherRgb) {
|
|
198
|
+
const first = MinimalAccent.luminance(rgb);
|
|
199
|
+
const second = MinimalAccent.luminance(otherRgb);
|
|
200
|
+
return (Math.max(first, second) + 0.05) / (Math.min(first, second) + 0.05);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// White or the dark ground: the one that reads better on rgb.
|
|
204
|
+
MinimalAccent.textOn = function (rgb) {
|
|
205
|
+
const onWhite = MinimalAccent.contrast(rgb, MinimalAccent.toRgb(MinimalAccent.WHITE));
|
|
206
|
+
const onDark = MinimalAccent.contrast(rgb, MinimalAccent.toRgb(MinimalAccent.DARK_GROUND));
|
|
207
|
+
return (onWhite >= onDark) ? MinimalAccent.WHITE : MinimalAccent.DARK_GROUND;
|
|
208
|
+
};
|