Add: Generated Avatar colors
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import seedrandom from "seedrandom";
|
||||
|
||||
/**
|
||||
* @typedef {{r: number, g: number, b: number}} rgb
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {seedrandom.PRNG} random
|
||||
* @returns {rgb}
|
||||
*/
|
||||
function createColor(random) {
|
||||
const r = random.quick();
|
||||
const g = random.quick();
|
||||
const b = random.quick();
|
||||
const normal = Math.pow(r, 2) + Math.pow(g, 2) + Math.pow(b, 2);
|
||||
return {
|
||||
r: Math.round(r / normal * 255),
|
||||
g: Math.round(g / normal * 255),
|
||||
b: Math.round(b / normal * 255)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {rgb} a
|
||||
* @param {rgb} b
|
||||
* @returns {rgb}
|
||||
*/
|
||||
function mixColors(a, b) {
|
||||
return {
|
||||
r: Math.round(a.r * b.r / 255),
|
||||
g: Math.round(a.g * b.g / 255),
|
||||
b: Math.round(a.b * b.b / 255),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} seed
|
||||
* @returns {{
|
||||
* c1: rgb,
|
||||
* c2: rgb,
|
||||
* c3: rgb,
|
||||
* c4: rgb,
|
||||
* c13: rgb,
|
||||
* c14: rgb,
|
||||
* c23: rgb,
|
||||
* c24: rgb
|
||||
* }}
|
||||
*/
|
||||
export function generateQuadColors(seed) {
|
||||
const r = seedrandom(seed);
|
||||
|
||||
const c1 = createColor(r);
|
||||
const c2 = createColor(r);
|
||||
const c3 = createColor(r);
|
||||
const c4 = createColor(r);
|
||||
|
||||
return {
|
||||
c1: c1,
|
||||
c2: c2,
|
||||
c3: c3,
|
||||
c4: c4,
|
||||
c13: mixColors(c1, c3),
|
||||
c14: mixColors(c1, c4),
|
||||
c23: mixColors(c2, c3),
|
||||
c24: mixColors(c2, c4),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* https://stackoverflow.com/a/8076436
|
||||
* @param {string} string
|
||||
* @returns {number}
|
||||
*/
|
||||
export function hashCode(string) {
|
||||
var hash = 0;
|
||||
for (var i = 0; i < string.length; i++) {
|
||||
var code = string.charCodeAt(i);
|
||||
hash = ((hash<<5)-hash)+code;
|
||||
hash = hash & hash; // Convert to 32bit integer
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} any
|
||||
* @returns {number}
|
||||
*/
|
||||
export function hashOf(any) {
|
||||
return hashCode(String(any));
|
||||
}
|
||||
Reference in New Issue
Block a user