Created
July 24, 2023 05:05
-
-
Save sturmenta/1363f0bd32e87acaa4f6e608c2791945 to your computer and use it in GitHub Desktop.
convert decimal percentage on hexadecimal percentage to be used with hex colors
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
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