Skip to content

Instantly share code, notes, and snippets.

@huozhi
Created October 23, 2023 00:47
Show Gist options
  • Save huozhi/ad69e2f68d8e1d143e1324e846707223 to your computer and use it in GitHub Desktop.
Save huozhi/ad69e2f68d8e1d143e1324e846707223 to your computer and use it in GitHub Desktop.
create linear gradient color
const linearGradientText = (
text: string,
startColor: number[],
endColor: number[]
) => {
let gradient = ''
for (let i = 0; i < text.length; i++) {
const r = Math.floor(
startColor[0] + (i / (text.length - 1)) * (endColor[0] - startColor[0])
)
const g = Math.floor(
startColor[1] + (i / (text.length - 1)) * (endColor[1] - startColor[1])
)
const b = Math.floor(
startColor[2] + (i / (text.length - 1)) * (endColor[2] - startColor[2])
)
const color = `\x1b[38;2;${r};${g};${b}m` // ANSI escape code for setting text color
const resetChar = '\x1b[0m' // ANSI escape code to reset text color
gradient += color + text[i] + resetChar
}
return gradient
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment