diff src/nabble/view/web/util/RoundedCorner.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/view/web/util/RoundedCorner.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,109 @@
+package nabble.view.web.util;
+
+import fschmidt.util.java.IoUtils;
+import fschmidt.util.servlet.JtpContext;
+import nabble.view.lib.UrlMappable;
+
+import javax.imageio.ImageIO;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.awt.AlphaComposite;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.Shape;
+import java.awt.geom.Ellipse2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.RenderedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Quadrants
+ * 1 | 2
+ * 3 | 4
+ *
+ * @author Hugo Teixeira
+ */
+public class RoundedCorner extends HttpServlet implements UrlMappable {
+
+	private static final Pattern URL_PATTERN = Pattern.compile("/rounded/q(\\d)_([A-Fa-f0-9]{6})([A-Fa-f0-9]{2})?$");
+
+	public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) {
+		Matcher m = URL_PATTERN.matcher(mappedUrl);
+		if( !m.find() )
+			throw new RuntimeException();
+		Map<String,String[]> params = new HashMap<String,String[]>();
+		String quadrant = m.group(1);
+		params.put("quadrant",new String[]{quadrant});
+		String bgColor = m.group(2) + (m.group(3) == null? "" : m.group(3));
+		params.put( "bgColor", new String[]{bgColor} );
+		return params;
+	}
+
+	public Pattern getUrlPattern() {
+		return URL_PATTERN;
+	}
+
+	protected void service(HttpServletRequest request, HttpServletResponse response)
+		throws ServletException, IOException {
+		OutputStream out = response.getOutputStream();
+		response.setContentType("image/png");
+
+		JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
+		jtpContext.setEtag(request,response);
+
+		int quadrant = Integer.valueOf(request.getParameter("quadrant"));
+		String bgColor = request.getParameter("bgColor");
+
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		ImageIO.write(createImage(quadrant, bgColor), "png", baos);
+		final byte[] bytes = baos.toByteArray();
+
+		InputStream in = new ByteArrayInputStream(bytes);
+		try {
+			IoUtils.copyAll(in,out);
+		} finally {
+			in.close();
+		}
+	}
+
+	private RenderedImage createImage(int quadrant, String bgColor) {
+		int radius = 12;
+		int[] xy = { 0, 0 };
+		if (quadrant == 2) xy = new int[]{ -radius, 0 };
+		else if (quadrant == 3) xy = new int[]{ 0, -radius };
+		else if (quadrant == 4) xy = new int[]{ -radius, -radius };
+
+		Color bg = parseColor(bgColor);
+		BufferedImage img = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_ARGB);
+		Graphics2D g2 = img.createGraphics();
+		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+		g2.setColor(bg);
+		g2.fillRect(0, 0, radius, radius);
+		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.5f));
+		Shape s = new Ellipse2D.Float(xy[0], xy[1], 2 * radius, 2* radius);
+		g2.fill(s);
+		g2.dispose();
+		return img;
+	}
+
+	private Color parseColor(String c) {
+		if (c.length() == 6)
+			return Color.decode("0x"+c);
+		int r = Integer.parseInt(c.substring(0,2), 16);
+		int g = Integer.parseInt(c.substring(2,4), 16);
+		int b = Integer.parseInt(c.substring(4,6), 16);
+		int a = Integer.parseInt(c.substring(6,8), 16);
+		return new Color(r, g, b, a);
+	}
+}
\ No newline at end of file