Skip to content

Instantly share code, notes, and snippets.

@fabiofidanza
Created July 7, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiofidanza/d57878787f74b42b60a655edec94c518 to your computer and use it in GitHub Desktop.
Save fabiofidanza/d57878787f74b42b60a655edec94c518 to your computer and use it in GitHub Desktop.
precalculateColorTransition = function(fromHex, toHex, steps=30) {
const fromRGB = [ parseInt(fromHex.substr(0, 2), 16),
parseInt(fromHex.substr(2, 2), 16),
parseInt(fromHex.substr(4, 2), 16)
];
const toRGB = [ parseInt(toHex.substr(0, 2), 16),
parseInt(toHex.substr(2, 2), 16),
parseInt(toHex.substr(4, 2), 16)
];
let transition = [];
let transitionSteps = [ (toRGB[0]-fromRGB[0]) / (steps-1),
(toRGB[1]-fromRGB[1]) / (steps-1),
(toRGB[2]-fromRGB[2]) / (steps-1)
];
console.log(transitionSteps);
for(let i=0; i<steps; i++) {
transition.push(
parseInt(fromRGB[0] + Math.min(255,Math.ceil(transitionSteps[0]*i))).toString(16) +
parseInt(fromRGB[1] + Math.min(255,Math.ceil(transitionSteps[1]*i))).toString(16) +
parseInt(fromRGB[2] + Math.min(255,Math.ceil(transitionSteps[2]*i))).toString(16)
)
}
return transition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment