forked from FoundKeyGang/FoundKey
client: Replace CRC32 with FNV1a in colour hashing
Paste fnv-plus directly to save bundle size
This commit is contained in:
parent
24a10acd34
commit
ba331f771d
3 changed files with 49 additions and 13 deletions
|
@ -40,7 +40,6 @@
|
||||||
"cli-highlight": "2.1.11",
|
"cli-highlight": "2.1.11",
|
||||||
"color-convert": "2.0.1",
|
"color-convert": "2.0.1",
|
||||||
"content-disposition": "0.5.4",
|
"content-disposition": "0.5.4",
|
||||||
"crc": "^4.1.1",
|
|
||||||
"date-fns": "2.28.0",
|
"date-fns": "2.28.0",
|
||||||
"deep-email-validator": "0.1.21",
|
"deep-email-validator": "0.1.21",
|
||||||
"escape-regexp": "0.0.1",
|
"escape-regexp": "0.0.1",
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import crc32 from 'crc/crc32';
|
|
||||||
import { watch } from 'vue';
|
import { watch } from 'vue';
|
||||||
import * as foundkey from 'foundkey-js';
|
import * as foundkey from 'foundkey-js';
|
||||||
import { getStaticImageUrl } from '@/scripts/get-static-image-url';
|
import { getStaticImageUrl } from '@/scripts/get-static-image-url';
|
||||||
|
@ -44,8 +43,56 @@ const url = $computed(() => defaultStore.state.disableShowingAnimatedImages
|
||||||
// https://www.nature.com/articles/nmeth.1618
|
// https://www.nature.com/articles/nmeth.1618
|
||||||
const hashlette = ['rgb(0, 0, 0)', 'rgb(230, 159, 0)', 'rgb(86, 180, 233)', 'rgb(0, 158, 115)', 'rgb(240, 228, 66)', 'rgb(0, 114, 178)', 'rgb(213, 94, 0)', 'rgb(204, 121, 167)'] as const;
|
const hashlette = ['rgb(0, 0, 0)', 'rgb(230, 159, 0)', 'rgb(86, 180, 233)', 'rgb(0, 158, 115)', 'rgb(240, 228, 66)', 'rgb(0, 114, 178)', 'rgb(213, 94, 0)', 'rgb(204, 121, 167)'] as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FNV-1a Hash implementation (32, 64, 128, 256, 512, and 1024 bit)
|
||||||
|
*
|
||||||
|
* stolen under the terms of MIT license which i actually had to make up myself cause all i had was the readme
|
||||||
|
*
|
||||||
|
* Copyright © 2017 Travis Webb and desudesutalk
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author Travis Webb <me@traviswebb.com>
|
||||||
|
* @see http://tools.ietf.org/html/draft-eastlake-fnv-06
|
||||||
|
*/
|
||||||
|
function fnv1a32(str: string): number {
|
||||||
|
let i: number, l = str.length - 3, t0 = 0, v0 = 0x9dc5, t1 = 0, v1 = 0x811c;
|
||||||
|
|
||||||
|
for (i = 0; i < l;) {
|
||||||
|
v0 ^= str.charCodeAt(i++);
|
||||||
|
t0 = v0 * 403; t1 = v1 * 403;
|
||||||
|
t1 += v0 << 8;
|
||||||
|
v1 = (t1 + (t0 >>> 16)) & 65535; v0 = t0 & 65535;
|
||||||
|
v0 ^= str.charCodeAt(i++);
|
||||||
|
t0 = v0 * 403; t1 = v1 * 403;
|
||||||
|
t1 += v0 << 8;
|
||||||
|
v1 = (t1 + (t0 >>> 16)) & 65535; v0 = t0 & 65535;
|
||||||
|
v0 ^= str.charCodeAt(i++);
|
||||||
|
t0 = v0 * 403; t1 = v1 * 403;
|
||||||
|
t1 += v0 << 8;
|
||||||
|
v1 = (t1 + (t0 >>> 16)) & 65535; v0 = t0 & 65535;
|
||||||
|
v0 ^= str.charCodeAt(i++);
|
||||||
|
t0 = v0 * 403; t1 = v1 * 403;
|
||||||
|
t1 += v0 << 8;
|
||||||
|
v1 = (t1 + (t0 >>> 16)) & 65535; v0 = t0 & 65535;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < l + 3) {
|
||||||
|
v0 ^= str.charCodeAt(i++);
|
||||||
|
t0 = v0 * 403; t1 = v1 * 403;
|
||||||
|
t1 += v0 << 8;
|
||||||
|
v1 = (t1 + (t0 >>> 16)) & 65535; v0 = t0 & 65535;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((v1 << 16) >>> 0) + v0;
|
||||||
|
}
|
||||||
|
|
||||||
const handleHash = $computed(() => {
|
const handleHash = $computed(() => {
|
||||||
const stripColours = crc32(acct(props.user)).toString(8).split('').map(digitStr => hashlette[parseInt(digitStr)]);
|
const stripColours = fnv1a32(acct(props.user)).toString(8).split('').map(digitStr => hashlette[parseInt(digitStr)]);
|
||||||
const fraction = 1 / stripColours.length;
|
const fraction = 1 / stripColours.length;
|
||||||
const positions = stripColours.flatMap((colour, idx) =>
|
const positions = stripColours.flatMap((colour, idx) =>
|
||||||
[`${colour} ${fraction * idx}turn`, `${colour} ${fraction * (idx + 1)}turn`]
|
[`${colour} ${fraction * idx}turn`, `${colour} ${fraction * (idx + 1)}turn`]
|
||||||
|
|
10
yarn.lock
10
yarn.lock
|
@ -3649,7 +3649,6 @@ __metadata:
|
||||||
cli-highlight: 2.1.11
|
cli-highlight: 2.1.11
|
||||||
color-convert: 2.0.1
|
color-convert: 2.0.1
|
||||||
content-disposition: 0.5.4
|
content-disposition: 0.5.4
|
||||||
crc: ^4.1.1
|
|
||||||
cross-env: 7.0.3
|
cross-env: 7.0.3
|
||||||
date-fns: 2.28.0
|
date-fns: 2.28.0
|
||||||
deep-email-validator: 0.1.21
|
deep-email-validator: 0.1.21
|
||||||
|
@ -5161,15 +5160,6 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"crc@npm:^4.1.1":
|
|
||||||
version: 4.1.1
|
|
||||||
resolution: "crc@npm:4.1.1"
|
|
||||||
peerDependencies:
|
|
||||||
buffer: ">=6.0.3"
|
|
||||||
checksum: b2ab89547bd47ac8bdaadcd362d94aa9ddda2ddfe7267e0b449f62a83739bd8f99e9874e4933652d5653ab5f9703eebba9665d03206d600cef2ded9ff3c5c1e2
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"create-require@npm:^1.1.0":
|
"create-require@npm:^1.1.0":
|
||||||
version: 1.1.1
|
version: 1.1.1
|
||||||
resolution: "create-require@npm:1.1.1"
|
resolution: "create-require@npm:1.1.1"
|
||||||
|
|
Loading…
Reference in a new issue