Skip to content

Instantly share code, notes, and snippets.

@axelrindle
Created August 20, 2024 08:29
Show Gist options
  • Save axelrindle/97ee1757705628e181b515210c62036c to your computer and use it in GitHub Desktop.
Save axelrindle/97ee1757705628e181b515210c62036c to your computer and use it in GitHub Desktop.
Hexadecimal RGB Opacity
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