mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-15 06:33:55 +01:00
Merge pull request #3973 from overleaf/as-clean-up-hue-generator
Clean up hue generator GitOrigin-RevId: 1b7fad05d7c2f6519efbbe0379f2449c273e4f4b
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import md5 from 'crypto-js/md5'
|
||||
|
||||
const ANONYMOUS_HUE = 100
|
||||
const OWN_HUE = 200 // We will always appear as this color to ourselves
|
||||
const OWN_HUE_BLOCKED_SIZE = 20 // no other user should have a HUE in this range
|
||||
const TOTAL_HUES = 360 // actually 361, but 360 for legacy reasons
|
||||
|
||||
export function getHueForUserId(userId, currentUserId) {
|
||||
if (userId == null || userId === 'anonymous-user') {
|
||||
return ANONYMOUS_HUE
|
||||
}
|
||||
|
||||
if (currentUserId === userId) {
|
||||
return OWN_HUE
|
||||
}
|
||||
|
||||
let hue = getHueForId(userId)
|
||||
|
||||
// if `hue` is within `OWN_HUE_BLOCKED_SIZE` degrees of the personal hue
|
||||
// (`OWN_HUE`), shift `hue` to the end of available hues by adding
|
||||
if (
|
||||
hue > OWN_HUE - OWN_HUE_BLOCKED_SIZE &&
|
||||
hue < OWN_HUE + OWN_HUE_BLOCKED_SIZE
|
||||
) {
|
||||
hue = hue - OWN_HUE // `hue` now at 0 +/- `OWN_HUE_BLOCKED_SIZE`
|
||||
hue = hue + TOTAL_HUES - OWN_HUE_BLOCKED_SIZE
|
||||
}
|
||||
|
||||
return hue
|
||||
}
|
||||
|
||||
function getHueForId(id) {
|
||||
const hash = md5(id)
|
||||
const hue =
|
||||
parseInt(hash.toString().slice(0, 8), 16) %
|
||||
(TOTAL_HUES - OWN_HUE_BLOCKED_SIZE * 2)
|
||||
return hue
|
||||
}
|
||||
Reference in New Issue
Block a user