-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
497 lines (427 loc) · 17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//#region ST imports
import { eventSource, event_types, saveSettingsDebounced } from "../../../../script.js";
import { extension_settings } from "../../../extensions.js";
//#endregion ST imports
//#region Local imports
import { ExColor } from "./ExColor.js";
import { CharacterType, STCharacter } from "./STCharacter.js";
import { getImageVibrant, getValidSwatch } from "./color-utils.js";
import { createColorSourceDropdown, createColorTargetDropdown, createColorTextPickerCombo } from "./element-creators.js";
import { initializeSettings } from "./settings-utils.js";
import {
expEventSource,
exp_event_type,
getAllPersonas,
getCharacterBeingEdited,
getCurrentCharacter,
getCurrentGroupCharacters,
getCurrentPersona,
getMessageAuthor,
isInAnyChat,
isInCharacterChat,
isInGroupChat } from "./st-utils.js";
import { setInputColorPickerComboValue } from "./utils.js";
//#endregion Local imports
const DEFAULT_STATIC_DIALOGUE_COLOR_HEX = "#e18a24";
/** @type {[number, number, number]} */
const DEFAULT_STATIC_DIALOGUE_COLOR_RGB = [225, 138, 36];
/**
* @typedef {ValueOf<typeof ColorizeSourceType>} ColorizeSourceType
* @readonly
*/
export const ColorizeSourceType = {
AVATAR_VIBRANT: "avatar_vibrant",
//AVATAR_DOMINANT: "avatar_dominant",
CHAR_COLOR_OVERRIDE: "char_color_override",
STATIC_COLOR: "static_color",
DISABLED: "disabled"
};
/**
* @typedef {ValueOf<typeof ColorizeTargetType>} ColorizeTargetType
* @readonly
*/
export const ColorizeTargetType = {
QUOTED_TEXT: 1 << 0,
BUBBLES: 1 << 1,
QUOTED_TEXT_AND_BUBBLES: (1 << 0) | (1 << 1)
};
/**
* @typedef {defaultExtSettings} XDCSettings
*/
const defaultCharColorSettings = {
colorizeSource: ColorizeSourceType.AVATAR_VIBRANT,
staticColor: DEFAULT_STATIC_DIALOGUE_COLOR_HEX,
colorOverrides: {},
};
const defaultExtSettings = {
charColorSettings: defaultCharColorSettings,
personaColorSettings: defaultCharColorSettings,
colorizeTargets: ColorizeTargetType.QUOTED_TEXT,
chatBubbleLightness: 0.15,
};
const extName = "SillyTavern-Dialogue-Colorizer";
const extFolderPath = `scripts/extensions/third-party/${extName}`;
const extSettings = initializeSettings(extName, defaultExtSettings);
/** @type {HTMLStyleElement} */
let charactersStyleSheet;
/** @type {HTMLStyleElement} */
let personasStyleSheet;
/**
* @param {STCharacter} stChar
*/
async function getCharStyleString(stChar) {
let styleHtml = "";
if ((extSettings.colorizeTargets & ColorizeTargetType.QUOTED_TEXT) === ColorizeTargetType.QUOTED_TEXT) {
const charDialogueColor = await getCharacterDialogueColor(stChar);
if (charDialogueColor) {
styleHtml += `
.mes[xdc-author_uid="${stChar.uid}"] .mes_text q {
color: #${charDialogueColor.toHex()};
}
`;
}
}
if ((extSettings.colorizeTargets & ColorizeTargetType.BUBBLES) === ColorizeTargetType.BUBBLES) {
const charBubbleColor = await getCharacterBubbleColor(stChar);
if (charBubbleColor) {
styleHtml += `
.bubblechat .mes[xdc-author_uid="${stChar.uid}"] {
background-color: #${charBubbleColor.toHex()} !important;
border-color: #${charBubbleColor.toHex()} !important;
}
`;
}
}
return styleHtml;
}
/**
*
* @param {STCharacter[]=} characterList
*/
async function updateCharactersStyleSheet(characterList) {
if (!characterList) {
if (!isInAnyChat()) {
return;
}
if (isInGroupChat()) {
characterList = getCurrentGroupCharacters();
}
else if (isInCharacterChat()) {
characterList = [getCurrentCharacter()];
}
}
const stylesHtml = await Promise.all(characterList.map(async char => await getCharStyleString(char)));
charactersStyleSheet.innerHTML = stylesHtml.join("");
}
// Handled differently from the chars style sheet so we don't have to do any dirty/complex tricks when a chat has messages
// from a persona the user isn't currently using (otherwise the message color would revert to the default).
/**
*
* @param {STCharacter[]=} personaList
*/
async function updatePersonasStyleSheet(personaList) {
personaList ??= getAllPersonas();
const stylesHtml = await Promise.all(personaList.map(async persona => await getCharStyleString(persona)));
personasStyleSheet.innerHTML = stylesHtml.join("");
}
/**
*
* @param {STCharacter | CharacterType} charType
*/
function getSettingsForChar(charType) {
if (charType instanceof STCharacter) {
charType = charType.type;
}
switch (charType) {
case CharacterType.CHARACTER:
return extSettings.charColorSettings;
case CharacterType.PERSONA:
return extSettings.personaColorSettings;
default:
console.warn(`Character type '${charType}' has no settings key, using defaults.`);
return structuredClone(defaultCharColorSettings);
}
}
/**
*
* @param {import("./ExColor.js").ColorArray} rgb
* @returns {import("./ExColor.js").ColorArray}
*/
function makeBetterContrast(rgb) {
const [h, s, l, a] = ExColor.rgb2hsl(rgb);
// TODO: Very arbitrary and probably doesn't really make sense.
// Change this?
const nHue = h;
const nSat = s > 0.5 ? s - 0.1 : s + 0.3;
const nLum = l > 0.7 ? l : 0.7;
return ExColor.hsl2rgb([nHue, nSat, nLum, a]);
}
/**
*
* @param {HTMLImageElement} image
* @param {...(keyof VibrantSwatches)} swatchKeys
* @returns {Promise<[number, number, number]?>}
*/
async function getVibrantColorRgb(image, ...swatchKeys) {
const vibrant = await getImageVibrant(image);
const swatch = getValidSwatch(vibrant.swatches(), ...swatchKeys);
return swatch?.getRgb();
}
let avatarVibrantColorCache = {};
/**
*
* @param {STCharacter} stChar
* @returns {Promise<ExColor?>}
*/
async function getCharacterDialogueColor(stChar) {
const colorSettings = getSettingsForChar(stChar);
const colorizeSource = Object.keys(colorSettings.colorOverrides).includes(stChar.avatarName)
? ColorizeSourceType.CHAR_COLOR_OVERRIDE
: colorSettings.colorizeSource;
switch (colorizeSource) {
case ColorizeSourceType.AVATAR_VIBRANT: {
if (avatarVibrantColorCache[stChar.uid]) {
return avatarVibrantColorCache[stChar.uid];
}
const avatar = stChar.getAvatarImageThumbnail();
const colorRgb = await getVibrantColorRgb(avatar, "Vibrant", "Muted");
const betterContrastRgb = colorRgb ? makeBetterContrast(colorRgb) : DEFAULT_STATIC_DIALOGUE_COLOR_RGB;
const exColor = ExColor.fromRgb(betterContrastRgb);
avatarVibrantColorCache[stChar.uid] = exColor;
return exColor;
}
case ColorizeSourceType.STATIC_COLOR: {
return ExColor.fromHex(colorSettings.staticColor);
}
case ColorizeSourceType.CHAR_COLOR_OVERRIDE: {
const overrideColor = colorSettings.colorOverrides[stChar.avatarName];
return overrideColor ? ExColor.fromHex(overrideColor) : null;
}
case ColorizeSourceType.DISABLED:
default:
return null;
}
}
/**
*
* @param {STCharacter} stChar
* @returns {Promise<ExColor?>}
*/
async function getCharacterBubbleColor(stChar) {
const dialogueColor = await getCharacterDialogueColor(stChar);
if (!dialogueColor) {
return null;
}
const hsl = dialogueColor.toHsl();
//hsl.s = 0.5;
hsl.l = extSettings.chatBubbleLightness;
return ExColor.fromHsl(hsl);
}
/**
*
* @param {string} textboxValue
* @param {any} defaultValue
* @returns {string | null}
*/
function getTextValidHexOrDefault(textboxValue, defaultValue) {
const trimmed = textboxValue.trim();
if (!ExColor.isValidHexString(trimmed))
return defaultValue;
return ExColor.getHexWithHash(trimmed);
}
/**
*
* @param {HTMLElement} message
*/
function addAuthorUidClassToMessage(message) {
const authorChatUidAttr = "xdc-author_uid";
if (message.hasAttribute(authorChatUidAttr)) {
console.debug(`[XDC] Message already has '${authorChatUidAttr}' attribute, skipping.`);
}
const messageAuthorChar = getMessageAuthor(message);
if (!messageAuthorChar) {
console.error("[XDC] Couldn't get message author character to add class.");
return;
}
message.setAttribute(authorChatUidAttr, messageAuthorChar.uid);
}
//#region Event Handlers
async function onCharacterSettingsUpdated() {
await updateCharactersStyleSheet();
saveSettingsDebounced();
}
async function onPersonaSettingsUpdated() {
await updatePersonasStyleSheet();
saveSettingsDebounced();
}
async function onAnySettingsUpdated() {
await updateCharactersStyleSheet();
await updatePersonasStyleSheet();
saveSettingsDebounced();
}
/**
*
* @param {STCharacter} char
*/
function onCharacterChanged(char) {
const colorOverride = document.getElementById("xdc-char_color_override");
setInputColorPickerComboValue(colorOverride, extSettings.charColorSettings.colorOverrides[char.avatarName]);
}
/**
*
* @param {STCharacter} persona
*/
function onPersonaChanged(persona) {
const colorOverride = document.getElementById("xdc-persona_color_override");
setInputColorPickerComboValue(colorOverride, extSettings.personaColorSettings.colorOverrides[persona.avatarName]);
}
//#endregion Event Handlers
//#region Initialization
function initializeStyleSheets() {
charactersStyleSheet = createAndAppendStyleSheet("xdc-chars_style_sheet");
personasStyleSheet = createAndAppendStyleSheet("xdc-personas_style_sheet");
function createAndAppendStyleSheet(id) {
const styleSheet = document.createElement('style');
styleSheet.id = id;
return document.body.appendChild(styleSheet);
}
}
function initializeSettingsUI() {
const elemExtensionSettings = document.getElementById("xdc-extension-settings");
const elemGlobalDialogueSettings = elemExtensionSettings.querySelector("#xdc-global_dialogue_settings");
const elemColorTargetDropdown = createColorTargetDropdown("xdc-global_colorize_target", (changedEvent) => {
const value = $(changedEvent.target).prop("value");
extSettings.colorizeTargets = value;
onAnySettingsUpdated();
});
elemGlobalDialogueSettings.children[0].insertAdjacentElement("afterend", elemColorTargetDropdown);
$(elemColorTargetDropdown.querySelector('select')).prop("value", extSettings.colorizeTargets);
const elemChatBubbleLightness = elemGlobalDialogueSettings.querySelector("#xdc-chat_bubble_color_lightness");
$(elemChatBubbleLightness)
.prop("value", extSettings.chatBubbleLightness)
.on('focusout', (event) => {
const target = $(event.target);
const value = target.prop("value");
const numValue = parseFloat(value);
if (Number.isNaN(numValue)) {
const lastValidValue = target.prop("lastValidValue") || extSettings.chatBubbleLightness;
target.prop("value", lastValidValue);
return;
}
const resultValue = numValue < 0.0 ? 0.0
: numValue > 1.0 ? 1.0
: numValue;
target.prop("value", resultValue);
extSettings.chatBubbleLightness = resultValue;
onAnySettingsUpdated();
});
const charDialogueSettings = elemExtensionSettings.querySelector("#xdc-char_dialogue_settings");
const charColorSourceDropdown = createColorSourceDropdown("xdc-char_colorize_source", (changedEvent) => {
const value = $(changedEvent.target).prop("value");
extSettings.charColorSettings.colorizeSource = value;
onCharacterSettingsUpdated();
});
const charStaticColorPickerCombo = createColorTextPickerCombo(
(textboxValue) => getTextValidHexOrDefault(textboxValue, null),
(colorValue) => {
extSettings.charColorSettings.staticColor = colorValue;
onCharacterSettingsUpdated();
}
);
charDialogueSettings.children[0].insertAdjacentElement("afterend", charColorSourceDropdown);
charDialogueSettings.children[3].insertAdjacentElement("beforeend", charStaticColorPickerCombo);
$(charColorSourceDropdown.querySelector('select'))
.prop("value", extSettings.charColorSettings.colorizeSource)
.trigger('change');
$(charStaticColorPickerCombo.querySelector('input[type="text"]'))
.prop("value", extSettings.charColorSettings.staticColor)
.trigger('focusout');
const personaDialogueSettings = elemExtensionSettings.querySelector("#xdc-persona_dialogue_settings");
const personaColorSourceDropdown = createColorSourceDropdown("xdc-persona_colorize_source", (changedEvent) => {
const value = $(changedEvent.target).prop("value");
extSettings.personaColorSettings.colorizeSource = value;
onPersonaSettingsUpdated();
});
const personaStaticColorPickerCombo = createColorTextPickerCombo(
(textboxValue) => getTextValidHexOrDefault(textboxValue, null),
(colorValue) => {
extSettings.personaColorSettings.staticColor = colorValue;
onPersonaSettingsUpdated();
}
);
personaDialogueSettings.children[0].insertAdjacentElement("afterend", personaColorSourceDropdown);
personaDialogueSettings.children[3].insertAdjacentElement("beforeend", personaStaticColorPickerCombo);
$(personaColorSourceDropdown.querySelector('select'))
.prop("value", extSettings.personaColorSettings.colorizeSource)
.trigger('change');
$(personaStaticColorPickerCombo.querySelector('input[type="text"]'))
.prop("value", extSettings.personaColorSettings.staticColor)
.trigger('focusout');
}
function initializeCharSpecificUI() {
// Character
const elemCharColorOverride = createColorOverrideElem("xdc-char_color_override", getCharacterBeingEdited);
const elemCharCardForm = document.getElementById("form_create");
const elemAvatarNameBlock = elemCharCardForm.querySelector("div#avatar-and-name-block");
elemAvatarNameBlock.insertAdjacentElement("afterend", elemCharColorOverride);
// Persona
const elemPersonaColorOverride = createColorOverrideElem("xdc-persona_color_override", getCurrentPersona);
elemPersonaColorOverride.removeChild(elemPersonaColorOverride.querySelector("hr.sysHR")); // eh
const elemPersonaDescription = document.getElementById("persona_description");
const elemDescParent = elemPersonaDescription.parentElement;
elemDescParent.insertAdjacentElement("afterbegin", elemPersonaColorOverride);
/**
*
* @param {string} id
* @param {() => STCharacter} stCharGetter
*/
function createColorOverrideElem(id, stCharGetter) {
const label = document.createElement('label');
label.htmlFor = id;
label.title = "The color to use for this character's dialogue (quoted text). Overrides the global setting.";
label.innerHTML = `Dialogue Color<span class="margin5 fa-solid fa-circle-info opacity50p"></span>`;
const hr = document.createElement('hr');
hr.className = "sysHR";
const inputColorPickerCombo = createColorTextPickerCombo(
(textboxValue) => getTextValidHexOrDefault(textboxValue, ""),
(colorValue) => {
const stChar = stCharGetter();
const colorSettings = getSettingsForChar(stChar);
if (colorValue.length > 0)
colorSettings.colorOverrides[stChar.avatarName] = colorValue;
else
delete colorSettings.colorOverrides[stChar.avatarName];
if (stChar.type === CharacterType.PERSONA) {
onPersonaSettingsUpdated();
}
else {
onCharacterSettingsUpdated();
}
}
);
const wrapper = document.createElement('div');
wrapper.id = id;
wrapper.className = "dc-flex-container";
wrapper.appendChild(hr);
wrapper.appendChild(label);
wrapper.appendChild(inputColorPickerCombo);
return wrapper;
}
}
jQuery(async ($) => {
const settingsHtml = await $.get(`${extFolderPath}/dialogue-colorizer.html`);
const elemStExtensionSettings2 = document.getElementById("extensions_settings2");
$(elemStExtensionSettings2).append(settingsHtml);
initializeStyleSheets();
initializeSettingsUI();
initializeCharSpecificUI();
eventSource.on(event_types.CHAT_CHANGED, () => updateCharactersStyleSheet());
expEventSource.on(exp_event_type.MESSAGE_ADDED, addAuthorUidClassToMessage);
expEventSource.on(exp_event_type.CHAR_CARD_CHANGED, onCharacterChanged);
expEventSource.on(exp_event_type.PERSONA_CHANGED, onPersonaChanged);
eventSource.once(event_types.APP_READY, () => {
onPersonaChanged(getCurrentPersona()); // Initialize color inputs with starting values.
updatePersonasStyleSheet();
});
})
//#endregion Initialization