Skip to content

Commit

Permalink
[color] Add flag to 'Hex.ToWeb' to disable shorthand generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 26, 2023
1 parent a6826ae commit 08493b3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 15 additions & 14 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
}

Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions color/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func ExampleParse() {

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

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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 08493b3

Please sign in to comment.