RGB 飞往飞利浦顺化 (HSB)
我正在为学校的作业制作一个音乐播放器。飞利浦 Hue 灯光将产生一些相应的视觉效果。我想让每首歌的视觉效果都独一无二。因此,我获取了播放轨道的封面艺术(使用LastFM API)以获得最常见的颜色,并将其用作创建其他颜色的基础。飞利浦Hue具有不同的显示颜色的方式,即(HSB)。所以我通过转换它
Color.RGBtoHSB();
例如,它给我的R= 127,G = 190,B = 208的值H = 0.5370371,S = 0.38942307,B = 0.8156863。现在我猜它们是在1的基础上计算的,所以我将亮度en饱和度乘以255。和顺化由65535。(如 http://developers.meethue.com/1_lightsapi.html 所示)
在飞利浦 Hue 中设置这些计算值时,无论播放什么歌曲,颜色始终为红色或白色。
从RGB到HSB的转换是否有问题?
根据流行的要求,我的代码:
作为测试:
Color c = Colorconverter.getMostCommonColour("urltoimage");
float[] f = Colorconverter.getRGBtoHSB(c);
ArrayList<Lamp> myLamps = PhilipsHue.getInstance().getMyLamps();
State state = new State();
state.setBri((int) Math.ceil(f[2]*255));
state.setSat((int) Math.ceil(f[1]*255));
state.setHue((int) Math.ceil(f[0]*65535));
state.setOn(true);
PhilipsHue.setState(myLamps.get(1), state);
如上所示的功能
public static Color getMostCommonColour(String coverArtURL) {
Color coulourHex = null;
try {
BufferedImage image = ImageIO.read(new URL(coverArtURL));
int height = image.getHeight();
int width = image.getWidth();
Map m = new HashMap();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int rgb = image.getRGB(i, j);
int[] rgbArr = getRGBArr(rgb);
// No grays ...
if (!isGray(rgbArr)) {
Integer counter = (Integer) m.get(rgb);
if (counter == null) {
counter = 0;
}
counter++;
m.put(rgb, counter);
}
}
}
coulourHex = getMostCommonColour(m);
System.out.println(coulourHex);
} catch (IOException e) {
e.printStackTrace();
}
return coulourHex;
}
private static Color getMostCommonColour(Map map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
Map.Entry me = (Map.Entry) list.get(list.size() - 1);
int[] rgb = getRGBArr((Integer) me.getKey());
String r = Integer.toHexString(rgb[0]);
String g = Integer.toHexString(rgb[1]);
String b = Integer.toHexString(rgb[2]);
Color c = new Color(rgb[0], rgb[1], rgb[2]);
return c;
}
private static int[] getRGBArr(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
return new int[] { red, green, blue };
}
private static boolean isGray(int[] rgbArr) {
int rgDiff = rgbArr[0] - rgbArr[1];
int rbDiff = rgbArr[0] - rgbArr[2];
// Filter out black, white and grays...... (tolerance within 10 pixels)
int tolerance = 10;
if (rgDiff > tolerance || rgDiff < -tolerance)
if (rbDiff > tolerance || rbDiff < -tolerance) {
return false;
}
return true;
}
public static float[] getRGBtoHSB(Color c) {
float[] hsv = new float[3];
return Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsv);
}
设置状态只是对飞利浦灯泡做了一个简单的放。当我检查受影响的灯泡上的 JSON 时
{
"state": {
"on": true,
"bri": 81,
"hue": 34277,
"sat": 18,
"xy": [
0.298,
0.2471
],
"ct": 153,
"alert": "none",
"effect": "none",
"colormode": "hs",
"reachable": true
},
"type": "Extended color light",
"name": "Hue Spot 1",
"modelid": "LCT003",
"swversion": "66010732",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
}