Created
November 15, 2022 09:44
-
-
Save seognil/be58aecb4d760cb1f5a56be0baec0382 to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env node | |
// * ================================================================================ lolcat | |
/** | |
* hsl color space | |
* https://stackoverflow.com/questions/36721830/convert-hsl-to-rgb-and-hex#answer-54014428 | |
*/ | |
const hsl2rgb = (h, s = 1, l = 0.5) => { | |
const a = s * Math.min(l, 1 - l); | |
const f = (n, k = (n + h / 30) % 12) => Math.round((l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1)) * 255); | |
return [f(0), f(8), f(4)]; | |
}; | |
// * ---------------- | |
/** | |
* smoother color space | |
* https://github.com/jaseg/lolcat/blob/main/lolcat.c | |
*/ | |
const circle2rgb = (hue) => { | |
/** | |
* saturation | |
* 0: white | |
* 1: colorful | |
*/ | |
const s = 1; | |
const r = Math.round((1 - s + s * (0.5 + 0.5 * Math.sin(((hue + 0) / 180) * Math.PI))) * 255); | |
const g = Math.round((1 - s + s * (0.5 + 0.5 * Math.sin(((hue + 120) / 180) * Math.PI))) * 255); | |
const b = Math.round((1 - s + s * (0.5 + 0.5 * Math.sin(((hue + 240) / 180) * Math.PI))) * 255); | |
return [r, g, b]; | |
}; | |
// * ---------------- ansi escape color | |
// * https://en.wikipedia.org/wiki/ANSI_escape_code#Colors | |
const ansicolor = (hue, [r, g, b] = circle2rgb(hue)) => `\x1b[38;2;${r};${g};${b}m`; | |
const resetcolor = "\x1b[0m"; | |
// * ---------------- lolcat | |
const lolcat = (str, h = 0.2, v = 0.1) => { | |
const offset = 0 + Math.random() * 360; | |
const direction = Math.random() < 0.5 ? -1 : 1; | |
str.split("\n").forEach((line, l) => { | |
line.split("").forEach((char, i) => { | |
const hue = ((i * h) / 5.0 + l * v) * 60 * direction + offset; | |
process.stdout.write(ansicolor(hue) + char + ""); | |
}); | |
process.stdout.write("\n"); | |
}); | |
process.stdout.write(resetcolor); | |
}; | |
// * ================================================================================ | |
const theWelcome = String.raw` | |
_____ _ _ _ _____ | |
| __ \| | | | /\ | | / ____| | |
| |__) | | | | / \ | | | | | |
| _ /| | | |/ /\ \ | | | | | |
| | \ \| |__| / ____ \| |___| |____ | |
|_| \_\\____/_/ \_\______\_____| | |
`; | |
// * ---------------------------------------------------------------- | |
const height = theWelcome.split("\n").length; | |
const width = Math.max(...theWelcome.split("\n").map((e) => e.length)); | |
const len = Math.sqrt(height ** 2 * 4 + width ** 2); | |
const ratio = height / width; | |
lolcat(theWelcome, 2 / len, 0.2 * ratio); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment