【问题标题】:I need to know details of how java implements the function Color.RGBtoHSB(r, g, b, hsb). Does they normalize r,g,b我需要知道 java 如何实现函数 Color.RGBtoHSB(r, g, b, hsb) 的细节。他们是否标准化 r,g,b
【发布时间】:2012-08-26 08:16:21
【问题描述】:

我不知道 Color.RGBtoHSB(r, g, b, hsb) 函数是否在将 r,g,b 转换为 H,S,B 之前对其进行规范化,或者我在哪里可以获得它们内置函数的 java 实现.

【问题讨论】:

    标签: java image-processing colors rgb hsb


    【解决方案1】:

    这里是实现,直接来自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;
    }
    

    【讨论】:

      【解决方案2】:

      只需打开 Eclipse - Ctrl+Shift+T(打开类型),输入 Color,在 java.awt 中找到那个 - 瞧。适用于大多数内置类型。

      【讨论】:

      • 奇怪 - 我会尝试重建类型缓存:关闭 Eclipse:转到工作区/.metadata/.plugins/org.eclipse.jdt.core,删除 *.index 和 savedIndexNames.txt,重启 Eclipse。它将重建整个类型缓存。
      【解决方案3】:

      RGB 不是首先标准化的。它在期间正常化,通常只是在正确的范围内。所以亮度是最大的分量,亮度从 0-255 范围归一化到 0-1 范围。饱和度也是这样,它是从最大分量到最小分量的距离,被压缩到 0-1 的范围内。色调是色轮中的角度。但是,不,它直接转换为 HSV,而不是通过 sRGB 之类的东西进行归一化(sRGB 是 RGB/255 并归一化为 0-1 范围)。

      但是,您根本不需要知道这一点。它转换为 HSB。如果你来回转换一堆,你会得到舍入错误吗?你当然可以。除此之外,将 RGB 缩放到 1 或 1,000,000 并不重要,它会转换为完全不同的方式来表示 0-1 之间的颜色。

      【讨论】:

        【解决方案4】:

        注意:从Color.RGBtoHSB 返回的色调在 0.0 和 1.0 之间进行归一化,不是在 0 和 255 之间: ```

        public static void main(String[] args) {
                float[] hsbvals = new float[3];
                Random random = new Random();
                for(int i=0;i<20;i++) {
                    Color.RGBtoHSB(random.nextInt(256),random.nextInt(256),random.nextInt(256),hsbvals);
                    System.out.println(Arrays.toString(hsbvals));
                }
            }
        

        ```

        【讨论】:

          猜你喜欢
          • 2020-05-08
          • 2015-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-24
          • 1970-01-01
          • 2018-10-11
          相关资源
          最近更新 更多