Created
March 6, 2024 03:30
-
-
Save jeremy-code/bb7ebcb973cb39fe286541dfd3b5857a to your computer and use it in GitHub Desktop.
colors (RGB, hexToRgb, rgbToHex)
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
type RGB = { | |
r: number; | |
g: number; | |
b: number; | |
}; | |
export const hexToRgb = (hex: string): RGB => { | |
const bigint = parseInt(hex.slice(1), 16); | |
return { | |
r: (bigint >> 16) & 255, | |
g: (bigint >> 8) & 255, | |
b: bigint & 255, | |
}; | |
}; | |
export const rgbToHex = ({ r, g, b }: RGB) => | |
`#${[r, g, b].map((x) => x.toString(16).padStart(2, "0")).join("")}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment