Skip to content

Instantly share code, notes, and snippets.

@sturmenta
Created July 24, 2023 05:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturmenta/1363f0bd32e87acaa4f6e608c2791945 to your computer and use it in GitHub Desktop.
Save sturmenta/1363f0bd32e87acaa4f6e608c2791945 to your computer and use it in GitHub Desktop.
convert decimal percentage on hexadecimal percentage to be used with hex colors
export const getPercentageInHex = (percentage: number): string => {
if (percentage >= 0 && percentage <= 100) {
const preHexNumber = (percentage * 255) / 100;
const hexNumber = preHexNumber.toString(16).split('.')[0].padStart(2, '0').replace('f0', 'ff');
return hexNumber;
}
return 'ff';
};
// usage: '#000000' + getPercentageInHex(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment