Implementation of creating pill and printing to StdOut.

pull/1/head
ALI Hamza 2019-08-12 14:47:26 +07:00
commit b2b8268532
No known key found for this signature in database
GPG Key ID: 7C608266BF384ADC
3 changed files with 116 additions and 0 deletions

@ -0,0 +1,33 @@
package main
import (
"fmt"
"os"
"text/template"
)
const (
baseColour = "#555"
modifier float64 = 0.2
maxValue = 220
blue = 55
)
func main() {
t := template.New("svgConv")
percentage := 0.999
fill := percentageToRGB(percentage)
pill := CoveragePill{
fmt.Sprintf("%.1f", toFixed(percentage, 3)*100),
baseColour,
fill,
}
t, err := t.Parse(svgTemplate)
if err != nil {
panic(err)
}
_ = t.Execute(os.Stdout, pill)
}

@ -0,0 +1,63 @@
package main
import (
"math"
"strconv"
)
const svgTemplate string = `<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="108" height="20" version="1.1">
<linearGradient x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="108" height="20" rx="12" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path d="M0 0h66v20H0z" fill="{{.BaseColour}}"/>
<path d="M66 0h44v20H66z" fill="{{.FillColour}}"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="34" y="14" fill="#010101" fill-opacity="0.5">coverage</text>
<text x="34" y="13">coverage</text>
<text x="87" y="15" fill="#010101" fill-opacity=".5">{{.Percentage}}%</text>
<text x="87" y="14">{{.Percentage}}%</text>
</g>
</svg>`
type CoveragePill struct {
Percentage string
BaseColour string
FillColour string
}
func percentageToRGB(percentage float64) string {
red := modifier + clamp(2-2*math.Pow(percentage, 2), 0, 1)*(1-modifier)
green := modifier + clamp(2*math.Pow(percentage, 2), 0, 1)*(1-modifier)
redValue := strconv.Itoa(int(red * maxValue))
greenValue := strconv.Itoa(int(green * maxValue))
blueValue := strconv.Itoa(blue)
return "rgb(" + redValue + ", " + greenValue + ", " + blueValue + ")"
}
func clamp(operation float64, min float64, max float64) float64 {
if operation < min {
return min
}
if operation > max {
return max
}
return operation
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="108" height="20" version="1.1">
<linearGradient x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<mask id="a">
<rect width="108" height="20" rx="12" fill="#fff"/>
</mask>
<g mask="url(#a)">
<path d="M0 0h66v20H0z" fill="#555"/>
<path d="M66 0h44v20H66z" fill="rgb(49, 220, 55)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="34" y="14" fill="#010101" fill-opacity="0.5">coverage</text>
<text x="34" y="13">coverage</text>
<text x="87" y="15" fill="#010101" fill-opacity=".5">99.9%</text>
<text x="87" y="14">99.9%</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 875 B