Mercurial Hosting > nabble
comparison src/nabble/view/web/util/GradientImage.java @ 0:7ecd1a4ef557
add content
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 21 Mar 2019 19:15:52 -0600 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:7ecd1a4ef557 |
|---|---|
| 1 package nabble.view.web.util; | |
| 2 | |
| 3 import fschmidt.util.java.IoUtils; | |
| 4 import fschmidt.util.servlet.JtpContext; | |
| 5 import nabble.view.lib.UrlMappable; | |
| 6 | |
| 7 import javax.imageio.ImageIO; | |
| 8 import javax.servlet.ServletException; | |
| 9 import javax.servlet.http.HttpServlet; | |
| 10 import javax.servlet.http.HttpServletRequest; | |
| 11 import javax.servlet.http.HttpServletResponse; | |
| 12 import java.awt.Color; | |
| 13 import java.awt.GradientPaint; | |
| 14 import java.awt.Graphics2D; | |
| 15 import java.awt.image.BufferedImage; | |
| 16 import java.awt.image.RenderedImage; | |
| 17 import java.io.ByteArrayInputStream; | |
| 18 import java.io.ByteArrayOutputStream; | |
| 19 import java.io.IOException; | |
| 20 import java.io.InputStream; | |
| 21 import java.io.OutputStream; | |
| 22 import java.util.HashMap; | |
| 23 import java.util.Map; | |
| 24 import java.util.regex.Matcher; | |
| 25 import java.util.regex.Pattern; | |
| 26 | |
| 27 /** | |
| 28 * @author Hugo Teixeira | |
| 29 */ | |
| 30 public class GradientImage extends HttpServlet implements UrlMappable { | |
| 31 | |
| 32 private static final Pattern URL_PATTERN = Pattern.compile("/gradients/(v|h)(\\d+)_([A-Fa-f0-9]{6})([A-Fa-f0-9]{2})?_([A-Fa-f0-9]{6})([A-Fa-f0-9]{2})?$"); | |
| 33 | |
| 34 public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) { | |
| 35 Matcher m = URL_PATTERN.matcher(mappedUrl); | |
| 36 if( !m.find() ) | |
| 37 throw new RuntimeException(); | |
| 38 Map<String,String[]> params = new HashMap<String,String[]>(); | |
| 39 String direction = m.group(1); | |
| 40 params.put("direction",new String[]{"v".equals(direction)? "vertical" : "horizontal"}); | |
| 41 String size = m.group(2); | |
| 42 params.put("size",new String[]{size}); | |
| 43 String fromColor = m.group(3) + (m.group(4) == null? "" : m.group(4)); | |
| 44 params.put( "from", new String[]{fromColor} ); | |
| 45 String toColor = m.group(5) + (m.group(6) == null? "" : m.group(6)); | |
| 46 params.put( "to", new String[]{toColor} ); | |
| 47 return params; | |
| 48 } | |
| 49 | |
| 50 public Pattern getUrlPattern() { | |
| 51 return URL_PATTERN; | |
| 52 } | |
| 53 | |
| 54 protected void service(HttpServletRequest request, HttpServletResponse response) | |
| 55 throws ServletException, IOException | |
| 56 { | |
| 57 OutputStream out = response.getOutputStream(); | |
| 58 response.setContentType("image/png"); | |
| 59 | |
| 60 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName); | |
| 61 jtpContext.setEtag(request,response); | |
| 62 | |
| 63 String direction = request.getParameter("direction"); | |
| 64 String size = request.getParameter("size"); | |
| 65 String fromColor = request.getParameter("from"); | |
| 66 String toColor = request.getParameter("to"); | |
| 67 | |
| 68 ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| 69 ImageIO.write(createImage(direction, size, fromColor, toColor), "png", baos); | |
| 70 final byte[] bytes = baos.toByteArray(); | |
| 71 | |
| 72 InputStream in = new ByteArrayInputStream(bytes); | |
| 73 try { | |
| 74 IoUtils.copyAll(in,out); | |
| 75 } finally { | |
| 76 in.close(); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 private RenderedImage createImage(String direction, String size, String fromColor, String toColor) { | |
| 81 boolean isVertical = "vertical".equals(direction); | |
| 82 int width = isVertical? 1 : Integer.valueOf(size); | |
| 83 int height = isVertical? Integer.valueOf(size) : 1; | |
| 84 Color from = parseColor(fromColor); | |
| 85 Color to = parseColor(toColor); | |
| 86 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
| 87 Graphics2D g2 = img.createGraphics(); | |
| 88 GradientPaint paint; | |
| 89 if (isVertical) | |
| 90 paint = new GradientPaint(0, 0, from, 0, height, to); | |
| 91 else | |
| 92 paint = new GradientPaint(0, 0, from, width, 0, to); | |
| 93 g2.setPaint(paint); | |
| 94 g2.fillRect(0, 0, width, height); | |
| 95 g2.dispose(); | |
| 96 return img; | |
| 97 } | |
| 98 | |
| 99 private Color parseColor(String c) { | |
| 100 if (c.length() == 6) | |
| 101 return Color.decode("0x"+c); | |
| 102 int r = Integer.parseInt(c.substring(0,2), 16); | |
| 103 int g = Integer.parseInt(c.substring(2,4), 16); | |
| 104 int b = Integer.parseInt(c.substring(4,6), 16); | |
| 105 int a = Integer.parseInt(c.substring(6,8), 16); | |
| 106 return new Color(r, g, b, a); | |
| 107 } | |
| 108 } |
