diff --git a/CHANGELOG.md b/CHANGELOG.md index 36d97c83..4d009e20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * `[color]` Added method `RGBA.WithAlpha` * `[color]` Improved support for hex colors with alpha * `[color]` Use web color representation for `Hex.String` +* `[color]` Added flag to `Hex.ToWeb` to disable shorthand generation * `[color]` Fixed shorthand hex generation for `#FFF` * `[color]` Fixed `RGBA` to `Hex` conversion diff --git a/color/color.go b/color/color.go index a1748530..d6629bd1 100644 --- a/color/color.go +++ b/color/color.go @@ -306,29 +306,30 @@ func (c RGBA) WithAlpha(alpha float64) RGBA { return c } -// ToWeb converts hex color notation used in web (#RGB / #RRGGBB/#RRGGBBAA) -func (c Hex) ToWeb(caps bool) string { +// ToWeb converts hex color to notation used in web (#RGB/#RGBA/#RRGGBB/#RRGGBBAA) +func (c Hex) ToWeb(useCaps, allowShorthand bool) string { var k string switch { - case c.IsRGBA() && caps: + case c.IsRGBA() && useCaps: k = fmt.Sprintf("%08X", uint32(c.v)) - case c.IsRGBA() && !caps: + case c.IsRGBA() && !useCaps: k = fmt.Sprintf("%08x", uint32(c.v)) - case caps: + case useCaps: k = fmt.Sprintf("%06X", uint32(c.v)) default: k = fmt.Sprintf("%06x", uint32(c.v)) } - // Generate shorthand color - if !c.IsRGBA() { - if k[0] == k[1] && k[2] == k[3] && k[4] == k[5] { - k = k[0:1] + k[2:3] + k[4:5] - } - } else { - if k[0] == k[1] && k[2] == k[3] && k[4] == k[5] && k[6] == k[7] { - k = k[0:1] + k[2:3] + k[4:5] + k[6:7] + if allowShorthand { + if !c.IsRGBA() { + if k[0] == k[1] && k[2] == k[3] && k[4] == k[5] { + k = k[0:1] + k[2:3] + k[4:5] + } + } else { + if k[0] == k[1] && k[2] == k[3] && k[4] == k[5] && k[6] == k[7] { + k = k[0:1] + k[2:3] + k[4:5] + k[6:7] + } } } @@ -353,7 +354,7 @@ func (c RGBA) String() string { // String returns string representation of hex color func (c Hex) String() string { - return fmt.Sprintf("Hex{%s}", c.ToWeb(true)) + return fmt.Sprintf("Hex{%s}", c.ToWeb(true, false)) } // String returns string representation of CMYK color diff --git a/color/color_test.go b/color/color_test.go index 6ece1058..d700de13 100644 --- a/color/color_test.go +++ b/color/color_test.go @@ -81,12 +81,15 @@ func (s *ColorSuite) TestHex(c *C) { c.Assert(NewHex(0x49d62d).String(), Equals, "Hex{#49D62D}") c.Assert(NewHex(0x49d62df7).String(), Equals, "Hex{#49D62DF7}") - c.Assert(NewHex(0x49d62d).ToWeb(false), Equals, "#49d62d") - c.Assert(NewHex(0x49d62df7).ToWeb(true), Equals, "#49D62DF7") - c.Assert(NewHex(0x49d62df7).ToWeb(false), Equals, "#49d62df7") - c.Assert(NewHex(0xFFAA44).ToWeb(true), Equals, "#FA4") - c.Assert(NewHex(0xFFAA44CC).ToWeb(true), Equals, "#FA4C") - c.Assert(NewHex(0x0).ToWeb(true), Equals, "#000") + 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") + c.Assert(NewHex(0xFFAA44).ToWeb(true, false), Equals, "#FFAA44") + c.Assert(NewHex(0xFFAA44).ToWeb(true, true), Equals, "#FA4") + c.Assert(NewHex(0xFFAA44CC).ToWeb(true, false), Equals, "#FFAA44CC") + c.Assert(NewHex(0xFFAA44CC).ToWeb(true, true), Equals, "#FA4C") + c.Assert(NewHex(0x0).ToWeb(true, false), Equals, "#000000") + c.Assert(NewHex(0x0).ToWeb(true, true), Equals, "#000") } func (s *ColorSuite) TestRGBA(c *C) { diff --git a/color/examples_test.go b/color/examples_test.go index 23a519f1..9d1b6713 100644 --- a/color/examples_test.go +++ b/color/examples_test.go @@ -21,13 +21,13 @@ func ExampleParse() { // Output: // Hex{#FF6347} - // Hex{#B8F} + // Hex{#BB88FF} // Hex{#FF3B21A6} // Hex{#F5FFFA} } func ExampleRGB2Hex() { - fmt.Printf("%s\n", RGB2Hex(RGB{127, 25, 75}).ToWeb(true)) + fmt.Printf("%s\n", RGB2Hex(RGB{127, 25, 75}).ToWeb(true, false)) // Output: #7F194B } @@ -47,7 +47,7 @@ func ExampleHex2RGBA() { func ExampleRGBA2Hex() { c := RGBA2Hex(RGBA{127, 25, 75, 204}) - fmt.Printf("%s\n", c.ToWeb(true)) + fmt.Printf("%s\n", c.ToWeb(true, false)) // Output: #7F194BCC } @@ -125,7 +125,7 @@ func ExampleContrast() { // ////////////////////////////////////////////////////////////////////////////////// // func ExampleRGB_ToHex() { - fmt.Printf("%s\n", RGB{127, 25, 75}.ToHex().ToWeb(true)) + fmt.Printf("%s\n", RGB{127, 25, 75}.ToHex().ToWeb(true, false)) // Output: #7F194B } @@ -159,7 +159,7 @@ func ExampleRGB_ToTerm() { func ExampleRGBA_ToHex() { c := RGBA{127, 25, 75, 204}.ToHex() - fmt.Printf("%s\n", c.ToWeb(true)) + fmt.Printf("%s\n", c.ToWeb(true, false)) // Output: #7F194BCC } @@ -190,8 +190,8 @@ func ExampleHex_IsRGBA() { c1 := NewHex(0x7F194B) c2 := NewHex(0x7F194B5F) - fmt.Printf("%s → %t\n", c1.ToWeb(true), c1.IsRGBA()) - fmt.Printf("%s → %t\n", c2.ToWeb(true), c2.IsRGBA()) + fmt.Printf("%s → %t\n", c1.ToWeb(true, false), c1.IsRGBA()) + fmt.Printf("%s → %t\n", c2.ToWeb(true, false), c2.IsRGBA()) // Output: // #7F194B → false @@ -211,10 +211,10 @@ func ExampleHex_ToRGBA() { } func ExampleHex_ToWeb() { - fmt.Printf("%s\n", NewHex(0x7F194B).ToWeb(true)) - fmt.Printf("%s\n", NewHex(0x7F194B).ToWeb(false)) + fmt.Printf("%s\n", NewHex(0xFFAA44CC).ToWeb(true, false)) + fmt.Printf("%s\n", NewHex(0xFFAA44CC).ToWeb(false, true)) // Output: - // #7F194B - // #7f194b + // #FFAA44CC + // #fa4c }