Skip to content

Instantly share code, notes, and snippets.

@gerardpaapu
Created July 20, 2011 01:54
Show Gist options
  • Save gerardpaapu/1094170 to your computer and use it in GitHub Desktop.
Save gerardpaapu/1094170 to your computer and use it in GitHub Desktop.
A mini servlet to serve svg gradients
#lang racket
(require xml
net/url
web-server/servlet
web-server/servlet-env)
(define (svg-response svg)
(make-response/full
200 #"OK"
(current-seconds) #"image/svg+xml; charset=utf-8"
'()
(list #"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
#"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\""
#" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
(string->bytes/utf-8 (xexpr->string svg)))))
(define (svg-gradient start stop)
`(svg ([xmlns "http://www.w3.org/2000/svg"]
[preserveAspectRatio "none"]
[version "1.1"]
[width "100%"]
[height "100%"])
(defs
(linearGradient ([id "l"]
[gradientUnits "userSpaceOnUse"]
[x1 "0%"] [y1 "0%"]
[x2 "0%"] [y2 "100%"]
[spreadMethod "pad"])
(stop ([offset "0%"]
[stop-color ,start]
[stop-opacity "1"]))
(stop ([offset "100%"]
[stop-color ,stop]
[stop-opacity "1"]))))
(rect ([width "100%"]
[height "100%"]
[style "fill: url('#l');"]))))
(serve/servlet (lambda (req)
(let* ([uri (request-uri req)]
[q (url-query uri)]
[start (dict-ref q 'start "000")]
[end (dict-ref q 'end "FFF")])
(display (url->string uri))
(svg-response (svg-gradient (format "#~a" start)
(format "#~a" end)))))
#:servlet-path "/"
#:launch-browser? #f
#:listen-ip #f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment