Use shorthand hex colors if possible

main
Anton Novojilov 3 years ago
parent b9c63e3e0b
commit 7a3429c7d1

@ -9,6 +9,7 @@ package badge
// ////////////////////////////////////////////////////////////////////////////////// // // ////////////////////////////////////////////////////////////////////////////////// //
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"math" "math"
"strconv" "strconv"
@ -154,7 +155,7 @@ func (g *Generator) GenerateFlatSquareSimple(message, color string) []byte {
// generateBadge generates badge with given template // generateBadge generates badge with given template
func (g *Generator) generateBadge(template, label, message, color string) []byte { func (g *Generator) generateBadge(template, label, message, color string) []byte {
color = formatColor(color) c := parseColor(color)
gF := float64(g.Offset) gF := float64(g.Offset)
lW := float64(g.drawer.MeasureString(label)>>6) + gF lW := float64(g.drawer.MeasureString(label)>>6) + gF
@ -166,11 +167,11 @@ func (g *Generator) generateBadge(template, label, message, color string) []byte
mL := (mW - gF) * (10.0 + g.Spacing - 0.5) mL := (mW - gF) * (10.0 + g.Spacing - 0.5)
fS := g.fontSize * 10 fS := g.fontSize * 10
mC, mS := getMessageColors(color) mC, mS := getMessageColors(c)
badge := strings.ReplaceAll(template, "{LABEL}", label) badge := strings.ReplaceAll(template, "{LABEL}", label)
badge = strings.ReplaceAll(badge, "{MESSAGE}", message) badge = strings.ReplaceAll(badge, "{MESSAGE}", message)
badge = strings.ReplaceAll(badge, "{COLOR}", "#"+color) badge = strings.ReplaceAll(badge, "{COLOR}", formatColor(c))
badge = strings.ReplaceAll(badge, "{WIDTH}", formatFloat(fW)) badge = strings.ReplaceAll(badge, "{WIDTH}", formatFloat(fW))
badge = strings.ReplaceAll(badge, "{LABEL_WIDTH}", formatFloat(lW)) badge = strings.ReplaceAll(badge, "{LABEL_WIDTH}", formatFloat(lW))
badge = strings.ReplaceAll(badge, "{MESSAGE_WIDTH}", formatFloat(mW)) badge = strings.ReplaceAll(badge, "{MESSAGE_WIDTH}", formatFloat(mW))
@ -188,7 +189,7 @@ func (g *Generator) generateBadge(template, label, message, color string) []byte
// generateBadgeSimple generates badge with given template // generateBadgeSimple generates badge with given template
func (g *Generator) generateBadgeSimple(template, message, color string) []byte { func (g *Generator) generateBadgeSimple(template, message, color string) []byte {
color = formatColor(color) c := parseColor(color)
gF := float64(g.Offset) gF := float64(g.Offset)
fW := float64(g.drawer.MeasureString(message)>>6) + gF fW := float64(g.drawer.MeasureString(message)>>6) + gF
@ -196,13 +197,13 @@ func (g *Generator) generateBadgeSimple(template, message, color string) []byte
mL := (fW - gF) * (10.0 + g.Spacing) mL := (fW - gF) * (10.0 + g.Spacing)
fS := g.fontSize * 10 fS := g.fontSize * 10
mC, mS := getMessageColors(color) mC, mS := getMessageColors(c)
badge := strings.ReplaceAll(template, "{MESSAGE}", message) badge := strings.ReplaceAll(template, "{MESSAGE}", message)
badge = strings.ReplaceAll(badge, "{COLOR}", "#"+color) badge = strings.ReplaceAll(badge, "{COLOR}", formatColor(c))
badge = strings.ReplaceAll(badge, "{WIDTH}", strconv.Itoa(int(fW))) badge = strings.ReplaceAll(badge, "{WIDTH}", formatFloat(fW))
badge = strings.ReplaceAll(badge, "{MESSAGE_X}", strconv.Itoa(int(mX))) badge = strings.ReplaceAll(badge, "{MESSAGE_X}", formatFloat(mX))
badge = strings.ReplaceAll(badge, "{MESSAGE_LENGTH}", strconv.Itoa(int(mL))) badge = strings.ReplaceAll(badge, "{MESSAGE_LENGTH}", formatFloat(mL))
badge = strings.ReplaceAll(badge, "{MESSAGE_COLOR}", mC) badge = strings.ReplaceAll(badge, "{MESSAGE_COLOR}", mC)
badge = strings.ReplaceAll(badge, "{MESSAGE_SHADOW}", mS) badge = strings.ReplaceAll(badge, "{MESSAGE_SHADOW}", mS)
badge = strings.ReplaceAll(badge, "{FONT}", g.fontName) badge = strings.ReplaceAll(badge, "{FONT}", g.fontName)
@ -213,20 +214,33 @@ func (g *Generator) generateBadgeSimple(template, message, color string) []byte
// ////////////////////////////////////////////////////////////////////////////////// // // ////////////////////////////////////////////////////////////////////////////////// //
// formatColor formats color // parseColor parses hex color
func formatColor(c string) string { func parseColor(c string) int64 {
if strings.HasPrefix(c, "#") { if strings.HasPrefix(c, "#") {
c = strings.TrimLeft(c, "#") c = strings.TrimLeft(c, "#")
} }
// Short hex // Shorthand hex color
if len(c) == 3 { if len(c) == 3 {
c = strings.Repeat(string(c[0]), 2) + c = strings.Repeat(string(c[0]), 2) +
strings.Repeat(string(c[1]), 2) + strings.Repeat(string(c[1]), 2) +
strings.Repeat(string(c[2]), 2) strings.Repeat(string(c[2]), 2)
} }
return c i, _ := strconv.ParseInt(c, 16, 32)
return i
}
// formatColor formats color
func formatColor(c int64) string {
k := fmt.Sprintf("%06x", c)
if k[0] == k[1] && k[2] == k[3] && k[4] == k[5] {
k = k[0:1] + k[2:3] + k[4:5]
}
return "#" + k
} }
// formatFloat formats float values // formatFloat formats float values
@ -235,9 +249,7 @@ func formatFloat(v float64) string {
} }
// getMessageColors returns message text and shadow colors based on color of badge // getMessageColors returns message text and shadow colors based on color of badge
func getMessageColors(badgeColor string) (string, string) { func getMessageColors(c int64) (string, string) {
c := parseColor(badgeColor)
if c == 0 || calcLuminance(c) < 0.65 { if c == 0 || calcLuminance(c) < 0.65 {
return "#fff", "#010101" return "#fff", "#010101"
} }
@ -245,12 +257,6 @@ func getMessageColors(badgeColor string) (string, string) {
return "#333", "#ccc" return "#333", "#ccc"
} }
// parseColor parses hex color
func parseColor(c string) int64 {
i, _ := strconv.ParseInt(c, 16, 32)
return i
}
// calcLuminance calculates relative luminance // calcLuminance calculates relative luminance
func calcLuminance(color int64) float64 { func calcLuminance(color int64) float64 {
r := calcLumColor(float64(color>>16&0xFF) / 255) r := calcLumColor(float64(color>>16&0xFF) / 255)

@ -146,9 +146,12 @@ func (s *BadgeSuite) TestBlackAndWhite(c *C) {
} }
func (s *BadgeSuite) TestAux(c *C) { func (s *BadgeSuite) TestAux(c *C) {
c.Assert(formatColor("000000"), Equals, "000000") c.Assert(parseColor("000000"), Equals, int64(0x000000))
c.Assert(formatColor("#000000"), Equals, "000000") c.Assert(parseColor("#000000"), Equals, int64(0x000000))
c.Assert(formatColor("#4c1"), Equals, "44cc11") c.Assert(parseColor("#4c1"), Equals, int64(0x44cc11))
c.Assert(formatColor(0x000000), Equals, "#000")
c.Assert(formatColor(0xFCA1B4), Equals, "#fca1b4")
c.Assert(calcLumColor(0.7), Equals, 0.4479884124418833) c.Assert(calcLumColor(0.7), Equals, 0.4479884124418833)
c.Assert(calcLumColor(0.01), Equals, 0.0007739938080495357) c.Assert(calcLumColor(0.01), Equals, 0.0007739938080495357)

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="60" height="20" role="img" aria-label="message"><title>message</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="60" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="0" height="20" fill="#000000"/><rect x="0" width="60" height="20" fill="#000000"/><rect width="60" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="300" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">message</text><text x="300" y="140" transform="scale(.1)" fill="#fff" textLength="510">message</text></g></svg> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="60" height="20" role="img" aria-label="message"><title>message</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="60" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="0" height="20" fill="#000"/><rect x="0" width="60" height="20" fill="#000"/><rect width="60" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="300" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">message</text><text x="300" y="140" transform="scale(.1)" fill="#fff" textLength="510">message</text></g></svg>

Before

Width:  |  Height:  |  Size: 915 B

After

Width:  |  Height:  |  Size: 909 B

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="60" height="20" role="img" aria-label="message"><title>message</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="60" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="0" height="20" fill="#FFFFFF"/><rect x="0" width="60" height="20" fill="#FFFFFF"/><rect width="60" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="300" y="150" fill="#ccc" fill-opacity=".3" transform="scale(.1)" textLength="510">message</text><text x="300" y="140" transform="scale(.1)" fill="#333" textLength="510">message</text></g></svg> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="60" height="20" role="img" aria-label="message"><title>message</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="60" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="0" height="20" fill="#fff"/><rect x="0" width="60" height="20" fill="#fff"/><rect width="60" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="300" y="150" fill="#ccc" fill-opacity=".3" transform="scale(.1)" textLength="510">message</text><text x="300" y="140" transform="scale(.1)" fill="#333" textLength="510">message</text></g></svg>

Before

Width:  |  Height:  |  Size: 912 B

After

Width:  |  Height:  |  Size: 906 B

Loading…
Cancel
Save