Replies: 2 comments
-
|
Howdy! Here's the code used in the MeshCore App (dart/flutter). static Color getColourForName(String name, {
int saturation = 60,
int lightness = 50,
}) {
var hash = HashUtil.fnv1aHash(name);
return HSLColor.fromAHSL(1.0, hash % 360, saturation / 100, lightness / 100).toColor();
}class HashUtil {
// we are using _mul32 and _xor32 to ensure web/js returns the same value as mobile
static int fnv1aHash(String str) {
int hash = 0x811c9dc5; // FNV1_32_INIT
for(int i = 0; i < str.length; i++){
hash = _xor32(hash, str.codeUnitAt(i));
hash = _mul32(hash, 0x01000193);
}
return hash & 0xFFFFFFFF; // UINT32_MASK
}
// simulate 32-bit multiplication with overflow
static int _mul32(int a, int b) {
return (Int32(a) * Int32(b)).toInt();
}
// simulate 32-bit xor with overflow
static int _xor32(int a, int b) {
return (Int32(a) ^ Int32(b)).toInt();
}
}Here's the code used on the web map. (Javascript) export const byteToHex = new Array(256).fill(0).map((_, i) => i.toString(16).padStart(2, '0'));
function fnv1aHash(str) {
let hash = 0x811c9dc5n;
for (let i = 0; i < str.length; i++) {
hash = BigInt.asIntN(32, hash ^ BigInt(str.charCodeAt(i)));
hash = BigInt.asIntN(32, hash * 0x01000193n);
}
return Number(hash & 0xFFFFFFFFn);
}
export function getColourForName(name, saturation = 60, lightness = 50) {
const hash = fnv1aHash(name);
return `hsl(${hash % 360}deg, ${saturation}%, ${lightness}%)`;
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Hey, thank you. That's quite some clever code. And you derived it via HSL conversion - I should have thought of that :) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Can someone give me a hint on how the color of a node name is derived in the (android) app? I tried 'first three bytes' and 'last three bytes' of the name, but that are different colors and, often times, not as distinct as the formula the the app seems to use.
Beta Was this translation helpful? Give feedback.
All reactions