Skip to content
Open
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
23 changes: 19 additions & 4 deletions lib/utils/getColorFromString.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
export const getColorFromString = (string: string, alpha = 1) => {
// pseudo random number from string
const hash = string.split("").reduce((acc, char) => {
return acc * 31 + char.charCodeAt(0)
}, 0)
return `hsl(${hash % 360}, 100%, 50%, ${alpha})`
//
// `acc * 31 + code` grows without bound: it leaves exact-integer range at
// 11 characters and overflows to Infinity past ~200, at which point
// `Infinity % 360` is NaN and the result is an unparseable `hsl(NaN, ...)`.
// Net ids here are selector paths such as
// `group > capacitor.C101 > port.pin1 to U100.VOUT`, so deeply nested
// groups reach that length in practice.
//
// Keeping the accumulator in signed 32-bit range via `| 0` is the standard
// string-hash formulation and stays finite and exact for any input.
let hash = 0
for (let i = 0; i < string.length; i++) {
hash = (hash * 31 + string.charCodeAt(i)) | 0
}

// `%` keeps the sign of the dividend, so normalise into [0, 360).
const hue = ((hash % 360) + 360) % 360

return `hsl(${hue}, 100%, 50%, ${alpha})`
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading