From 7ddb889195aa14f61ab64aa2a253790c998fe308 Mon Sep 17 00:00:00 2001 From: Grzesiek11 Date: Mon, 24 Jun 2024 05:52:18 +0200 Subject: [PATCH] Move functions outside plugin definition They don't need to be there if they aren't used by patches. --- src/plugins/ircColors/index.ts | 53 +++++++++++++++++----------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/plugins/ircColors/index.ts b/src/plugins/ircColors/index.ts index 40693072f..75566e014 100644 --- a/src/plugins/ircColors/index.ts +++ b/src/plugins/ircColors/index.ts @@ -20,6 +20,32 @@ import { Devs } from "@utils/constants"; import { definePluginSettings } from "@api/Settings"; import definePlugin, { OptionType } from "@utils/types"; +// Compute a 64-bit FNV-1a hash of the passed data +function hash(data: ArrayBuffer) { + const fnvPrime = 1099511628211n; + const offsetBasis = 14695981039346656037n; + + let result = offsetBasis; + for (const byte of new Uint8Array(data)) { + result ^= BigInt(byte); + result = (result * fnvPrime) % 2n**32n; + } + + return result; +} + +// Calculate a CSS color string based on the user ID +function calculateNameColorForUser(id: bigint) { + const idBuffer = new ArrayBuffer(16); + { + const idView = new DataView(idBuffer); + idView.setBigUint64(0, id); + } + const idHash = hash(idBuffer); + + return `hsl(${idHash % 360n}, 100%, ${settings.store.lightness}%)`; +} + const settings = definePluginSettings({ lightness: { description: "Lightness, in %. Change if the colors are too light or too dark.", @@ -43,32 +69,7 @@ export default definePlugin({ }, ], settings, - // Calculate a CSS color string based on the user ID calculateNameColorForContext(context: any) { - return this.calculateNameColorForUser(BigInt(context.message.author.id)); - }, - calculateNameColorForUser(id: bigint) { - // Compute a 64-bit FNV-1a hash of the passed data - function hash(data: ArrayBuffer) { - const fnvPrime = 1099511628211n; - const offsetBasis = 14695981039346656037n; - - let result = offsetBasis; - for (const byte of new Uint8Array(data)) { - result ^= BigInt(byte); - result = (result * fnvPrime) % 2n**32n; - } - - return result; - } - - const idBuffer = new ArrayBuffer(16); - { - const idView = new DataView(idBuffer); - idView.setBigUint64(0, id); - } - const idHash = hash(idBuffer); - - return `hsl(${idHash % 360n}, 100%, ${settings.store.lightness}%)`; + return calculateNameColorForUser(BigInt(context.message.author.id)); }, });