Skip to content

Instantly share code, notes, and snippets.

@ctrlaltdev
Forked from mlocati/color-scale.js
Last active May 25, 2023 06:38
Show Gist options
  • Save ctrlaltdev/866c08204cb53655d715d194b1d68193 to your computer and use it in GitHub Desktop.
Save ctrlaltdev/866c08204cb53655d715d194b1d68193 to your computer and use it in GitHub Desktop.
Javascript color scale from 0% to 100%, rendering it from red to yellow to green to blue to magenta
function colorScale(int) {
let r, g, b = 0
if (int >= 0 && int <= 20) {
r = 255
g = Math.round(12.75 * int)
b = 0
} else if (int > 20 && int <= 40) {
r = Math.round(-12.75 * int + 510)
g = 255
b = 0
} else if (int > 40 && int <= 60) {
r = 0
g = 255
b = Math.round(12.75 * int) - 510
} else if (int > 60 && int <= 80) {
r = 0
g = Math.round(-12.75 * int + 1020)
b = 255
} else {
r = Math.round(12.75 * int - 1020)
g = 0
b = 255
}
const h = r * 0x10000 + g * 0x100 + b * 0x1
return '#' + ('000000' + h.toString(16)).slice(-6)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment