Skip to content

Instantly share code, notes, and snippets.

@jeremy-code
Created March 6, 2024 03:30
Show Gist options
  • Save jeremy-code/bb7ebcb973cb39fe286541dfd3b5857a to your computer and use it in GitHub Desktop.
Save jeremy-code/bb7ebcb973cb39fe286541dfd3b5857a to your computer and use it in GitHub Desktop.
colors (RGB, hexToRgb, rgbToHex)
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