vertical flags

This commit is contained in:
Ted Unangst 2020-05-22 23:34:32 -04:00
parent 46238c0ec4
commit b00eccf427
1 changed files with 37 additions and 10 deletions

View File

@ -31,11 +31,19 @@ func bloat_showflag(writer http.ResponseWriter, req *http.Request) {
code := mux.Vars(req)["code"] code := mux.Vars(req)["code"]
colors := strings.Split(code, ",") colors := strings.Split(code, ",")
numcolors := len(colors) numcolors := len(colors)
h := (128 / numcolors) * numcolors vert := false
w := h * 3 / 2 if colors[0] == "vert" {
img := image.NewRGBA(image.Rect(0, 0, w, h)) vert = true
for i := 0; i < h; i++ { colors = colors[1:]
hex := colors[i*numcolors/h] numcolors--
if numcolors == 0 {
http.Error(writer, "bad flag", 400)
return
}
}
pixels := make([][4]byte, numcolors)
for i := 0; i < numcolors; i++ {
hex := colors[i]
if len(hex) == 3 { if len(hex) == 3 {
hex = fmt.Sprintf("%c%c%c%c%c%c", hex = fmt.Sprintf("%c%c%c%c%c%c",
hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]) hex[0], hex[0], hex[1], hex[1], hex[2], hex[2])
@ -44,14 +52,33 @@ func bloat_showflag(writer http.ResponseWriter, req *http.Request) {
r := byte(c >> 16 & 0xff) r := byte(c >> 16 & 0xff)
g := byte(c >> 8 & 0xff) g := byte(c >> 8 & 0xff)
b := byte(c >> 0 & 0xff) b := byte(c >> 0 & 0xff)
pixels[i][0] = r
pixels[i][1] = g
pixels[i][2] = b
pixels[i][3] = 255
}
h := 128
w := h * 3 / 2
img := image.NewRGBA(image.Rect(0, 0, w, h))
if vert {
for j := 0; j < w; j++ {
pix := pixels[j*numcolors/w][:]
for i := 0; i < h; i++ {
p := i*img.Stride + j*4
copy(img.Pix[p:], pix)
}
}
} else {
for i := 0; i < h; i++ {
pix := pixels[i*numcolors/h][:]
for j := 0; j < w; j++ { for j := 0; j < w; j++ {
p := i*img.Stride + j*4 p := i*img.Stride + j*4
img.Pix[p+0] = r copy(img.Pix[p:], pix)
img.Pix[p+1] = g
img.Pix[p+2] = b
img.Pix[p+3] = 255
} }
} }
}
writer.Header().Set("Cache-Control", "max-age="+somedays()) writer.Header().Set("Cache-Control", "max-age="+somedays())
png.Encode(writer, img) png.Encode(writer, img)
} }