From 0e7bc76a0c932cbe12a86d3b27e360a70e42e1e6 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 12 Mar 2024 15:23:08 +0100 Subject: [PATCH] Added more tests --- Color.go | 8 -------- Color_test.go | 33 --------------------------------- LCH_test.go | 2 +- README.md | 15 ++++++++++----- sRGB.go => RGB.go | 5 +++++ RGB_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ Value.go | 4 ++++ ansi/Code_test.go | 22 +++++++++++++++------- tty/Terminal_darwin.go | 5 ++++- tty/Terminal_test.go | 12 ++++++++++++ 10 files changed, 91 insertions(+), 55 deletions(-) rename sRGB.go => RGB.go (77%) create mode 100644 RGB_test.go create mode 100644 Value.go create mode 100644 tty/Terminal_test.go diff --git a/Color.go b/Color.go index 04489e9..85be94c 100644 --- a/Color.go +++ b/Color.go @@ -4,9 +4,6 @@ import ( "fmt" ) -// Value is a type definition for the data type of a single color component. -type Value = float64 - // Color represents an RGB color. type Color struct { R Value @@ -14,11 +11,6 @@ type Color struct { B Value } -// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0. -func RGB(r Value, g Value, b Value) Color { - return Color{r, g, b} -} - // Print writes the text in the given color to standard output. func (c Color) Print(args ...any) { if !Terminal { diff --git a/Color_test.go b/Color_test.go index f21e033..8260a09 100644 --- a/Color_test.go +++ b/Color_test.go @@ -3,7 +3,6 @@ package color_test import ( "testing" - "git.akyoto.dev/go/assert" "git.akyoto.dev/go/color" ) @@ -48,35 +47,3 @@ func TestPrintln(t *testing.T) { color.RGB(0, 1, 0).Println("green") color.RGB(0, 0, 1).Println("blue") } - -func TestRGB(t *testing.T) { - color.Terminal = true - - rgbColors := map[string]color.Color{ - "black": color.RGB(0, 0, 0), - "white": color.RGB(1, 1, 1), - "gray": color.RGB(0.5, 0.5, 0.5), - "red": color.RGB(1, 0, 0), - "green": color.RGB(0, 1, 0), - "blue": color.RGB(0, 0, 1), - "cyan": color.RGB(0, 1, 1), - "yellow": color.RGB(1, 1, 0), - "orange": color.RGB(1, 0.5, 0), - "magenta": color.RGB(1, 0, 1), - } - - for name, c := range rgbColors { - testColorRange(t, c) - c.Println("█ " + name) - } -} - -func testColorRange(t *testing.T, c color.Color) { - assert.True(t, c.R >= 0.0) - assert.True(t, c.G >= 0.0) - assert.True(t, c.B >= 0.0) - - assert.True(t, c.R <= 1.0) - assert.True(t, c.G <= 1.0) - assert.True(t, c.B <= 1.0) -} diff --git a/LCH_test.go b/LCH_test.go index 6dc33a2..f2d59a9 100644 --- a/LCH_test.go +++ b/LCH_test.go @@ -18,7 +18,7 @@ func TestLCH(t *testing.T) { "red": color.LCH(0.75, 1.0, 40), "orange": color.LCH(0.75, 1.0, 60), "yellow": color.LCH(0.9, 1.0, 100), - "green": color.LCH(0.75, 1.0, 150), + "green": color.LCH(0.75, 1.0, 135), "blue": color.LCH(0.75, 1.0, 260), "cyan": color.LCH(0.75, 1.0, 210), "magenta": color.LCH(0.75, 1.0, 320), diff --git a/README.md b/README.md index e7ff716..fdca232 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,16 @@ go get git.akyoto.dev/go/color ## Usage ```go -red := color.RGB(1.0, 0.0, 0.0) -red.Println("red text") +// ANSI +ansi.Red.Println("red text") -orange := color.LCH(0.7, 1.0, 65) -orange.Println("orange text") +// LCH +green := color.LCH(0.5, 1.0, 135) +green.Println("green text") + +// RGB +blue := color.RGB(0, 0, 1) +blue.Println("blue text") ``` ## Tests @@ -30,9 +35,9 @@ orange.Println("orange text") PASS: TestPrint PASS: TestPrintf PASS: TestPrintln -PASS: TestRGB PASS: TestLCH PASS: TestLCHSpectrum +PASS: TestRGB coverage: 100.0% of statements ``` diff --git a/sRGB.go b/RGB.go similarity index 77% rename from sRGB.go rename to RGB.go index 1d522c8..f882f07 100644 --- a/sRGB.go +++ b/RGB.go @@ -2,6 +2,11 @@ package color import "math" +// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0. +func RGB(r Value, g Value, b Value) Color { + return Color{r, g, b} +} + // inSRGB indicates whether the given color can be mapped to the sRGB color space. func inSRGB(l Value, a Value, b Value, chroma Value) bool { r, g, b := oklabToLinearRGB(l, a*chroma, b*chroma) diff --git a/RGB_test.go b/RGB_test.go new file mode 100644 index 0000000..86686e4 --- /dev/null +++ b/RGB_test.go @@ -0,0 +1,40 @@ +package color_test + +import ( + "testing" + + "git.akyoto.dev/go/assert" + "git.akyoto.dev/go/color" +) + +func TestRGB(t *testing.T) { + color.Terminal = true + + rgbColors := map[string]color.Color{ + "black": color.RGB(0, 0, 0), + "white": color.RGB(1, 1, 1), + "gray": color.RGB(0.5, 0.5, 0.5), + "red": color.RGB(1, 0, 0), + "green": color.RGB(0, 1, 0), + "blue": color.RGB(0, 0, 1), + "cyan": color.RGB(0, 1, 1), + "yellow": color.RGB(1, 1, 0), + "orange": color.RGB(1, 0.5, 0), + "magenta": color.RGB(1, 0, 1), + } + + for name, c := range rgbColors { + testColorRange(t, c) + c.Println("█ " + name) + } +} + +func testColorRange(t *testing.T, c color.Color) { + assert.True(t, c.R >= 0.0) + assert.True(t, c.G >= 0.0) + assert.True(t, c.B >= 0.0) + + assert.True(t, c.R <= 1.0) + assert.True(t, c.G <= 1.0) + assert.True(t, c.B <= 1.0) +} diff --git a/Value.go b/Value.go new file mode 100644 index 0000000..42635f1 --- /dev/null +++ b/Value.go @@ -0,0 +1,4 @@ +package color + +// Value is a type definition for the data type of a single color component. +type Value = float64 diff --git a/ansi/Code_test.go b/ansi/Code_test.go index a2230d6..a9fb83a 100644 --- a/ansi/Code_test.go +++ b/ansi/Code_test.go @@ -7,17 +7,21 @@ import ( "git.akyoto.dev/go/color/ansi" ) -func Test(t *testing.T) { - color.Terminal = true - testColors() -} - func TestPrintRaw(t *testing.T) { color.Terminal = false - testColors() + testPrint() + testPrintf() + testPrintln() } -func testColors() { +func TestPrint(t *testing.T) { + color.Terminal = true + testPrint() + testPrintf() + testPrintln() +} + +func testPrint() { ansi.Black.Print("Black\n") ansi.White.Print("White\n") ansi.Red.Print("Red\n") @@ -26,7 +30,9 @@ func testColors() { ansi.Yellow.Print("Yellow\n") ansi.Magenta.Print("Magenta\n") ansi.Cyan.Print("Cyan\n") +} +func testPrintf() { ansi.Black.Printf("%s\n", "Black") ansi.White.Printf("%s\n", "White") ansi.Red.Printf("%s\n", "Red") @@ -35,7 +41,9 @@ func testColors() { ansi.Yellow.Printf("%s\n", "Yellow") ansi.Magenta.Printf("%s\n", "Magenta") ansi.Cyan.Printf("%s\n", "Cyan") +} +func testPrintln() { ansi.Black.Println("Black") ansi.White.Println("White") ansi.Red.Println("Red") diff --git a/tty/Terminal_darwin.go b/tty/Terminal_darwin.go index 48257c4..0726279 100644 --- a/tty/Terminal_darwin.go +++ b/tty/Terminal_darwin.go @@ -1,6 +1,9 @@ package tty +import "golang.org/x/sys/unix" + // IsTerminal returns true if the file descriptor is a terminal. func IsTerminal(fd uintptr) bool { - return false + _, err := unix.IoctlGetTermios(int(fd), unix.TIOCGETA) + return err == nil } diff --git a/tty/Terminal_test.go b/tty/Terminal_test.go new file mode 100644 index 0000000..be7406f --- /dev/null +++ b/tty/Terminal_test.go @@ -0,0 +1,12 @@ +package tty_test + +import ( + "os" + "testing" + + "git.akyoto.dev/go/color/tty" +) + +func TestIsTerminal(t *testing.T) { + tty.IsTerminal(os.Stdout.Fd()) +}