Skip to content

Commit

Permalink
Graph: color schema nodes and attestation edges by hashed entity IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
darrylyeo committed Apr 19, 2024
1 parent deabfaf commit 943044b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
} = data
// Functions
import { hashStringToColor } from '$/utils/hashStringToColor'
// Internal state
import Graph from 'graphology'
Expand All @@ -36,7 +40,7 @@
x: Math.random() * 100,
y: Math.random() * 100,
size: Math.log(schema.attestationCount) * 5,
color: `rgb(${Math.round(Math.random() * 255)}, ${Math.round(Math.random() * 255)}, ${Math.round(Math.random() * 255)})`,
color: hashStringToColor(id),
})
}
Expand Down Expand Up @@ -80,7 +84,7 @@
x: Math.random() * 100,
y: Math.random() * 100,
size: 10,
color: `rgb(${Math.round(Math.random() * 255)}, ${Math.round(Math.random() * 255)}, ${Math.round(Math.random() * 255)})`,
color: hashStringToColor(id),
})
}
Expand All @@ -91,19 +95,21 @@
for (const [attestationId, attestation] of allAttestations.entries()) {
for (const recipient of attestation.recipients) {
// Attester → Recipient
// {
// const id = `account/${attestation.attester}|account/${recipient}`
// if(!graph.hasEdge(id))
// graph.addEdge(
// `account/${attestation.attester}`,
// `account/${recipient}`,
// {
// id,
// label: attestationId,
// },
// )
// }
{
const id = `account/${attestation.attester}|account/${recipient}`
if(!graph.hasEdge(id))
graph.addEdge(
`account/${attestation.attester}`,
`account/${recipient}`,
{
id,
label: attestationId,
color: hashStringToColor(`schema/${attestation.schemaId}`),
size: 3,
},
)
}
// Attester → Schema
{
Expand Down
14 changes: 14 additions & 0 deletions src/utils/hashStringToColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const hashStringToColor = (str: string) => {
let hash = 0
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash)
}

let color = '#'
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xFF
color += ('00' + value.toString(16)).slice(-2)
}

return color
}

0 comments on commit 943044b

Please sign in to comment.