Skip to content

Commit

Permalink
use raw bitwise instead of buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendicated committed Aug 2, 2024
1 parent 2b1c010 commit 7cdf346
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/plugins/ircColors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";

// Compute a 64-bit FNV-1a hash of the passed data
function hash(data: ArrayBuffer) {
function hash(id: bigint) {
const fnvPrime = 1099511628211n;
const offsetBasis = 14695981039346656037n;

let result = offsetBasis;
for (const byte of new Uint8Array(data)) {
result ^= BigInt(byte);
result = (result * fnvPrime) % 2n**32n;
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
function calculateNameColorForUser(id: bigint) {
const idBuffer = new ArrayBuffer(8);
{
const idView = new DataView(idBuffer);
idView.setBigUint64(0, id);
}
const idHash = hash(idBuffer);
const idHash = hash(id);

return `hsl(${idHash % 360n}, 100%, ${settings.store.lightness}%)`;
}
Expand Down

0 comments on commit 7cdf346

Please sign in to comment.