Created
August 20, 2024 08:29
-
-
Save axelrindle/97ee1757705628e181b515210c62036c to your computer and use it in GitHub Desktop.
Hexadecimal RGB Opacity
This file contains hidden or 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
import { log } from 'console' | |
type Row = { | |
opacity: string | |
hex: string | |
} | |
const rows: Row[] = [] | |
function dec2hex(dec: number) { | |
let raw = dec.toString(16) | |
const removeAfter = raw.indexOf('.') | |
if (removeAfter > -1) { | |
raw = raw.substring(0, removeAfter) | |
} | |
if (raw.length % 2 !== 0) { | |
return '0' + raw | |
} | |
return raw | |
} | |
for (let i = 0; i <= 100; i++) { | |
const step = 255 * (i / 100) | |
rows.push({ | |
opacity: `${i}%`, | |
hex: dec2hex(step), | |
}) | |
} | |
log('| Opacity | 2 digit hex code |') | |
log('| --- | --- |') | |
for (const row of rows) { | |
log(`| ${row.opacity} | ${row.hex} |`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment