Color.RGBToHSV类型不匹配:无法从void转换为float []

时间:2014-08-29 13:47:26

标签: java android arrays colors rgb

使用ADT

import android.graphics.Color;

我不断获得Type mismatch: cannot convert from void to float[]

float[] hsv = new float[3];

hsv = Color.RGBToHSV(rgb[0], rgb[1], rgb[2], hsv);

Color.RGBToHSV(rgb[0], rgb[1], rgb[2], hsv);下的错误行突出显示,并显示type mismatch。有没有办法解决?此代码以前是为JRE设置的,但我将其转换为ADT。

以前读过;

hsv = java.awt.Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], hsv);

如何更正此类型不匹配?

我试过这种方式,但我需要将它添加到float[] hsv数组;

Color.RGBToHSV(rgb[0], rgb[1], rgb[2], hsv);

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

这是来自android源代码

public static void RGBToHSV(int red, int green, int blue, float hsv[]) {
    if (hsv.length < 3) {
        throw new RuntimeException("3 components required for hsv");
    }
    nativeRGBToHSV(red, green, blue, hsv);
}

这意味着它将RGB颜色转换为HSV,并将它们放入数组中。与java.awt.Color源代码

相反,该方法不返回任何内容
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
    float hue, saturation, brightness;
    if (hsbvals == null) {
        hsbvals = new float[3];
    }
    int cmax = (r > g) ? r : g;
    if (b > cmax) cmax = b;
    int cmin = (r < g) ? r : g;
    if (b < cmin) cmin = b;

    brightness = ((float) cmax) / 255.0f;
    if (cmax != 0)
        saturation = ((float) (cmax - cmin)) / ((float) cmax);
    else
        saturation = 0;
    if (saturation == 0)
        hue = 0;
    else {
        float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
        float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
        float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
        if (r == cmax)
            hue = bluec - greenc;
        else if (g == cmax)
            hue = 2.0f + redc - bluec;
        else
            hue = 4.0f + greenc - redc;
        hue = hue / 6.0f;
        if (hue < 0)
            hue = hue + 1.0f;
    }
    hsbvals[0] = hue;
    hsbvals[1] = saturation;
    hsbvals[2] = brightness;
    return hsbvals;
}

返回类型不同。 float[] vs void

答案 1 :(得分:0)

它将这些值放在您作为参数传递的数组中。所以它的返回值类型实际上是void

相关问题