Last active
September 22, 2020 12:39
-
-
Save kyleshevlin/ce7e1407ed3ce9f671f283adca64378a to your computer and use it in GitHub Desktop.
Recolor your GH contribution graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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