Skip to content

Instantly share code, notes, and snippets.

@DeadlySurgeon
Created February 3, 2024 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeadlySurgeon/a129f7512351a31c7605d71ecfa79e4b to your computer and use it in GitHub Desktop.
Save DeadlySurgeon/a129f7512351a31c7605d71ecfa79e4b to your computer and use it in GitHub Desktop.
Just a colorful square.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"time"
"github.com/lucasb-eyer/go-colorful"
)
var rampColorA, _ = colorful.Hex("#5A56E0")
var rampColorB, _ = colorful.Hex("#E0565A") //
func main() {
fmt.Println()
fmt.Print("\033[?25l")
defer func() { fmt.Println("\033[?25h") }()
defer fmt.Print(resetEscape)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Kill, os.Interrupt)
go func() {
<-c
cancel()
}()
Decode5A3Image(ctx, 16, 16)
down(30)
}
// Decode5A3Image takes in a 5A3 encoded image and returns an image.Image
func Decode5A3Image(ctx context.Context, height, width int) {
var pixels = make([][]int, height)
for i := 0; i < height; i++ {
pixels[i] = make([]int, width)
}
for hb := 0; hb < height; hb += 4 {
for wb := 0; wb < width; wb += 4 {
for h := 0; h < 4; h++ {
for w := 0; w < 4; w++ {
pixels[hb+h][wb+w] = 1
printP(pixels)
if sleepOrExit(ctx, 100*time.Millisecond) {
return
}
}
}
}
}
}
func sleepOrExit(ctx context.Context, d time.Duration) bool {
select {
case <-time.After(d):
return false
case <-ctx.Done():
return true
}
}
func printP(pg [][]int) {
fmt.Println(black + strings.Repeat("█", len(pg)*2+4))
for p1, pg1 := range pg {
fmt.Print(black + "██")
for p2, pgg := range pg1 {
fmt.Print(getTermCode(float64(p1+p2)/float64(len(pg)+len(pg1))) + pggToS(pgg))
}
fmt.Println(black + "██")
}
fmt.Println(strings.Repeat("█", len(pg)*2+4))
up(len(pg) + 2)
}
func pggToS(i int) string {
if i == 1 {
return "██"
}
return " "
}
var black = "\x1b[38;2;0;0;0m"
var resetEscape = "\x1b[0m"
const (
usefulUp = "\033[1A"
usefulDown = "\033[1B"
)
func upC(n int) string { return fmt.Sprintf("\033[%dA", n) }
func up(n int) { fmt.Print(upC(n)) }
func downC(n int) string { return fmt.Sprintf("\033[%dB", n) }
func down(n int) { fmt.Print(downC(n)) }
// getTermCode returns the ANSI escape sequence to set the color based on the given hex code
func getTermCode(percent float64) string {
hexCode := rampColorA.BlendLuv(rampColorB, percent).Hex()
var r, g, b int
if len(hexCode) == 7 {
fmt.Sscanf(hexCode, "#%2x%2x%2x", &r, &g, &b)
}
colorType := 38 // Foreground color
return fmt.Sprintf("\x1b[%d;2;%d;%d;%dm", colorType, r, g, b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment