coverage/pill.go

86 lines
2.2 KiB
Go

package main
import (
"bytes"
"fmt"
"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>`
const (
baseColour = "#555"
modifier float64 = 0.2
maxValue = 220
blue = 55
)
//Pill used for template
type CoveragePill struct {
Percentage string
BaseColour string
FillColour string
}
func unknownCoveragePill() string {
pill := CoveragePill{
"?",
baseColour,
baseColour, // Using the base colour for fill colour because unknown
}
buf := &bytes.Buffer{}
baseTemplate.Execute(buf, pill)
return buf.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 toOneDecimal(num float64) string {
output := math.Pow(10, float64(5))
return fmt.Sprintf("%.1f", float64(round(num*output))/output)
}