Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Last active September 22, 2020 12:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleshevlin/ce7e1407ed3ce9f671f283adca64378a to your computer and use it in GitHub Desktop.
Save kyleshevlin/ce7e1407ed3ce9f671f283adca64378a to your computer and use it in GitHub Desktop.
Recolor your GH contribution graph
// Run this in your console with whatever `newColors` you choose.
function recolorContributionGraph(newColors) {
const days = [...document.querySelectorAll('.day')]
const legendItems = [...document.querySelectorAll('.legend > li')]
const colors = [
...days.reduce((acc, day) => {
acc.add(day.getAttribute('fill'))
return acc
}, new Set()),
]
days.forEach((day) => {
const colorIndex = colors.findIndex(
(color) => color === day.getAttribute('fill')
)
const newColor = newColors[colorIndex]
if (newColor) {
day.setAttribute('fill', newColor)
}
})
legendItems.forEach((item) => {
const bgColor = parseRGBToHex(item.style.backgroundColor)
const colorIndex = colors.findIndex((color) => color === bgColor)
const newColor = newColors[colorIndex]
if (newColor) {
item.style.backgroundColor = newColor
}
})
}
function parseRGBToHex(rgbString) {
const [r, g, b] = rgbString
.replace('rgb(', '')
.replace(')', '')
.split(',')
.map((x) => parseInt(x, 10))
return `#${toHex(r)}${toHex(g)}${toHex(b)}`
}
function toHex(number) {
return number.toString(16).padStart(2, '0')
}
const newColors = ['#FFFFFB', '#B8B8D1', '#FF6B6C', '#5B5F97', '#FFC145']
recolorContributionGraph(newColors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment