Skip to content

Instantly share code, notes, and snippets.

@tomconte
Created March 19, 2016 11:08
Show Gist options
  • Select an option

  • Save tomconte/bd506e868c56541b381b to your computer and use it in GitHub Desktop.

Select an option

Save tomconte/bd506e868c56541b381b to your computer and use it in GitHub Desktop.
I used this small servlet to convert RGB values to Hue's specific XY encoding.
package com.hypernephelist;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.philips.lighting.hue.sdk.utilities.PHUtilities;
/**
* Servlet implementation class HueRGBConverter
*/
@WebServlet("/convert")
public class HueRGBConverter extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HueRGBConverter() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get Parameters
int r = Integer.parseInt(request.getParameter("r"));
int g = Integer.parseInt(request.getParameter("g"));
int b = Integer.parseInt(request.getParameter("b"));
String modelId = request.getParameter("modelid");
// Calculate XY
// Color is "complicated", see http://www.developers.meethue.com/documentation/core-concepts#color_gets_more_complicated
// For Model ID, see http://www.developers.meethue.com/documentation/supported-lights
float xy[] = PHUtilities.calculateXYFromRGB(r, g, b, modelId);
// Return JSON
// It can be sent directly to the Hue API, see http://www.developers.meethue.com/documentation/lights-api#16_set_light_state
String x = Float.toString(xy[0]);
String y = Float.toString(xy[1]);
response.getWriter().append("{\"xy\":[").append(x).append(",").append(y).append("]}");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment