Initial commit
This commit is contained in:
commit
aae983fd5b
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
*
|
||||
!*/
|
||||
!.gitignore
|
||||
!*.go
|
||||
!*.md
|
||||
!*.mod
|
||||
!*.sum
|
22
Benchmarks_test.go
Normal file
22
Benchmarks_test.go
Normal file
@ -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, "")
|
||||
}
|
||||
}
|
48
Color.go
Normal file
48
Color.go
Normal file
@ -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)
|
||||
}
|
39
Color_test.go
Normal file
39
Color_test.go
Normal file
@ -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("-")
|
||||
}
|
||||
}
|
42
README.md
Normal file
42
README.md
Normal file
@ -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
|
14
Terminal.go
Normal file
14
Terminal.go
Normal file
@ -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"
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module git.akyoto.dev/go/color
|
||||
|
||||
go 1.22.0
|
||||
|
||||
require git.akyoto.dev/go/assert v0.1.3
|
Loading…
Reference in New Issue
Block a user