svg.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. // Package svg provides an API for generating Scalable Vector Graphics (SVG)
  2. package svg
  3. // package main
  4. //
  5. // import (
  6. // "github.com/ajstarks/svgo"
  7. // "os"
  8. // )
  9. //
  10. // var (
  11. // width = 500
  12. // height = 500
  13. // canvas = svg.New(os.Stdout)
  14. // )
  15. //
  16. // func main() {
  17. // canvas.Start(width, height)
  18. // canvas.Circle(width/2, height/2, 100)
  19. // canvas.Text(width/2, height/2, "Hello, SVG",
  20. // "text-anchor:middle;font-size:30px;fill:white")
  21. // canvas.End()
  22. // }
  23. //
  24. import (
  25. "fmt"
  26. "io"
  27. "encoding/xml"
  28. "strings"
  29. )
  30. // SVG defines the location of the generated SVG
  31. type SVG struct {
  32. Writer io.Writer
  33. }
  34. // Offcolor defines the offset and color for gradients
  35. type Offcolor struct {
  36. Offset uint8
  37. Color string
  38. Opacity float64
  39. }
  40. // Filterspec defines the specification of SVG filters
  41. type Filterspec struct {
  42. In, In2, Result string
  43. }
  44. const (
  45. svgtop = `<?xml version="1.0"?>
  46. <!-- Generated by SVGo -->
  47. <svg`
  48. svginitfmt = `%s width="%d%s" height="%d%s"`
  49. svgns = `
  50. xmlns="http://www.w3.org/2000/svg"
  51. xmlns:xlink="http://www.w3.org/1999/xlink">`
  52. vbfmt = `viewBox="%d %d %d %d"`
  53. emptyclose = "/>\n"
  54. )
  55. // New is the SVG constructor, specifying the io.Writer where the generated SVG is written.
  56. func New(w io.Writer) *SVG { return &SVG{w} }
  57. func (svg *SVG) print(a ...interface{}) (n int, errno error) {
  58. return fmt.Fprint(svg.Writer, a...)
  59. }
  60. func (svg *SVG) println(a ...interface{}) (n int, errno error) {
  61. return fmt.Fprintln(svg.Writer, a...)
  62. }
  63. func (svg *SVG) printf(format string, a ...interface{}) (n int, errno error) {
  64. return fmt.Fprintf(svg.Writer, format, a...)
  65. }
  66. func (svg *SVG) genattr(ns []string) {
  67. for _, v := range ns {
  68. svg.printf("\n %s", v)
  69. }
  70. svg.println(svgns)
  71. }
  72. // Structure, Metadata, Scripting, Style, Transformation, and Links
  73. // Start begins the SVG document with the width w and height h.
  74. // Other attributes may be optionally added, for example viewbox or additional namespaces
  75. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#SVGElement
  76. func (svg *SVG) Start(w int, h int, ns ...string) {
  77. svg.printf(svginitfmt, svgtop, w, "", h, "")
  78. svg.genattr(ns)
  79. }
  80. // Startunit begins the SVG document, with width and height in the specified units
  81. // Other attributes may be optionally added, for example viewbox or additional namespaces
  82. func (svg *SVG) Startunit(w int, h int, unit string, ns ...string) {
  83. svg.printf(svginitfmt, svgtop, w, unit, h, unit)
  84. svg.genattr(ns)
  85. }
  86. // Startpercent begins the SVG document, with width and height as percentages
  87. // Other attributes may be optionally added, for example viewbox or additional namespaces
  88. func (svg *SVG) Startpercent(w int, h int, ns ...string) {
  89. svg.printf(svginitfmt, svgtop, w, "%", h, "%")
  90. svg.genattr(ns)
  91. }
  92. // Startview begins the SVG document, with the specified width, height, and viewbox
  93. // Other attributes may be optionally added, for example viewbox or additional namespaces
  94. func (svg *SVG) Startview(w, h, minx, miny, vw, vh int) {
  95. svg.Start(w, h, fmt.Sprintf(vbfmt, minx, miny, vw, vh))
  96. }
  97. func (svg *SVG) StartviewUnit(w, h int, unit string, minx, miny, vw, vh int) {
  98. svg.Startunit(w, h, unit, fmt.Sprintf(vbfmt, minx, miny, vw, vh))
  99. }
  100. // Startraw begins the SVG document, passing arbitrary attributes
  101. func (svg *SVG) Startraw(ns ...string) {
  102. svg.printf(svgtop)
  103. svg.genattr(ns)
  104. }
  105. // End the SVG document
  106. func (svg *SVG) End() { svg.println("</svg>") }
  107. // linkembed defines an element with a specified type,
  108. // (for example "application/javascript", or "text/css").
  109. // if the first variadic argument is a link, use only the link reference.
  110. // Otherwise, treat those arguments as the text of the script (marked up as CDATA).
  111. // if no data is specified, just close the element
  112. func (svg *SVG) linkembed(tag string, scriptype string, data ...string) {
  113. svg.printf(`<%s type="%s"`, tag, scriptype)
  114. switch {
  115. case len(data) == 1 && islink(data[0]):
  116. svg.printf(" %s/>\n", href(data[0]))
  117. case len(data) > 0:
  118. svg.printf(">\n<![CDATA[\n")
  119. for _, v := range data {
  120. svg.println(v)
  121. }
  122. svg.printf("]]>\n</%s>\n", tag)
  123. default:
  124. svg.println(`/>`)
  125. }
  126. }
  127. // Script defines a script with a specified type, (for example "application/javascript").
  128. func (svg *SVG) Script(scriptype string, data ...string) {
  129. svg.linkembed("script", scriptype, data...)
  130. }
  131. // Style defines the specified style (for example "text/css")
  132. func (svg *SVG) Style(scriptype string, data ...string) {
  133. svg.linkembed("style", scriptype, data...)
  134. }
  135. // Gstyle begins a group, with the specified style.
  136. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#GElement
  137. func (svg *SVG) Gstyle(s string) { svg.println(group("style", s)) }
  138. // Gtransform begins a group, with the specified transform
  139. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  140. func (svg *SVG) Gtransform(s string) { svg.println(group("transform", s)) }
  141. // Translate begins coordinate translation, end with Gend()
  142. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  143. func (svg *SVG) Translate(x, y int) { svg.Gtransform(translate(x, y)) }
  144. // Scale scales the coordinate system by n, end with Gend()
  145. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  146. func (svg *SVG) Scale(n float64) { svg.Gtransform(scale(n)) }
  147. // ScaleXY scales the coordinate system by dx and dy, end with Gend()
  148. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  149. func (svg *SVG) ScaleXY(dx, dy float64) { svg.Gtransform(scaleXY(dx, dy)) }
  150. // SkewX skews the x coordinate system by angle a, end with Gend()
  151. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  152. func (svg *SVG) SkewX(a float64) { svg.Gtransform(skewX(a)) }
  153. // SkewY skews the y coordinate system by angle a, end with Gend()
  154. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  155. func (svg *SVG) SkewY(a float64) { svg.Gtransform(skewY(a)) }
  156. // SkewXY skews x and y coordinates by ax, ay respectively, end with Gend()
  157. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  158. func (svg *SVG) SkewXY(ax, ay float64) { svg.Gtransform(skewX(ax) + " " + skewY(ay)) }
  159. // Rotate rotates the coordinate system by r degrees, end with Gend()
  160. // Standard Reference: http://www.w3.org/TR/SVG11/coords.html#TransformAttribute
  161. func (svg *SVG) Rotate(r float64) { svg.Gtransform(rotate(r)) }
  162. // TranslateRotate translates the coordinate system to (x,y), then rotates to r degrees, end with Gend()
  163. func (svg *SVG) TranslateRotate(x, y int, r float64) {
  164. svg.Gtransform(translate(x, y) + " " + rotate(r))
  165. }
  166. // RotateTranslate rotates the coordinate system r degrees, then translates to (x,y), end with Gend()
  167. func (svg *SVG) RotateTranslate(x, y int, r float64) {
  168. svg.Gtransform(rotate(r) + " " + translate(x, y))
  169. }
  170. // Group begins a group with arbitrary attributes
  171. func (svg *SVG) Group(s ...string) { svg.printf("<g %s\n", endstyle(s, `>`)) }
  172. // Gid begins a group, with the specified id
  173. func (svg *SVG) Gid(s string) {
  174. svg.print(`<g id="`)
  175. xml.Escape(svg.Writer, []byte(s))
  176. svg.println(`">`)
  177. }
  178. // Gend ends a group (must be paired with Gsttyle, Gtransform, Gid).
  179. func (svg *SVG) Gend() { svg.println(`</g>`) }
  180. // ClipPath defines a clip path
  181. func (svg *SVG) ClipPath(s ...string) { svg.printf(`<clipPath %s`, endstyle(s, `>`)) }
  182. // ClipEnd ends a ClipPath
  183. func (svg *SVG) ClipEnd() {
  184. svg.println(`</clipPath>`)
  185. }
  186. // Def begins a defintion block.
  187. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#DefsElement
  188. func (svg *SVG) Def() { svg.println(`<defs>`) }
  189. // DefEnd ends a defintion block.
  190. func (svg *SVG) DefEnd() { svg.println(`</defs>`) }
  191. // Marker defines a marker
  192. // Standard reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement
  193. func (svg *SVG) Marker(id string, x, y, width, height int, s ...string) {
  194. svg.printf(`<marker id="%s" refX="%d" refY="%d" markerWidth="%d" markerHeight="%d" %s`,
  195. id, x, y, width, height, endstyle(s, ">\n"))
  196. }
  197. // MarkEnd ends a marker
  198. func (svg *SVG) MarkerEnd() { svg.println(`</marker>`) }
  199. // Pattern defines a pattern with the specified dimensions.
  200. // The putype can be either "user" or "obj", which sets the patternUnits
  201. // attribute to be either userSpaceOnUse or objectBoundingBox
  202. // Standard reference: http://www.w3.org/TR/SVG11/pservers.html#Patterns
  203. func (svg *SVG) Pattern(id string, x, y, width, height int, putype string, s ...string) {
  204. puattr := "userSpaceOnUse"
  205. if putype != "user" {
  206. puattr = "objectBoundingBox"
  207. }
  208. svg.printf(`<pattern id="%s" x="%d" y="%d" width="%d" height="%d" patternUnits="%s" %s`,
  209. id, x, y, width, height, puattr, endstyle(s, ">\n"))
  210. }
  211. // PatternEnd ends a marker
  212. func (svg *SVG) PatternEnd() { svg.println(`</pattern>`) }
  213. // Desc specified the text of the description tag.
  214. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#DescElement
  215. func (svg *SVG) Desc(s string) { svg.tt("desc", s) }
  216. // Title specified the text of the title tag.
  217. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#TitleElement
  218. func (svg *SVG) Title(s string) { svg.tt("title", s) }
  219. // Link begins a link named "name", with the specified title.
  220. // Standard Reference: http://www.w3.org/TR/SVG11/linking.html#Links
  221. func (svg *SVG) Link(href string, title string) {
  222. svg.printf("<a xlink:href=\"%s\" xlink:title=\"", href)
  223. xml.Escape(svg.Writer, []byte(title))
  224. svg.println("\">")
  225. }
  226. // LinkEnd ends a link.
  227. func (svg *SVG) LinkEnd() { svg.println(`</a>`) }
  228. // Use places the object referenced at link at the location x, y, with optional style.
  229. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#UseElement
  230. func (svg *SVG) Use(x int, y int, link string, s ...string) {
  231. svg.printf(`<use %s %s %s`, loc(x, y), href(link), endstyle(s, emptyclose))
  232. }
  233. // Mask creates a mask with a specified id, dimension, and optional style.
  234. func (svg *SVG) Mask(id string, x int, y int, w int, h int, s ...string) {
  235. svg.printf(`<mask id="%s" x="%d" y="%d" width="%d" height="%d" %s`, id, x, y, w, h, endstyle(s, `>`))
  236. }
  237. // MaskEnd ends a Mask.
  238. func (svg *SVG) MaskEnd() { svg.println(`</mask>`) }
  239. // Shapes
  240. // Circle centered at x,y, with radius r, with optional style.
  241. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#CircleElement
  242. func (svg *SVG) Circle(x int, y int, r int, s ...string) {
  243. svg.printf(`<circle cx="%d" cy="%d" r="%d" %s`, x, y, r, endstyle(s, emptyclose))
  244. }
  245. // Ellipse centered at x,y, centered at x,y with radii w, and h, with optional style.
  246. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#EllipseElement
  247. func (svg *SVG) Ellipse(x int, y int, w int, h int, s ...string) {
  248. svg.printf(`<ellipse cx="%d" cy="%d" rx="%d" ry="%d" %s`,
  249. x, y, w, h, endstyle(s, emptyclose))
  250. }
  251. // Polygon draws a series of line segments using an array of x, y coordinates, with optional style.
  252. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#PolygonElement
  253. func (svg *SVG) Polygon(x []int, y []int, s ...string) {
  254. svg.poly(x, y, "polygon", s...)
  255. }
  256. // Rect draws a rectangle with upper left-hand corner at x,y, with width w, and height h, with optional style
  257. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#RectElement
  258. func (svg *SVG) Rect(x int, y int, w int, h int, s ...string) {
  259. svg.printf(`<rect %s %s`, dim(x, y, w, h), endstyle(s, emptyclose))
  260. }
  261. // CenterRect draws a rectangle with its center at x,y, with width w, and height h, with optional style
  262. func (svg *SVG) CenterRect(x int, y int, w int, h int, s ...string) {
  263. svg.Rect(x-(w/2), y-(h/2), w, h, s...)
  264. }
  265. // Roundrect draws a rounded rectangle with upper the left-hand corner at x,y,
  266. // with width w, and height h. The radii for the rounded portion
  267. // are specified by rx (width), and ry (height).
  268. // Style is optional.
  269. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#RectElement
  270. func (svg *SVG) Roundrect(x int, y int, w int, h int, rx int, ry int, s ...string) {
  271. svg.printf(`<rect %s rx="%d" ry="%d" %s`, dim(x, y, w, h), rx, ry, endstyle(s, emptyclose))
  272. }
  273. // Square draws a square with upper left corner at x,y with sides of length l, with optional style.
  274. func (svg *SVG) Square(x int, y int, l int, s ...string) {
  275. svg.Rect(x, y, l, l, s...)
  276. }
  277. // Paths
  278. // Path draws an arbitrary path, the caller is responsible for structuring the path data
  279. func (svg *SVG) Path(d string, s ...string) {
  280. svg.printf(`<path d="%s" %s`, d, endstyle(s, emptyclose))
  281. }
  282. // Arc draws an elliptical arc, with optional style, beginning coordinate at sx,sy, ending coordinate at ex, ey
  283. // width and height of the arc are specified by ax, ay, the x axis rotation is r
  284. // if sweep is true, then the arc will be drawn in a "positive-angle" direction (clockwise), if false,
  285. // the arc is drawn counterclockwise.
  286. // if large is true, the arc sweep angle is greater than or equal to 180 degrees,
  287. // otherwise the arc sweep is less than 180 degrees
  288. // http://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands
  289. func (svg *SVG) Arc(sx int, sy int, ax int, ay int, r int, large bool, sweep bool, ex int, ey int, s ...string) {
  290. svg.printf(`%s A%s %d %s %s %s" %s`,
  291. ptag(sx, sy), coord(ax, ay), r, onezero(large), onezero(sweep), coord(ex, ey), endstyle(s, emptyclose))
  292. }
  293. // Bezier draws a cubic bezier curve, with optional style, beginning at sx,sy, ending at ex,ey
  294. // with control points at cx,cy and px,py.
  295. // Standard Reference: http://www.w3.org/TR/SVG11/paths.html#PathDataCubicBezierCommands
  296. func (svg *SVG) Bezier(sx int, sy int, cx int, cy int, px int, py int, ex int, ey int, s ...string) {
  297. svg.printf(`%s C%s %s %s" %s`,
  298. ptag(sx, sy), coord(cx, cy), coord(px, py), coord(ex, ey), endstyle(s, emptyclose))
  299. }
  300. // Qbez draws a quadratic bezier curver, with optional style
  301. // beginning at sx,sy, ending at ex, sy with control points at cx, cy
  302. // Standard Reference: http://www.w3.org/TR/SVG11/paths.html#PathDataQuadraticBezierCommands
  303. func (svg *SVG) Qbez(sx int, sy int, cx int, cy int, ex int, ey int, s ...string) {
  304. svg.printf(`%s Q%s %s" %s`,
  305. ptag(sx, sy), coord(cx, cy), coord(ex, ey), endstyle(s, emptyclose))
  306. }
  307. // Qbezier draws a Quadratic Bezier curve, with optional style, beginning at sx, sy, ending at tx,ty
  308. // with control points are at cx,cy, ex,ey.
  309. // Standard Reference: http://www.w3.org/TR/SVG11/paths.html#PathDataQuadraticBezierCommands
  310. func (svg *SVG) Qbezier(sx int, sy int, cx int, cy int, ex int, ey int, tx int, ty int, s ...string) {
  311. svg.printf(`%s Q%s %s T%s" %s`,
  312. ptag(sx, sy), coord(cx, cy), coord(ex, ey), coord(tx, ty), endstyle(s, emptyclose))
  313. }
  314. // Lines
  315. // Line draws a straight line between two points, with optional style.
  316. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#LineElement
  317. func (svg *SVG) Line(x1 int, y1 int, x2 int, y2 int, s ...string) {
  318. svg.printf(`<line x1="%d" y1="%d" x2="%d" y2="%d" %s`, x1, y1, x2, y2, endstyle(s, emptyclose))
  319. }
  320. // Polyline draws connected lines between coordinates, with optional style.
  321. // Standard Reference: http://www.w3.org/TR/SVG11/shapes.html#PolylineElement
  322. func (svg *SVG) Polyline(x []int, y []int, s ...string) {
  323. svg.poly(x, y, "polyline", s...)
  324. }
  325. // Image places at x,y (upper left hand corner), the image with
  326. // width w, and height h, referenced at link, with optional style.
  327. // Standard Reference: http://www.w3.org/TR/SVG11/struct.html#ImageElement
  328. func (svg *SVG) Image(x int, y int, w int, h int, link string, s ...string) {
  329. svg.printf(`<image %s %s %s`, dim(x, y, w, h), href(link), endstyle(s, emptyclose))
  330. }
  331. // Text places the specified text, t at x,y according to the style specified in s
  332. // Standard Reference: http://www.w3.org/TR/SVG11/text.html#TextElement
  333. func (svg *SVG) Text(x int, y int, t string, s ...string) {
  334. svg.printf(`<text %s %s`, loc(x, y), endstyle(s, ">"))
  335. xml.Escape(svg.Writer, []byte(t))
  336. svg.println(`</text>`)
  337. }
  338. // Textpath places text optionally styled text along a previously defined path
  339. // Standard Reference: http://www.w3.org/TR/SVG11/text.html#TextPathElement
  340. func (svg *SVG) Textpath(t string, pathid string, s ...string) {
  341. svg.printf("<text %s<textPath xlink:href=\"%s\">", endstyle(s, ">"), pathid)
  342. xml.Escape(svg.Writer, []byte(t))
  343. svg.println(`</textPath></text>`)
  344. }
  345. // Textlines places a series of lines of text starting at x,y, at the specified size, fill, and alignment.
  346. // Each line is spaced according to the spacing argument
  347. func (svg *SVG) Textlines(x, y int, s []string, size, spacing int, fill, align string) {
  348. svg.Gstyle(fmt.Sprintf("font-size:%dpx;fill:%s;text-anchor:%s", size, fill, align))
  349. for _, t := range s {
  350. svg.Text(x, y, t)
  351. y += spacing
  352. }
  353. svg.Gend()
  354. }
  355. // Colors
  356. // RGB specifies a fill color in terms of a (r)ed, (g)reen, (b)lue triple.
  357. // Standard reference: http://www.w3.org/TR/css3-color/
  358. func (svg *SVG) RGB(r int, g int, b int) string {
  359. return fmt.Sprintf(`fill:rgb(%d,%d,%d)`, r, g, b)
  360. }
  361. // RGBA specifies a fill color in terms of a (r)ed, (g)reen, (b)lue triple and opacity.
  362. func (svg *SVG) RGBA(r int, g int, b int, a float64) string {
  363. return fmt.Sprintf(`fill-opacity:%.2f; %s`, a, svg.RGB(r, g, b))
  364. }
  365. // Gradients
  366. // LinearGradient constructs a linear color gradient identified by id,
  367. // along the vector defined by (x1,y1), and (x2,y2).
  368. // The stop color sequence defined in sc. Coordinates are expressed as percentages.
  369. func (svg *SVG) LinearGradient(id string, x1, y1, x2, y2 uint8, sc []Offcolor) {
  370. svg.printf("<linearGradient id=\"%s\" x1=\"%d%%\" y1=\"%d%%\" x2=\"%d%%\" y2=\"%d%%\">\n",
  371. id, pct(x1), pct(y1), pct(x2), pct(y2))
  372. svg.stopcolor(sc)
  373. svg.println("</linearGradient>")
  374. }
  375. // RadialGradient constructs a radial color gradient identified by id,
  376. // centered at (cx,cy), with a radius of r.
  377. // (fx, fy) define the location of the focal point of the light source.
  378. // The stop color sequence defined in sc.
  379. // Coordinates are expressed as percentages.
  380. func (svg *SVG) RadialGradient(id string, cx, cy, r, fx, fy uint8, sc []Offcolor) {
  381. svg.printf("<radialGradient id=\"%s\" cx=\"%d%%\" cy=\"%d%%\" r=\"%d%%\" fx=\"%d%%\" fy=\"%d%%\">\n",
  382. id, pct(cx), pct(cy), pct(r), pct(fx), pct(fy))
  383. svg.stopcolor(sc)
  384. svg.println("</radialGradient>")
  385. }
  386. // stopcolor is a utility function used by the gradient functions
  387. // to define a sequence of offsets (expressed as percentages) and colors
  388. func (svg *SVG) stopcolor(oc []Offcolor) {
  389. for _, v := range oc {
  390. svg.printf("<stop offset=\"%d%%\" stop-color=\"%s\" stop-opacity=\"%.2f\"/>\n",
  391. pct(v.Offset), v.Color, v.Opacity)
  392. }
  393. }
  394. // Filter Effects:
  395. // Most functions have common attributes (in, in2, result) defined in type Filterspec
  396. // used as a common first argument.
  397. // Filter begins a filter set
  398. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#FilterElement
  399. func (svg *SVG) Filter(id string, s ...string) {
  400. svg.printf(`<filter id="%s" %s`, id, endstyle(s, ">\n"))
  401. }
  402. // Fend ends a filter set
  403. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#FilterElement
  404. func (svg *SVG) Fend() {
  405. svg.println(`</filter>`)
  406. }
  407. // FeBlend specifies a Blend filter primitive
  408. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feBlendElement
  409. func (svg *SVG) FeBlend(fs Filterspec, mode string, s ...string) {
  410. switch mode {
  411. case "normal", "multiply", "screen", "darken", "lighten":
  412. break
  413. default:
  414. mode = "normal"
  415. }
  416. svg.printf(`<feBlend %s mode="%s" %s`,
  417. fsattr(fs), mode, endstyle(s, emptyclose))
  418. }
  419. // FeColorMatrix specifies a color matrix filter primitive, with matrix values
  420. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
  421. func (svg *SVG) FeColorMatrix(fs Filterspec, values [20]float64, s ...string) {
  422. svg.printf(`<feColorMatrix %s type="matrix" values="`, fsattr(fs))
  423. for _, v := range values {
  424. svg.printf(`%g `, v)
  425. }
  426. svg.printf(`" %s`, endstyle(s, emptyclose))
  427. }
  428. // FeColorMatrixHue specifies a color matrix filter primitive, with hue rotation values
  429. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
  430. func (svg *SVG) FeColorMatrixHue(fs Filterspec, value float64, s ...string) {
  431. if value < -360 || value > 360 {
  432. value = 0
  433. }
  434. svg.printf(`<feColorMatrix %s type="hueRotate" values="%g" %s`,
  435. fsattr(fs), value, endstyle(s, emptyclose))
  436. }
  437. // FeColorMatrixSaturate specifies a color matrix filter primitive, with saturation values
  438. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
  439. func (svg *SVG) FeColorMatrixSaturate(fs Filterspec, value float64, s ...string) {
  440. if value < 0 || value > 1 {
  441. value = 1
  442. }
  443. svg.printf(`<feColorMatrix %s type="saturate" values="%g" %s`,
  444. fsattr(fs), value, endstyle(s, emptyclose))
  445. }
  446. // FeColorMatrixLuminence specifies a color matrix filter primitive, with luminence values
  447. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
  448. func (svg *SVG) FeColorMatrixLuminence(fs Filterspec, s ...string) {
  449. svg.printf(`<feColorMatrix %s type="luminenceToAlpha" %s`,
  450. fsattr(fs), endstyle(s, emptyclose))
  451. }
  452. // FeComponentTransfer begins a feComponent filter element
  453. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  454. func (svg *SVG) FeComponentTransfer() {
  455. svg.println(`<feComponentTransfer>`)
  456. }
  457. // FeCompEnd ends a feComponent filter element
  458. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  459. func (svg *SVG) FeCompEnd() {
  460. svg.println(`</feComponentTransfer>`)
  461. }
  462. // FeComposite specifies a feComposite filter primitive
  463. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feCompositeElement
  464. func (svg *SVG) FeComposite(fs Filterspec, operator string, k1, k2, k3, k4 int, s ...string) {
  465. switch operator {
  466. case "over", "in", "out", "atop", "xor", "arithmetic":
  467. break
  468. default:
  469. operator = "over"
  470. }
  471. svg.printf(`<feComposite %s operator="%s" k1="%d" k2="%d" k3="%d" k4="%d" %s`,
  472. fsattr(fs), operator, k1, k2, k3, k4, endstyle(s, emptyclose))
  473. }
  474. // FeConvolveMatrix specifies a feConvolveMatrix filter primitive
  475. // Standard referencd: http://www.w3.org/TR/SVG11/filters.html#feConvolveMatrixElement
  476. func (svg *SVG) FeConvolveMatrix(fs Filterspec, matrix [9]int, s ...string) {
  477. svg.printf(`<feConvolveMatrix %s kernelMatrix="%d %d %d %d %d %d %d %d %d" %s`,
  478. fsattr(fs),
  479. matrix[0], matrix[1], matrix[2],
  480. matrix[3], matrix[4], matrix[5],
  481. matrix[6], matrix[7], matrix[8], endstyle(s, emptyclose))
  482. }
  483. // FeDiffuseLighting specifies a diffuse lighting filter primitive,
  484. // a container for light source elements, end with DiffuseEnd()
  485. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  486. func (svg *SVG) FeDiffuseLighting(fs Filterspec, scale, constant float64, s ...string) {
  487. svg.printf(`<feDiffuseLighting %s surfaceScale="%g" diffuseConstant="%g" %s`,
  488. fsattr(fs), scale, constant, endstyle(s, `>`))
  489. }
  490. // FeDiffEnd ends a diffuse lighting filter primitive container
  491. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feDiffuseLightingElement
  492. func (svg *SVG) FeDiffEnd() {
  493. svg.println(`</feDiffuseLighting>`)
  494. }
  495. // FeDisplacementMap specifies a feDisplacementMap filter primitive
  496. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement
  497. func (svg *SVG) FeDisplacementMap(fs Filterspec, scale float64, xchannel, ychannel string, s ...string) {
  498. svg.printf(`<feDisplacementMap %s scale="%g" xChannelSelector="%s" yChannelSelector="%s" %s`,
  499. fsattr(fs), scale, imgchannel(xchannel), ychannel, endstyle(s, emptyclose))
  500. }
  501. // FeDistantLight specifies a feDistantLight filter primitive
  502. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feDistantLightElement
  503. func (svg *SVG) FeDistantLight(fs Filterspec, azimuth, elevation float64, s ...string) {
  504. svg.printf(`<feDistantLight %s azimuth="%g" elevation="%g" %s`,
  505. fsattr(fs), azimuth, elevation, endstyle(s, emptyclose))
  506. }
  507. // FeFlood specifies a flood filter primitive
  508. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feFloodElement
  509. func (svg *SVG) FeFlood(fs Filterspec, color string, opacity float64, s ...string) {
  510. svg.printf(`<feFlood %s flood-fill-color="%s" flood-fill-opacity="%g" %s`,
  511. fsattr(fs), color, opacity, endstyle(s, emptyclose))
  512. }
  513. // FeFunc{linear|Gamma|Table|Discrete} specify various types of feFunc{R|G|B|A} filter primitives
  514. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  515. // FeFuncLinear specifies a linear style function for the feFunc{R|G|B|A} filter element
  516. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  517. func (svg *SVG) FeFuncLinear(channel string, slope, intercept float64) {
  518. svg.printf(`<feFunc%s type="linear" slope="%g" intercept="%g"%s`,
  519. imgchannel(channel), slope, intercept, emptyclose)
  520. }
  521. // FeFuncGamma specifies the curve values for gamma correction for the feFunc{R|G|B|A} filter element
  522. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  523. func (svg *SVG) FeFuncGamma(channel string, amplitude, exponent, offset float64) {
  524. svg.printf(`<feFunc%s type="gamma" amplitude="%g" exponent="%g" offset="%g"%s`,
  525. imgchannel(channel), amplitude, exponent, offset, emptyclose)
  526. }
  527. // FeFuncTable specifies the table of values for the feFunc{R|G|B|A} filter element
  528. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  529. func (svg *SVG) FeFuncTable(channel string, tv []float64) {
  530. svg.printf(`<feFunc%s type="table"`, imgchannel(channel))
  531. svg.tablevalues(`tableValues`, tv)
  532. }
  533. // FeFuncDiscrete specifies the discrete values for the feFunc{R|G|B|A} filter element
  534. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feComponentTransferElement
  535. func (svg *SVG) FeFuncDiscrete(channel string, tv []float64) {
  536. svg.printf(`<feFunc%s type="discrete"`, imgchannel(channel))
  537. svg.tablevalues(`tableValues`, tv)
  538. }
  539. // FeGaussianBlur specifies a Gaussian Blur filter primitive
  540. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement
  541. func (svg *SVG) FeGaussianBlur(fs Filterspec, stdx, stdy float64, s ...string) {
  542. if stdx < 0 {
  543. stdx = 0
  544. }
  545. if stdy < 0 {
  546. stdy = 0
  547. }
  548. svg.printf(`<feGaussianBlur %s stdDeviation="%g %g" %s`,
  549. fsattr(fs), stdx, stdy, endstyle(s, emptyclose))
  550. }
  551. // FeImage specifies a feImage filter primitive
  552. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feImageElement
  553. func (svg *SVG) FeImage(href string, result string, s ...string) {
  554. svg.printf(`<feImage xlink:href="%s" result="%s" %s`,
  555. href, result, endstyle(s, emptyclose))
  556. }
  557. // FeMerge specifies a feMerge filter primitive, containing feMerge elements
  558. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feMergeElement
  559. func (svg *SVG) FeMerge(nodes []string, s ...string) {
  560. svg.println(`<feMerge>`)
  561. for _, n := range nodes {
  562. svg.printf("<feMergeNode in=\"%s\"/>\n", n)
  563. }
  564. svg.println(`</feMerge>`)
  565. }
  566. // FeMorphology specifies a feMorphologyLight filter primitive
  567. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feMorphologyElement
  568. func (svg *SVG) FeMorphology(fs Filterspec, operator string, xradius, yradius float64, s ...string) {
  569. switch operator {
  570. case "erode", "dilate":
  571. break
  572. default:
  573. operator = "erode"
  574. }
  575. svg.printf(`<feMorphology %s operator="%s" radius="%g %g" %s`,
  576. fsattr(fs), operator, xradius, yradius, endstyle(s, emptyclose))
  577. }
  578. // FeOffset specifies the feOffset filter primitive
  579. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feOffsetElement
  580. func (svg *SVG) FeOffset(fs Filterspec, dx, dy int, s ...string) {
  581. svg.printf(`<feOffset %s dx="%d" dy="%d" %s`,
  582. fsattr(fs), dx, dy, endstyle(s, emptyclose))
  583. }
  584. // FePointLight specifies a fePpointLight filter primitive
  585. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#fePointLightElement
  586. func (svg *SVG) FePointLight(x, y, z float64, s ...string) {
  587. svg.printf(`<fePointLight x="%g" y="%g" z="%g" %s`,
  588. x, y, z, endstyle(s, emptyclose))
  589. }
  590. // FeSpecularLighting specifies a specular lighting filter primitive,
  591. // a container for light source elements, end with SpecularEnd()
  592. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feSpecularLightingElement
  593. func (svg *SVG) FeSpecularLighting(fs Filterspec, scale, constant float64, exponent int, color string, s ...string) {
  594. svg.printf(`<feSpecularLighting %s surfaceScale="%g" specularConstant="%g" specularExponent="%d" lighting-color="%s" %s`,
  595. fsattr(fs), scale, constant, exponent, color, endstyle(s, ">\n"))
  596. }
  597. // FeSpecEnd ends a specular lighting filter primitive container
  598. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feSpecularLightingElement
  599. func (svg *SVG) FeSpecEnd() {
  600. svg.println(`</feSpecularLighting>`)
  601. }
  602. // FeSpotLight specifies a feSpotLight filter primitive
  603. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feSpotLightElement
  604. func (svg *SVG) FeSpotLight(fs Filterspec, x, y, z, px, py, pz float64, s ...string) {
  605. svg.printf(`<feSpotLight %s x="%g" y="%g" z="%g" pointsAtX="%g" pointsAtY="%g" pointsAtZ="%g" %s`,
  606. fsattr(fs), x, y, z, px, py, pz, endstyle(s, emptyclose))
  607. }
  608. // FeTile specifies the tile utility filter primitive
  609. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feTileElement
  610. func (svg *SVG) FeTile(fs Filterspec, in string, s ...string) {
  611. svg.printf(`<feTile %s %s`, fsattr(fs), endstyle(s, emptyclose))
  612. }
  613. // FeTurbulence specifies a turbulence filter primitive
  614. // Standard reference: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
  615. func (svg *SVG) FeTurbulence(fs Filterspec, ftype string, bfx, bfy float64, octaves int, seed int64, stitch bool, s ...string) {
  616. if bfx < 0 || bfx > 1 {
  617. bfx = 0
  618. }
  619. if bfy < 0 || bfy > 1 {
  620. bfy = 0
  621. }
  622. switch ftype[0:1] {
  623. case "f", "F":
  624. ftype = "fractalNoise"
  625. case "t", "T":
  626. ftype = "turbulence"
  627. default:
  628. ftype = "turbulence"
  629. }
  630. var ss string
  631. if stitch {
  632. ss = "stitch"
  633. } else {
  634. ss = "noStitch"
  635. }
  636. svg.printf(`<feTurbulence %s type="%s" baseFrequency="%.2f %.2f" numOctaves="%d" seed="%d" stitchTiles="%s" %s`,
  637. fsattr(fs), ftype, bfx, bfy, octaves, seed, ss, endstyle(s, emptyclose))
  638. }
  639. // Filter Effects convenience functions, modeled after CSS versions
  640. // Blur emulates the CSS blur filter
  641. func (svg *SVG) Blur(p float64) {
  642. svg.FeGaussianBlur(Filterspec{}, p, p)
  643. }
  644. // Brightness emulates the CSS brightness filter
  645. func (svg *SVG) Brightness(p float64) {
  646. svg.FeComponentTransfer()
  647. svg.FeFuncLinear("R", p, 0)
  648. svg.FeFuncLinear("G", p, 0)
  649. svg.FeFuncLinear("B", p, 0)
  650. svg.FeCompEnd()
  651. }
  652. // Contrast emulates the CSS contrast filter
  653. //func (svg *SVG) Contrast(p float64) {
  654. //}
  655. // Dropshadow emulates the CSS dropshadow filter
  656. //func (svg *SVG) Dropshadow(p float64) {
  657. //}
  658. // Grayscale eumulates the CSS grayscale filter
  659. func (svg *SVG) Grayscale() {
  660. svg.FeColorMatrixSaturate(Filterspec{}, 0)
  661. }
  662. // HueRotate eumulates the CSS huerotate filter
  663. func (svg *SVG) HueRotate(a float64) {
  664. svg.FeColorMatrixHue(Filterspec{}, a)
  665. }
  666. // Invert eumulates the CSS invert filter
  667. func (svg *SVG) Invert() {
  668. svg.FeComponentTransfer()
  669. svg.FeFuncTable("R", []float64{1, 0})
  670. svg.FeFuncTable("G", []float64{1, 0})
  671. svg.FeFuncTable("B", []float64{1, 0})
  672. svg.FeCompEnd()
  673. }
  674. // Saturate eumulates the CSS saturate filter
  675. func (svg *SVG) Saturate(p float64) {
  676. svg.FeColorMatrixSaturate(Filterspec{}, p)
  677. }
  678. // Sepia applies a sepia tone, emulating the CSS sepia filter
  679. func (svg *SVG) Sepia() {
  680. var sepiamatrix = [20]float64{
  681. 0.280, 0.450, 0.05, 0, 0,
  682. 0.140, 0.390, 0.04, 0, 0,
  683. 0.080, 0.280, 0.03, 0, 0,
  684. 0, 0, 0, 1, 0,
  685. }
  686. svg.FeColorMatrix(Filterspec{}, sepiamatrix)
  687. }
  688. // Utility
  689. // Grid draws a grid at the specified coordinate, dimensions, and spacing, with optional style.
  690. func (svg *SVG) Grid(x int, y int, w int, h int, n int, s ...string) {
  691. if len(s) > 0 {
  692. svg.Gstyle(s[0])
  693. }
  694. for ix := x; ix <= x+w; ix += n {
  695. svg.Line(ix, y, ix, y+h)
  696. }
  697. for iy := y; iy <= y+h; iy += n {
  698. svg.Line(x, iy, x+w, iy)
  699. }
  700. if len(s) > 0 {
  701. svg.Gend()
  702. }
  703. }
  704. // Support functions
  705. // style returns a style name,attribute string
  706. func style(s string) string {
  707. if len(s) > 0 {
  708. return fmt.Sprintf(`style="%s"`, s)
  709. }
  710. return s
  711. }
  712. // pp returns a series of polygon points
  713. func (svg *SVG) pp(x []int, y []int, tag string) {
  714. svg.print(tag)
  715. if len(x) != len(y) {
  716. svg.print(" ")
  717. return
  718. }
  719. lx := len(x) - 1
  720. for i := 0; i < lx; i++ {
  721. svg.print(coord(x[i], y[i]) + " ")
  722. }
  723. svg.print(coord(x[lx], y[lx]))
  724. }
  725. // endstyle modifies an SVG object, with either a series of name="value" pairs,
  726. // or a single string containing a style
  727. func endstyle(s []string, endtag string) string {
  728. if len(s) > 0 {
  729. nv := ""
  730. for i := 0; i < len(s); i++ {
  731. if strings.Index(s[i], "=") > 0 {
  732. nv += (s[i]) + " "
  733. } else {
  734. nv += style(s[i]) + " "
  735. }
  736. }
  737. return nv + endtag
  738. }
  739. return endtag
  740. }
  741. // tt creates a xml element, tag containing s
  742. func (svg *SVG) tt(tag string, s string) {
  743. svg.print("<" + tag + ">")
  744. xml.Escape(svg.Writer, []byte(s))
  745. svg.println("</" + tag + ">")
  746. }
  747. // poly compiles the polygon element
  748. func (svg *SVG) poly(x []int, y []int, tag string, s ...string) {
  749. svg.pp(x, y, "<"+tag+" points=\"")
  750. svg.print(`" ` + endstyle(s, "/>\n"))
  751. }
  752. // onezero returns "0" or "1"
  753. func onezero(flag bool) string {
  754. if flag {
  755. return "1"
  756. }
  757. return "0"
  758. }
  759. // pct returns a percetage, capped at 100
  760. func pct(n uint8) uint8 {
  761. if n > 100 {
  762. return 100
  763. }
  764. return n
  765. }
  766. // islink determines if a string is a script reference
  767. func islink(link string) bool {
  768. return strings.HasPrefix(link, "http://") || strings.HasPrefix(link, "#") ||
  769. strings.HasPrefix(link, "../") || strings.HasPrefix(link, "./")
  770. }
  771. // group returns a group element
  772. func group(tag string, value string) string { return fmt.Sprintf(`<g %s="%s">`, tag, value) }
  773. // scale return the scale string for the transform
  774. func scale(n float64) string { return fmt.Sprintf(`scale(%g)`, n) }
  775. // scaleXY return the scale string for the transform
  776. func scaleXY(dx, dy float64) string { return fmt.Sprintf(`scale(%g,%g)`, dx, dy) }
  777. // skewx returns the skewX string for the transform
  778. func skewX(angle float64) string { return fmt.Sprintf(`skewX(%g)`, angle) }
  779. // skewx returns the skewX string for the transform
  780. func skewY(angle float64) string { return fmt.Sprintf(`skewY(%g)`, angle) }
  781. // rotate returns the rotate string for the transform
  782. func rotate(r float64) string { return fmt.Sprintf(`rotate(%g)`, r) }
  783. // translate returns the translate string for the transform
  784. func translate(x, y int) string { return fmt.Sprintf(`translate(%d,%d)`, x, y) }
  785. // coord returns a coordinate string
  786. func coord(x int, y int) string { return fmt.Sprintf(`%d,%d`, x, y) }
  787. // ptag returns the beginning of the path element
  788. func ptag(x int, y int) string { return fmt.Sprintf(`<path d="M%s`, coord(x, y)) }
  789. // loc returns the x and y coordinate attributes
  790. func loc(x int, y int) string { return fmt.Sprintf(`x="%d" y="%d"`, x, y) }
  791. // href returns the href name and attribute
  792. func href(s string) string { return fmt.Sprintf(`xlink:href="%s"`, s) }
  793. // dim returns the dimension string (x, y coordinates and width, height)
  794. func dim(x int, y int, w int, h int) string {
  795. return fmt.Sprintf(`x="%d" y="%d" width="%d" height="%d"`, x, y, w, h)
  796. }
  797. // fsattr returns the XML attribute representation of a filterspec, ignoring empty attributes
  798. func fsattr(s Filterspec) string {
  799. attrs := ""
  800. if len(s.In) > 0 {
  801. attrs += fmt.Sprintf(`in="%s" `, s.In)
  802. }
  803. if len(s.In2) > 0 {
  804. attrs += fmt.Sprintf(`in2="%s" `, s.In2)
  805. }
  806. if len(s.Result) > 0 {
  807. attrs += fmt.Sprintf(`result="%s" `, s.Result)
  808. }
  809. return attrs
  810. }
  811. // tablevalues outputs a series of values as a XML attribute
  812. func (svg *SVG) tablevalues(s string, t []float64) {
  813. svg.printf(` %s="`, s)
  814. for i := 0; i < len(t)-1; i++ {
  815. svg.printf("%g ", t[i])
  816. }
  817. svg.printf(`%g"%s`, t[len(t)-1], emptyclose)
  818. }
  819. // imgchannel validates the image channel indicator
  820. func imgchannel(c string) string {
  821. switch c {
  822. case "R", "G", "B", "A":
  823. return c
  824. case "r", "g", "b", "a":
  825. return strings.ToUpper(c)
  826. case "red", "green", "blue", "alpha":
  827. return strings.ToUpper(c[0:1])
  828. case "Red", "Green", "Blue", "Alpha":
  829. return c[0:1]
  830. }
  831. return "R"
  832. }