diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/view/web/util/GradientImage.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,108 @@
+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.Color;
+import java.awt.GradientPaint;
+import java.awt.Graphics2D;
+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;
+
+/**
+ * @author Hugo Teixeira
+ */
+public class GradientImage extends HttpServlet implements UrlMappable {
+
+	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})?$");
+
+	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 direction = m.group(1);
+		params.put("direction",new String[]{"v".equals(direction)? "vertical" : "horizontal"});
+		String size = m.group(2);
+		params.put("size",new String[]{size});
+		String fromColor = m.group(3) + (m.group(4) == null? "" : m.group(4));
+		params.put( "from", new String[]{fromColor} );
+		String toColor = m.group(5) + (m.group(6) == null? "" : m.group(6));
+		params.put( "to", new String[]{toColor} );
+		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);
+
+		String direction = request.getParameter("direction");
+		String size = request.getParameter("size");
+		String fromColor = request.getParameter("from");
+		String toColor = request.getParameter("to");
+
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		ImageIO.write(createImage(direction, size, fromColor, toColor), "png", baos);
+		final byte[] bytes = baos.toByteArray();
+
+		InputStream in = new ByteArrayInputStream(bytes);
+		try {
+			IoUtils.copyAll(in,out);
+		} finally {
+			in.close();
+		}
+	}
+
+	private RenderedImage createImage(String direction, String size, String fromColor, String toColor) {
+		boolean isVertical = "vertical".equals(direction);
+		int width = isVertical? 1 : Integer.valueOf(size);
+		int height = isVertical? Integer.valueOf(size) : 1;
+		Color from = parseColor(fromColor);
+		Color to = parseColor(toColor);
+		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+		Graphics2D g2 = img.createGraphics();
+		GradientPaint paint;
+		if (isVertical)
+			paint = new GradientPaint(0, 0, from, 0, height, to);
+		else
+			paint = new GradientPaint(0, 0, from, width, 0, to);
+		g2.setPaint(paint);
+		g2.fillRect(0, 0, width, height);
+		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);
+	}
+}