Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate colors from a mandr path rather than randomly #105

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: "3.12"
- name: Set up Node 20
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
19 changes: 12 additions & 7 deletions frontend/src/components/FileTreeItem.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<script setup lang="ts">
import { computed } from "vue";
import { computed, onBeforeMount, ref } from "vue";
import { useRouter } from "vue-router";
import { type FileTreeNode } from "./FileTree.vue";

import { type FileTreeNode } from "@/components/FileTree.vue";
import { remap, sha1 } from "@/services/utils";

const router = useRouter();
const props = defineProps<FileTreeNode>();

const randomColor = ref("");
const hasChildren = computed(() => props.children?.length);
const label = computed(() => {
const segment = props.uri.split("/");
return segment[segment.length - 1];
});

const randomColor = computed(() => {
const hue = Math.random() * 360;
return `background-color: hsl(${hue}deg 97 75);`;
});

function onClick() {
router.push({
name: "dashboard",
Expand All @@ -25,6 +23,13 @@ function onClick() {
},
});
}

onBeforeMount(async () => {
const hash = await sha1(props.uri);
const hashAsNumber = Number(`0x${hash.slice(0, 7)}`);
const hue = remap(hashAsNumber, 0x0000000, 0xfffffff, 0, 360);
randomColor.value = `background-color: hsl(${hue}deg 97 75);`;
});
</script>

<template>
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ export function sleep(delay: number): Promise<void> {
export function isString(s: any) {
return typeof s === "string" || s instanceof String;
}

export function remap(
x: number,
fromMin: number,
fromMax: number,
toMin: number,
toMax: number
): number {
return ((x - fromMin) * (toMax - toMin)) / (fromMax - fromMin) + toMin;
}

export async function sha1(message: string) {
const msgUint8 = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-1", msgUint8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}
Loading