Skip to content

Instantly share code, notes, and snippets.

@arouene
Created October 1, 2018 09:46
Show Gist options
  • Save arouene/07403e1c7f13a760fd930079ca53388c to your computer and use it in GitHub Desktop.
Save arouene/07403e1c7f13a760fd930079ca53388c to your computer and use it in GitHub Desktop.
Use ANSI code to test color capabilites of a terminal
// Test terminals color capabilities
package main
import "fmt"
func main() {
const (
normal = "\033[0m"
color = "\033[%d;%dm"
fgcolor2 = "\033[38;5;%dm"
bgcolor2 = "\033[48;5;%dm"
fgcolor24 = "\033[38;2;%d;%d;%dm"
bgcolor24 = "\033[48;2;%d;%d;%dm"
clear = "\033[2J"
pos = "\033[%d;%dH"
)
// 8 bits
// 8 Normal Colors
// 0-7
fmt.Println("8 Normal colors")
for i := 0; i < 8; i++ {
fmt.Printf(color+" %d ", 0, 40+i, i)
}
fmt.Println(normal + "\n")
// 8 Bright Colors
// 8-15
fmt.Println("8 Bright colors")
for i := 0; i < 8; i++ {
fmt.Printf(color+" %d ", 0, 100+i, i)
}
fmt.Println(normal + "\n")
// 6 x 6 x 6 color cube
// color = 16 + 36 x r + 6 x g + b
// 16-231
fmt.Println("6x6x6 color cube")
for r := 0; r < 6; r++ {
for g := 0; g < 6; g++ {
for b := 0; b < 6; b++ {
c := 16 + 36*r + 6*g + b
fmt.Printf(bgcolor2+" %3d ", c, c)
}
fmt.Printf(normal + " ")
}
fmt.Println("")
}
fmt.Println(normal)
// Gray scale
// 232-255
fmt.Println("Gray scale")
for i := 232; i < 256; i++ {
fmt.Printf(bgcolor2+" %d ", i, i)
}
fmt.Println(normal + "\n")
// 24 bits
fmt.Println("Nice 24bits color square")
for r := 0; r < 60; r++ {
for g := 0; g < 1; g++ {
for b := 0; b < 60; b++ {
fmt.Printf(bgcolor24+" ", r, g, b)
}
fmt.Printf(normal)
}
fmt.Println("")
}
fmt.Println(normal)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment