diff --git a/CHANGELOG.md b/CHANGELOG.md index d5778ebe..8a90260b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Changelog +### [13.14.3](https://kaos.sh/ek/13.14.3) + +- `[color]` Added `fmt.GoStringer` support for structs +- `[color]` Code refactoring + ### [13.14.2](https://kaos.sh/ek/13.14.2) - `[log]` Added variable `LogLevels` with all supported log level names diff --git a/color/color.go b/color/color.go index fee183be..26e7f4d4 100644 --- a/color/color.go +++ b/color/color.go @@ -336,51 +336,82 @@ func (c Hex) ToWeb(useCaps, allowShorthand bool) string { return "#" + k } +// String returns string representation of hex color +func (c Hex) String() string { + return c.ToWeb(true, false) +} + // String returns string representation of RGB color func (c RGB) String() string { - return fmt.Sprintf( - "RGB{R:%d G:%d B:%d}", - c.R, c.G, c.B, - ) + return fmt.Sprintf("%d,%d,%d", c.R, c.G, c.B) +} + +// GoString returns Go representation of RGB color +func (c RGB) GoString() string { + return fmt.Sprintf("RGB{R:%d, G:%d, B:%d}", c.R, c.G, c.B) } // String returns string representation of RGBA color func (c RGBA) String() string { - return fmt.Sprintf( - "RGBA{R:%d G:%d B:%d A:%.2f}", - c.R, c.G, c.B, float64(c.A)/255.0, - ) + return fmt.Sprintf("%d,%d,%d,%.2f", c.R, c.G, c.B, float64(c.A)/255.0) } -// String returns string representation of hex color -func (c Hex) String() string { - return fmt.Sprintf("Hex{%s}", c.ToWeb(true, false)) +// GoString returns Go representation of RGBA color +func (c RGBA) GoString() string { + if c.A == 0 { + return fmt.Sprintf("RGBA{R:%d, G:%d, B:%d}", c.R, c.G, c.B) + } + + return fmt.Sprintf("RGBA{R:%d, G:%d, B:%d, A:%d}", c.R, c.G, c.B, c.A) } // String returns string representation of CMYK color func (c CMYK) String() string { return fmt.Sprintf( - "CMYK{C:%.0f%% M:%.0f%% Y:%.0f%% K:%.0f%%}", + "%.0f%%,%.0f%%,%.0f%%,%.0f%%", c.C*100.0, c.M*100.0, c.Y*100.0, c.K*100.0, ) } +// GoString returns Go representation of CMYK color +func (c CMYK) GoString() string { + return fmt.Sprintf("CMYK{C:%g, M:%g, Y:%g, K:%g}", c.C, c.M, c.Y, c.K) +} + // String returns string representation of HSV color func (c HSV) String() string { return fmt.Sprintf( - "HSV{H:%.0f° S:%.0f%% V:%.0f%% A:%.0f%%}", + "%.0f°,%.0f%%,%.0f%%,%.0f%%", c.H*360, c.S*100, c.V*100, c.A*100, ) } +// GoString returns Go representation of HSV color +func (c HSV) GoString() string { + if c.A == 0 { + return fmt.Sprintf("HSV{H:%g, S:%g, V:%g}", c.H, c.S, c.V) + } + + return fmt.Sprintf("HSV{H:%g, S:%g, V:%g, A:%g}", c.H, c.S, c.V, c.A) +} + // String returns string representation of HSL color func (c HSL) String() string { return fmt.Sprintf( - "HSL{H:%.0f° S:%.0f%% L:%.0f%% A:%.0f%%}", + "%.0f°,%.0f%%,%.0f%%,%.0f%%", c.H*360, c.S*100, c.L*100, c.A*100, ) } +// GoString returns Go representation of HSL color +func (c HSL) GoString() string { + if c.A == 0 { + return fmt.Sprintf("HSL{H:%g, S:%g, L:%g}", c.H, c.S, c.L) + } + + return fmt.Sprintf("HSL{H:%g, S:%g, L:%g, A:%g}", c.H, c.S, c.L, c.A) +} + // ////////////////////////////////////////////////////////////////////////////////// // // Parse parses color diff --git a/color/color_test.go b/color/color_test.go index c515c81f..7addb8c5 100644 --- a/color/color_test.go +++ b/color/color_test.go @@ -70,7 +70,8 @@ func (s *ColorSuite) TestRGB(c *C) { c.Assert(RGB{255, 255, 255}.ToHex().v, DeepEquals, uint32(0xFFFFFF)) c.Assert(RGB{127, 127, 127}.ToHex().v, DeepEquals, uint32(0x7F7F7F)) - c.Assert(RGB{23, 182, 89}.String(), Equals, "RGB{R:23 G:182 B:89}") + c.Assert(RGB{23, 182, 89}.String(), Equals, "23,182,89") + c.Assert(RGB{23, 182, 89}.GoString(), Equals, "RGB{R:23, G:182, B:89}") } func (s *ColorSuite) TestHex(c *C) { @@ -78,8 +79,8 @@ func (s *ColorSuite) TestHex(c *C) { c.Assert(NewHex(0xFFFFFF).ToRGB(), DeepEquals, RGB{0xFF, 0xFF, 0xFF}) c.Assert(NewHex(0x49d62d).ToRGB(), DeepEquals, RGB{0x49, 0xd6, 0x2d}) - c.Assert(NewHex(0x49d62d).String(), Equals, "Hex{#49D62D}") - c.Assert(NewHex(0x49d62df7).String(), Equals, "Hex{#49D62DF7}") + c.Assert(NewHex(0x49d62d).String(), Equals, "#49D62D") + c.Assert(NewHex(0x49d62df7).String(), Equals, "#49D62DF7") c.Assert(NewHex(0x49d62d).ToWeb(false, false), Equals, "#49d62d") c.Assert(NewHex(0x49d62df7).ToWeb(true, false), Equals, "#49D62DF7") c.Assert(NewHex(0x49d62df7).ToWeb(false, false), Equals, "#49d62df7") @@ -104,11 +105,13 @@ func (s *ColorSuite) TestRGBA(c *C) { c.Assert(NewHex(0xFFAABB).IsRGBA(), Equals, false) c.Assert(NewHex(0xFFAABB01).IsRGBA(), Equals, true) - c.Assert(RGBA{23, 182, 89, 130}.String(), Equals, "RGBA{R:23 G:182 B:89 A:0.51}") + c.Assert(RGBA{23, 182, 89, 130}.String(), Equals, "23,182,89,0.51") clr := RGBA{23, 182, 89, 0} - c.Assert(clr.String(), Equals, "RGBA{R:23 G:182 B:89 A:0.00}") - c.Assert(clr.WithAlpha(0.23).String(), Equals, "RGBA{R:23 G:182 B:89 A:0.23}") + c.Assert(clr.String(), Equals, "23,182,89,0.00") + c.Assert(clr.GoString(), Equals, "RGBA{R:23, G:182, B:89}") + c.Assert(clr.WithAlpha(0.23).String(), Equals, "23,182,89,0.23") + c.Assert(clr.WithAlpha(0.23).GoString(), Equals, "RGBA{R:23, G:182, B:89, A:58}") } func (s *ColorSuite) TestCMYK(c *C) { @@ -121,7 +124,8 @@ func (s *ColorSuite) TestCMYK(c *C) { c.Assert(CMYK{0.0, 0.0, 0.0, 1.0}.ToRGB(), DeepEquals, RGB{0, 0, 0}) c.Assert(CMYK{0.64, 0.77, 0, 0.17}.ToRGB(), DeepEquals, RGB{76, 48, 211}) - c.Assert(CMYK{0.64, 0.77, 0, 0.17}.String(), Equals, "CMYK{C:64% M:77% Y:0% K:17%}") + c.Assert(CMYK{0.64, 0.77, 0, 0.17}.String(), Equals, "64%,77%,0%,17%") + c.Assert(CMYK{0.64, 0.77, 0, 0.17}.GoString(), Equals, "CMYK{C:0.64, M:0.77, Y:0, K:0.17}") } func (s *ColorSuite) TestHSV(c *C) { @@ -143,8 +147,10 @@ func (s *ColorSuite) TestHSV(c *C) { c.Assert(HSV{0.32, 0.12, 0.76, 0.5}.ToRGBA(), DeepEquals, RGBA{172, 193, 170, 127}) - c.Assert(RGB{73, 158, 105}.ToHSV().String(), Equals, "HSV{H:143° S:54% V:62% A:0%}") - c.Assert(RGBA{73, 158, 105, 127}.ToHSV().String(), Equals, "HSV{H:143° S:54% V:62% A:50%}") + c.Assert(RGB{73, 158, 105}.ToHSV().String(), Equals, "143°,54%,62%,0%") + c.Assert(RGB{73, 158, 105}.ToHSV().GoString(), Equals, "HSV{H:0.396078431372549, S:0.5379746835443039, V:0.6196078431372549}") + c.Assert(RGBA{73, 158, 105, 127}.ToHSV().String(), Equals, "143°,54%,62%,50%") + c.Assert(RGBA{73, 158, 105, 127}.ToHSV().GoString(), Equals, "HSV{H:0.396078431372549, S:0.5379746835443039, V:0.6196078431372549, A:0.4980392156862745}") } func (s *ColorSuite) TestHSL(c *C) { @@ -160,8 +166,10 @@ func (s *ColorSuite) TestHSL(c *C) { c.Assert(HSL{0.6833333333333332, 0.7079646017699115, 0.5568627450980392, 0.0}.ToRGB(), DeepEquals, RGB{77, 62, 222}) c.Assert(HSL{0.6833333333333332, 0.7079646017699115, 0.5568627450980392, 0.50}.ToRGBA(), DeepEquals, RGBA{77, 62, 222, 127}) - c.Assert(RGB{146, 93, 176}.ToHSL().String(), Equals, "HSL{H:278° S:34% L:53% A:0%}") - c.Assert(RGBA{146, 93, 176, 80}.ToHSL().String(), Equals, "HSL{H:278° S:34% L:53% A:31%}") + c.Assert(RGB{146, 93, 176}.ToHSL().String(), Equals, "278°,34%,53%,0%") + c.Assert(RGB{146, 93, 176}.ToHSL().GoString(), Equals, "HSL{H:0.7730923694779116, S:0.34439834024896265, L:0.5274509803921569}") + c.Assert(RGBA{146, 93, 176, 80}.ToHSL().String(), Equals, "278°,34%,53%,31%") + c.Assert(RGBA{146, 93, 176, 80}.ToHSL().GoString(), Equals, "HSL{H:0.7730923694779116, S:0.34439834024896265, L:0.5274509803921569, A:0.3137254901960784}") } func (s *ColorSuite) TestHUE(c *C) { diff --git a/color/examples_test.go b/color/examples_test.go index 64442377..f87e7709 100644 --- a/color/examples_test.go +++ b/color/examples_test.go @@ -20,10 +20,10 @@ func ExampleParse() { fmt.Println(Parse("mintcream")) // Output: - // Hex{#FF6347} - // Hex{#BB88FF} - // Hex{#FF3B21A6} - // Hex{#F5FFFA} + // #FF6347 + // #BB88FF + // #FF3B21A6 + // #F5FFFA } func ExampleRGB2Hex() { @@ -33,15 +33,15 @@ func ExampleRGB2Hex() { } func ExampleHex2RGB() { - fmt.Printf("%s\n", Hex2RGB(NewHex(0x7F194B))) + fmt.Printf("%#v\n", Hex2RGB(NewHex(0x7F194B))) - // Output: RGB{R:127 G:25 B:75} + // Output: RGB{R:127, G:25, B:75} } func ExampleHex2RGBA() { - fmt.Printf("%s\n", Hex2RGBA(NewHex(0x7F194BCC))) + fmt.Printf("%#v\n", Hex2RGBA(NewHex(0x7F194BCC))) - // Output: RGBA{R:127 G:25 B:75 A:0.80} + // Output: RGBA{R:127, G:25, B:75, A:204} } func ExampleRGBA2Hex() { @@ -55,35 +55,35 @@ func ExampleRGBA2Hex() { func ExampleRGB2Term() { c := RGB{255, 0, 0} - fmt.Printf("%s → \\e[38;5;%dm\n", c, RGB2Term(c)) + fmt.Printf("%#v → \\e[38;5;%dm\n", c, RGB2Term(c)) - // Output: RGB{R:255 G:0 B:0} → \e[38;5;196m + // Output: RGB{R:255, G:0, B:0} → \e[38;5;196m } func ExampleTerm2RGB() { c := uint8(162) - fmt.Printf("%d → %s\n", c, Term2RGB(c)) + fmt.Printf("%d → %#v\n", c, Term2RGB(c)) - // Output: 162 → RGB{R:215 G:0 B:135} + // Output: 162 → RGB{R:215, G:0, B:135} } func ExampleRGB2CMYK() { fmt.Printf("%s\n", RGB2CMYK(RGB{127, 25, 75})) - // Output: CMYK{C:0% M:80% Y:41% K:50%} + // Output: 0%,80%,41%,50% } func ExampleCMYK2RGB() { fmt.Printf("%s\n", CMYK2RGB(CMYK{0, 0.8, 0.41, 0.5})) - // Output: RGB{R:127 G:25 B:75} + // Output: 127,25,75 } func ExampleRGB2HSV() { fmt.Printf("%s\n", RGB2HSV(RGB{127, 25, 75})) - // Output: HSV{H:331° S:80% V:50% A:0%} + // Output: 331°,80%,50%,0% } func ExampleHSV2RGB() { @@ -91,13 +91,13 @@ func ExampleHSV2RGB() { fmt.Printf("%s\n", c) - // Output: RGB{R:127 G:25 B:74} + // Output: 127,25,74 } func ExampleRGB2HSL() { fmt.Printf("%s\n", RGB2HSL(RGB{127, 25, 75})) - // Output: HSL{H:331° S:67% L:30% A:0%} + // Output: 331°,67%,30%,0% } func ExampleHSL2RGB() { @@ -105,7 +105,7 @@ func ExampleHSL2RGB() { fmt.Printf("%s\n", c) - // Output: RGB{R:127 G:25 B:74} + // Output: 127,25,74 } func ExampleHUE2RGB() { @@ -141,19 +141,19 @@ func ExampleRGB_ToHex() { func ExampleRGB_ToCMYK() { fmt.Printf("%s\n", RGB{127, 25, 75}.ToCMYK()) - // Output: CMYK{C:0% M:80% Y:41% K:50%} + // Output: 0%,80%,41%,50% } func ExampleRGB_ToHSV() { fmt.Printf("%s\n", RGB{127, 25, 75}.ToHSV()) - // Output: HSV{H:331° S:80% V:50% A:0%} + // Output: 331°,80%,50%,0% } func ExampleRGB_ToHSL() { fmt.Printf("%s\n", RGB{127, 25, 75}.ToHSL()) - // Output: HSL{H:331° S:67% L:30% A:0%} + // Output: 331°,67%,30%,0% } func ExampleRGB_ToTerm() { @@ -161,7 +161,7 @@ func ExampleRGB_ToTerm() { fmt.Printf("%s → \\e[38;5;%dm\n", c, c.ToTerm()) - // Output: RGB{R:255 G:0 B:0} → \e[38;5;196m + // Output: 255,0,0 → \e[38;5;196m } func ExampleRGBA_ToHex() { @@ -175,7 +175,7 @@ func ExampleRGBA_ToHex() { func ExampleCMYK_ToRGB() { fmt.Printf("%s\n", CMYK{0, 0.8, 0.41, 0.5}.ToRGB()) - // Output: RGB{R:127 G:25 B:75} + // Output: 127,25,75 } func ExampleHSV_ToRGB() { @@ -183,7 +183,7 @@ func ExampleHSV_ToRGB() { fmt.Printf("%s\n", c) - // Output: RGB{R:127 G:25 B:74} + // Output: 127,25,74 } func ExampleHSL_ToRGB() { @@ -191,7 +191,7 @@ func ExampleHSL_ToRGB() { fmt.Printf("%s\n", c) - // Output: RGB{R:127 G:25 B:74} + // Output: 127,25,74 } func ExampleHex_IsRGBA() { @@ -209,13 +209,13 @@ func ExampleHex_IsRGBA() { func ExampleHex_ToRGB() { fmt.Printf("%s\n", NewHex(0x7F194B).ToRGB()) - // Output: RGB{R:127 G:25 B:75} + // Output: 127,25,75 } func ExampleHex_ToRGBA() { fmt.Printf("%s\n", NewHex(0x7F194B5F).ToRGBA()) - // Output: RGBA{R:127 G:25 B:75 A:0.37} + // Output: 127,25,75,0.37 } func ExampleHex_ToWeb() { diff --git a/version.go b/version.go index 085c7f47..e319567e 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.14.2" +const VERSION = "13.14.3"