【问题标题】:Color.RGBToHSV Type mismatch: cannot convert from void to float[]Color.RGBToHSV 类型不匹配:无法从 void 转换为 float[]
【发布时间】:2014-10-23 13:09:26
【问题描述】:

使用 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);

任何帮助将不胜感激。

【问题讨论】:

  • java.awt.Color.RGBtoHSB 是 float[] 类型,android.graphics.Color.RGBToHSV 是 void 类型

标签: java android arrays colors rgb


【解决方案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[]void

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • Color.RGBToHSV(rgb[0], rgb[1], rgb[2], hsv);在我的理解中不会被添加到数组中。我需要找到一种方法将这些值添加到 hsv
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2019-01-16
    相关资源
    最近更新 更多