This question already has answers here:
RGB to Philips Hue (HSB)

(3个答案)


已关闭6年。




正如我之前说过的(RGB to Philips Hue (HSB)),我仍然没有放弃将简单的RGB值转换为Philips Hue可以接受的值的希望。

适用于iPhone和Android的官方应用程序允许您从随机照片中选择一种颜色,并且Hue会相应地进行调整。因此,必须有某种公式。

但是,这次我想我刚刚在这篇有趣的在线文章中找到了这个公式:

https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md

我一直在尝试将他的解释转换为Java语言,但没有得到预期的结果。
有人知道出什么问题了吗?

作为记录,我使用的是飞利浦Hue灯泡。
public static List<Double> getRGBtoXY(Color c) {
        // For the hue bulb the corners of the triangle are:
        // -Red: 0.675, 0.322
        // -Green: 0.4091, 0.518
        // -Blue: 0.167, 0.04
        double[] normalizedToOne = new double[3];
        float cred, cgreen, cblue;
        cred = c.getRed();
        cgreen = c.getGreen();
        cblue = c.getBlue();
        normalizedToOne[0] = (cred / 255);
        normalizedToOne[1] = (cgreen / 255);
        normalizedToOne[2] = (cblue / 255);
        float red, green, blue;

        // Make red more vivid
        if (normalizedToOne[0] > 0.04045) {
            red = (float) Math.pow(
                    (normalizedToOne[0] + 0.055) / (1.0 + 0.055), 2.4);
        } else {
            red = (float) (normalizedToOne[0] / 12.92);
        }

        // Make green more vivid
        if (normalizedToOne[1] > 0.04045) {
            green = (float) Math.pow((normalizedToOne[1] + 0.055)
                    / (1.0 + 0.055), 2.4);
        } else {
            green = (float) (normalizedToOne[1] / 12.92);
        }

        // Make blue more vivid
        if (normalizedToOne[2] > 0.04045) {
            blue = (float) Math.pow((normalizedToOne[2] + 0.055)
                    / (1.0 + 0.055), 2.4);
        } else {
            blue = (float) (normalizedToOne[2] / 12.92);
        }

        float X = (float) (red * 0.649926 + green * 0.103455 + blue * 0.197109);
        float Y = (float) (red * 0.234327 + green * 0.743075 + blue + 0.022598);
        float Z = (float) (red * 0.0000000 + green * 0.053077 + blue * 1.035763);

        float x = X / (X + Y + Z);
        float y = Y / (X + Y + Z);

        double[] xy = new double[2];
        xy[0] = x;
        xy[1] = y;
        List<Double> xyAsList = Doubles.asList(xy);
        return xyAsList;
    }

这不是重复的吗?请阅读引用的问题以完全理解。这是转换为XY的问题。
引用的问题是关于将RGB转换为HSB。怎么可能一样?!?!

最佳答案

发现一个小错误:

float Y = (float) (red * 0.234327 + green * 0.743075 + blue + 0.022598);

应该
float Y = (float) (red * 0.234327 + green * 0.743075 + blue * 0.022598);

09-10 07:12
查看更多