Skip to content

Commit

Permalink
[color] Add fmt.GoStringer support for structs
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Dec 9, 2024
1 parent 27ab8d5 commit 8ba5764
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 53 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
59 changes: 45 additions & 14 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ 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) {
c.Assert(NewHex(0x000000).ToRGB(), DeepEquals, RGB{0x00, 0x00, 0x00})
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")
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
54 changes: 27 additions & 27 deletions color/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func ExampleParse() {
fmt.Println(Parse("mintcream"))

// Output:
// Hex{#FF6347} <nil>
// Hex{#BB88FF} <nil>
// Hex{#FF3B21A6} <nil>
// Hex{#F5FFFA} <nil>
// #FF6347 <nil>
// #BB88FF <nil>
// #FF3B21A6 <nil>
// #F5FFFA <nil>
}

func ExampleRGB2Hex() {
Expand All @@ -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() {
Expand All @@ -55,57 +55,57 @@ 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() {
c := HSV2RGB(HSV{H: 331.0 / 360.0, S: 80.0 / 100.0, V: 50.0 / 100.0})

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() {
c := HSL2RGB(HSL{H: 331.0 / 360.0, S: 67.0 / 100.0, L: 30.0 / 100.0})

fmt.Printf("%s\n", c)

// Output: RGB{R:127 G:25 B:74}
// Output: 127,25,74
}

func ExampleHUE2RGB() {
Expand Down Expand Up @@ -141,27 +141,27 @@ 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() {
c := RGB{255, 0, 0}

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() {
Expand All @@ -175,23 +175,23 @@ 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() {
c := HSV{H: 331.0 / 360.0, S: 80.0 / 100.0, V: 50.0 / 100.0}.ToRGB()

fmt.Printf("%s\n", c)

// Output: RGB{R:127 G:25 B:74}
// Output: 127,25,74
}

func ExampleHSL_ToRGB() {
c := HSL{H: 331.0 / 360.0, S: 67.0 / 100.0, L: 30.0 / 100.0}.ToRGB()

fmt.Printf("%s\n", c)

// Output: RGB{R:127 G:25 B:74}
// Output: 127,25,74
}

func ExampleHex_IsRGBA() {
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.14.2"
const VERSION = "13.14.3"

0 comments on commit 8ba5764

Please sign in to comment.