【问题标题】:android convert from rgb to hsv and viceversaandroid从rgb转换为hsv,反之亦然
【发布时间】:2015-03-19 10:40:21
【问题描述】:

有人可以解释为什么从 hsv 转换回 rgb 时从 rgb 到 hsv 的转换不会给出相同的结果吗?

int color = Color.rgb(206, 43, 55);

int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
System.out.println(red + ", " + green + ", " + blue);
//prints: 206, 43, 55 (as expected)

float[] hsv = new float[3];
Color.RGBToHSV(red, green, blue, hsv);

float hue = hsv[0];
float sat = hsv[1];
float val = hsv[2];

int outputColor = Color.HSVToColor(hsv);
red = Color.red(outputColor);
green = Color.green(outputColor);
blue = Color.blue(outputColor);
System.out.println(red + ", " + green + ", " + blue);

//prints: 206, 42, 54 (green and blue are changed)

【问题讨论】:

    标签: android colors rgb hsv


    【解决方案1】:
    float[] hsv = new float[3];
    

    我想这足以回答您的问题,转换结果在 HSV 中,范围为 [0.0, 1.0],因此您处理的是有限精度,因此将其转换回不会给出完全相同的值。

    【讨论】:

    • 我还以为是这个原因。你知道其他精确转换的方法吗?
    【解决方案2】:

    我终于找到了解决方案。 android.graphics.Color.RGBToHSV 的 android 实现中似乎有一个奇怪的近似值。 近似值正是在此实现中从 0° 到 360° 的色调。

    我找到了 java.awt.Color.RGBtoHSB 的代码,其中 HUE 从 0.0f 变为 1.0f,并且转换效果很好。所以不是浮点精度错误,而是实现错误,实际上通过乘以 Hue * 360f 我得到正确的 HSV Hue 值。

    【讨论】:

      【解决方案3】:

      使用此修复:

          float[] hsv = new float[3];
      Color.RGBToHSV(red, green, blue, hsv);
      
      float hue = hsv[0];
      float sat = hsv[1];
      float val = hsv[2];
      
      // i delete this part
      /*int outputColor = Color.HSVToColor(hsv);
      red = Color.red(outputColor);
      green = Color.green(outputColor);
      blue = Color.blue(outputColor);*/
      
      //use only this to get correct values (HSV)
      System.out.println(hue + ", " + sat+ ", " + val);
      

      【讨论】:

        【解决方案4】:

        如果有人在 Kotlin 语言中查看从 RGB 到 HSV 的转换,反之亦然:

        // define array and color
        var hsv = FloatArray(3)
        var currentColor = Color.rgb(206, 43, 55);
        
        // RGB to HSV
        Color.colorToHSV(currentColor, hsv)
        
        // hsv[0] --> hue
        // hsv[1] --> saturation
        // hsv[2] --> value
        
        // HSV to RGB
        currentColor = Color.HSVToColor(hsv)
        
        // assign red, green and blue
        r = Color.red(currentColor)
        g = Color.green(currentColor)
        b = Color.blue(currentColor)
        
        

        【讨论】:

          猜你喜欢
          • 2012-10-31
          • 1970-01-01
          • 1970-01-01
          • 2020-10-24
          • 2014-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-18
          相关资源
          最近更新 更多