commit aae983fd5b1790bcf7f9490261af505f97fe5e62 Author: Eduard Urbach Date: Tue Mar 5 17:47:02 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7dbfd92 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +* +!*/ +!.gitignore +!*.go +!*.md +!*.mod +!*.sum diff --git a/Benchmarks_test.go b/Benchmarks_test.go new file mode 100644 index 0000000..fdbb35c --- /dev/null +++ b/Benchmarks_test.go @@ -0,0 +1,22 @@ +package color_test + +import ( + "io" + "testing" + + "git.akyoto.dev/go/color" +) + +func BenchmarkRGB(b *testing.B) { + for i := 0; i < b.N; i++ { + color.RGB(1.0, 1.0, 1.0) + } +} + +func BenchmarkFprint(b *testing.B) { + c := color.RGB(1.0, 1.0, 1.0) + + for i := 0; i < b.N; i++ { + c.Fprint(io.Discard, "") + } +} diff --git a/Color.go b/Color.go new file mode 100644 index 0000000..dfd67c0 --- /dev/null +++ b/Color.go @@ -0,0 +1,48 @@ +package color + +import ( + "fmt" + "io" +) + +// Component is a type definition for the data type of a single color component. +type Component float32 + +// Color represents an RGB color. +type Color struct { + R Component + G Component + B Component +} + +// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0. +func RGB(r Component, g Component, b Component) Color { + return Color{r, g, b} +} + +// Fprint writes the text in the given color to the writer. +func (c Color) Fprint(writer io.Writer, text string) { + if !Terminal { + fmt.Fprint(writer, text) + } + + fmt.Fprintf(writer, format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) +} + +// Print writes the text in the given color to standard output. +func (c Color) Print(text string) { + if !Terminal { + fmt.Print(text) + } + + fmt.Printf(format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) +} + +// Println writes the text in the given color to standard output and appends a newline. +func (c Color) Println(text string) { + if !Terminal { + fmt.Println(text) + } + + fmt.Printf(formatLine, byte(c.R*255), byte(c.G*255), byte(c.B*255), text) +} diff --git a/Color_test.go b/Color_test.go new file mode 100644 index 0000000..12c21d9 --- /dev/null +++ b/Color_test.go @@ -0,0 +1,39 @@ +package color_test + +import ( + "os" + "testing" + + "git.akyoto.dev/go/assert" + "git.akyoto.dev/go/color" +) + +func TestColors(t *testing.T) { + colors := map[string]color.Color{ + "black": color.RGB(0.0, 0.0, 0.0), + "white": color.RGB(1.0, 1.0, 1.0), + "red": color.RGB(1.0, 0.0, 0.0), + "green": color.RGB(0.0, 1.0, 0.0), + "blue": color.RGB(0.0, 0.0, 1.0), + } + + for name, value := range colors { + assert.True(t, value.R >= 0.0) + assert.True(t, value.G >= 0.0) + assert.True(t, value.B >= 0.0) + + assert.True(t, value.R <= 1.0) + assert.True(t, value.G <= 1.0) + assert.True(t, value.B <= 1.0) + + color.Terminal = true + value.Fprint(os.Stdout, name) + value.Print(name) + value.Println(name) + + color.Terminal = false + value.Fprint(os.Stdout, "-") + value.Print("-") + value.Println("-") + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..20759ff --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# color + +Adds color to your terminal output. + +## Features + +- Truecolor output + +## Installation + +```shell +go get git.akyoto.dev/go/color +``` + +## Usage + +```go +red := color.RGB(1.0, 0.0, 0.0) +fmt.Println(red.Format("red text")) +``` + +## Tests + +``` +PASS: TestColor +coverage: 100.0% of statements +``` + +## Benchmarks + +``` +BenchmarkRGB-12 1000000000 0.3131 ns/op 0 B/op 0 allocs/op +BenchmarkFprint-12 6455497 186.2 ns/op 0 B/op 0 allocs/op +``` + +## License + +Please see the [license documentation](https://akyoto.dev/license). + +## Copyright + +© 2024 Eduard Urbach diff --git a/Terminal.go b/Terminal.go new file mode 100644 index 0000000..f7c2d78 --- /dev/null +++ b/Terminal.go @@ -0,0 +1,14 @@ +package color + +import ( + "os" +) + +// These constants represent the escape codes needed to display color in terminals. +const ( + format = "\x1b[38;2;%d;%d;%dm%s\x1b[0m" + formatLine = format + "\n" +) + +// Terminal is a boolean that indicates if we're in a terminal with truecolor support. +var Terminal = os.Getenv("COLORTERM") == "truecolor" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..481b841 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.akyoto.dev/go/color + +go 1.22.0 + +require git.akyoto.dev/go/assert v0.1.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9fc2547 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8= +git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=