IrcColors: Make lightness apply without restart

This commit is contained in:
Nuckyz 2025-01-27 14:30:11 -03:00
parent 3350922c09
commit c4f8221f75
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9

View file

@ -17,33 +17,22 @@
*/ */
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import { hash as h64 } from "@intrnl/xxhash64";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
import { useMemo } from "@webpack/common";
// Compute a 64-bit FNV-1a hash of the passed data
function hash(id: bigint) {
const fnvPrime = 1099511628211n;
const offsetBasis = 14695981039346656037n;
let result = offsetBasis;
for (let i = 7n; i >= 0n; i--) {
result ^= (id >> (8n * i)) & 0xffn;
result = (result * fnvPrime) % 2n ** 32n;
}
return result;
}
// Calculate a CSS color string based on the user ID // Calculate a CSS color string based on the user ID
function calculateNameColorForUser(id: bigint) { function calculateNameColorForUser(id: string) {
const idHash = hash(id); const { lightness } = settings.use(["lightness"]);
const idHash = useMemo(() => h64(id), [id]);
return `hsl(${idHash % 360n}, 100%, ${settings.store.lightness}%)`; return `hsl(${idHash % 360n}, 100%, ${lightness}%)`;
} }
const settings = definePluginSettings({ const settings = definePluginSettings({
lightness: { lightness: {
description: "Lightness, in %. Change if the colors are too light or too dark. Reopen the chat to apply.", description: "Lightness, in %. Change if the colors are too light or too dark",
type: OptionType.NUMBER, type: OptionType.NUMBER,
default: 70, default: 70,
}, },
@ -51,44 +40,46 @@ const settings = definePluginSettings({
description: "Replace role colors in the member list", description: "Replace role colors in the member list",
restartNeeded: true, restartNeeded: true,
type: OptionType.BOOLEAN, type: OptionType.BOOLEAN,
default: true, default: true
}, }
}); });
export default definePlugin({ export default definePlugin({
name: "IrcColors", name: "IrcColors",
description: "Makes username colors in chat unique, like in IRC clients", description: "Makes username colors in chat unique, like in IRC clients",
authors: [Devs.Grzesiek11], authors: [Devs.Grzesiek11],
settings,
patches: [ patches: [
{ {
find: '="SYSTEM_TAG"', find: '="SYSTEM_TAG"',
replacement: { replacement: {
match: /(?<=className:\i\.username,style:.{0,50}:void 0,)/, match: /(?<=className:\i\.username,style:.{0,50}:void 0,)/,
replace: "style:{color:$self.calculateNameColorForMessageContext(arguments[0])},", replace: "style:{color:$self.calculateNameColorForMessageContext(arguments[0])},"
}, }
}, },
{ {
find: ".NameWithRole,{roleName:", find: "#{intl::GUILD_OWNER}),children:",
replacement: { replacement: {
match: /(?<=color:)null!=.{0,50}?(?=,)/, match: /(?<=\.MEMBER_LIST}\),\[\]\),)(.+?color:)null!=.{0,50}?(?=,)/,
replace: "$self.calculateNameColorForListContext(arguments[0])", replace: (_, rest) => `ircColor=$self.calculateNameColorForListContext(arguments[0]),${rest}ircColor`
}, },
predicate: () => settings.store.memberListColors, predicate: () => settings.store.memberListColors
}, }
], ],
settings,
calculateNameColorForMessageContext(context: any) { calculateNameColorForMessageContext(context: any) {
const id = context?.message?.author?.id; const id = context?.message?.author?.id;
if (id == null) { if (id == null) {
return null; return null;
} }
return calculateNameColorForUser(BigInt(id)); return calculateNameColorForUser(id);
}, },
calculateNameColorForListContext(context: any) { calculateNameColorForListContext(context: any) {
const id = context?.user?.id; const id = context?.user?.id;
if (id == null) { if (id == null) {
return null; return null;
} }
return calculateNameColorForUser(BigInt(id)); return calculateNameColorForUser(id);
}, }
}); });