Prevent possible invalid colors.

They're only theoretical, but better safe than sad.
This commit is contained in:
Thaddee Tyl 2014-11-19 09:40:30 +01:00
parent 2e868d1e12
commit ecaf11a50e
5 changed files with 19 additions and 15 deletions

View File

@ -52,20 +52,23 @@ function makeImage(data, cb) {
if (!(data.template + '-' + data.format in templates)) {
data.template = 'default';
}
if (data.colorscheme) {
var pickedColorscheme = colorscheme[data.colorscheme];
if (!pickedColorscheme) {
pickedColorscheme = colorscheme.red;
}
data.colorA = pickedColorscheme.colorA;
data.colorB = pickedColorscheme.colorB;
}
// String coercion.
data.text[0] = '' + data.text[0];
data.text[1] = '' + data.text[1];
var template = templates[data.template + '-' + data.format];
if (data.colorscheme) {
data.colorA = colorscheme[data.colorscheme].colorA;
data.colorB = colorscheme[data.colorscheme].colorB;
}
data.widths = [
(canvasContext.measureText(data.text[0]).width|0) + 10,
(canvasContext.measureText(data.text[1]).width|0) + 10,
];
var template = templates[data.template + '-' + data.format];
addEscapers(data);
try {
var result = template(data);

View File

@ -2453,6 +2453,7 @@ function(data, match, end, ask) {
}
badge(badgeData, makeSend(format, ask.res, end));
} catch(e) {
console.error(e.stack);
badge({text: ['error', 'bad badge'], colorscheme: 'red'},
makeSend(format, ask.res, end));
}

View File

@ -5,9 +5,9 @@
<stop offset=".9" stop-color="#000" stop-opacity=".3"/>
<stop offset="1" stop-color="#000" stop-opacity=".5"/>
</linearGradient>
<rect rx="4" width="{{=it.widths[0]+it.widths[1]}}" height="18" fill="{{=it.colorA||"#555"}}"/>
<rect rx="4" x="{{=it.widths[0]}}" width="{{=it.widths[1]}}" height="18" fill="{{=it.colorB||"#4c1"}}"/>
<rect x="{{=it.widths[0]}}" width="4" height="18" fill="{{=it.colorB||"#4c1"}}"/>
<rect rx="4" width="{{=it.widths[0]+it.widths[1]}}" height="18" fill="{{=it.escapeXml(it.colorA||"#555")}}"/>
<rect rx="4" x="{{=it.widths[0]}}" width="{{=it.widths[1]}}" height="18" fill="{{=it.escapeXml(it.colorB||"#4c1")}}"/>
<rect x="{{=it.widths[0]}}" width="4" height="18" fill="{{=it.escapeXml(it.colorB||"#4c1")}}"/>
<rect rx="4" width="{{=it.widths[0]+it.widths[1]}}" height="18" fill="url(#smooth)"/>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="{{=it.widths[0]/2+1}}" y="14" fill="#010101" fill-opacity=".3">{{=it.escapeXml(it.text[0])}}</text>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1,7 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" width="{{=it.widths[0]+it.widths[1]}}" height="20">
<rect width="{{=it.widths[0]+it.widths[1]}}" height="20" fill="{{=it.colorA||"#555"}}"/>
<rect x="{{=it.widths[0]}}" width="{{=it.widths[1]}}" height="20" fill="{{=it.colorB||"#4c1"}}"/>
<rect x="{{=it.widths[0]}}" width="4" height="20" fill="{{=it.colorB||"#4c1"}}"/>
<rect width="{{=it.widths[0]+it.widths[1]}}" height="20" fill="{{=it.escapeXml(it.colorA||"#555")}}"/>
<rect x="{{=it.widths[0]}}" width="{{=it.widths[1]}}" height="20" fill="{{=it.escapeXml(it.colorB||"#4c1")}}"/>
<rect x="{{=it.widths[0]}}" width="4" height="20" fill="{{=it.escapeXml(it.colorB||"#4c1")}}"/>
<rect width="{{=it.widths[0]+it.widths[1]}}" height="20" fill-opacity=".1"/>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="{{=it.widths[0]/2+1}}" y="14">{{=it.escapeXml(it.text[0])}}</text>

Before

Width:  |  Height:  |  Size: 739 B

After

Width:  |  Height:  |  Size: 781 B

View File

@ -3,9 +3,9 @@
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<rect rx="3" width="{{=it.widths[0]+it.widths[1]}}" height="20" fill="{{=it.colorA||"#555"}}"/>
<rect rx="3" x="{{=it.widths[0]}}" width="{{=it.widths[1]}}" height="20" fill="{{=it.colorB||"#4c1"}}"/>
<rect x="{{=it.widths[0]}}" width="4" height="20" fill="{{=it.colorB||"#4c1"}}"/>
<rect rx="3" width="{{=it.widths[0]+it.widths[1]}}" height="20" fill="{{=it.escapeXml(it.colorA||"#555")}}"/>
<rect rx="3" x="{{=it.widths[0]}}" width="{{=it.widths[1]}}" height="20" fill="{{=it.escapeXml(it.colorB||"#4c1")}}"/>
<rect x="{{=it.widths[0]}}" width="4" height="20" fill="{{=it.escapeXml(it.colorB||"#4c1")}}"/>
<rect rx="3" width="{{=it.widths[0]+it.widths[1]}}" height="20" fill="url(#smooth)"/>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
<text x="{{=it.widths[0]/2+1}}" y="15" fill="#010101" fill-opacity=".3">{{=it.escapeXml(it.text[0])}}</text>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB