coverage/pill.go

64 lines
1.8 KiB
Go

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
}