简体   繁体   English

我需要了解java如何实现Color.RGBtoHSB(r,g,b,hsb)函数的详细信息。 他们是否将r,g,b归一化

[英]I need to know details of how java implements the function Color.RGBtoHSB(r, g, b, hsb). Does they normalize r,g,b

我不知道Color.RGBtoHSB(r,g,b,hsb)函数在将r,g,b转换为H,S,B之前是否将r,g,b归一化,或者在哪里可以获取其内置函数的java实现。

Here is the implementation, from Color class source code directly: 这是直接从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;
}

Just open up Eclipse - Ctrl+Shift+T (open type), type in Color, find the one in java.awt - and voila. 只需打开Eclipse-Ctrl + Shift + T(打开类型),键入Color,在java.awt中找到一个-瞧。 Works for most built in types. 适用于大多数内置类型。

RGB isn't normalized first. RGB没有先标准化。 It's normalized during and generally just into the correct ranges. 通常在正确范围内将其标准化。 So brightness is the largest component and brightness is normalized from 0-255 range to 0-1 range. 因此,亮度是最大的组成部分,亮度从0-255范围标准化到0-1范围。 Saturation is like this as well, it's the distance from the largest component to the smallest component and squeezed into a 0-1 range. 饱和度也是如此,它是从最大成分到最小成分并被压缩到0-1范围内的距离。 And hue is the angle in the color wheel. 色相是色轮中的角度。 But, no it's converted directly into HSV and not normalized through something like sRGB (sRGB is RGB/255 and normalized into 0-1 range). 但是,不,它不能直接转换为HSV,也不能通过sRGB之类的东西进行归一化(sRGB是RGB / 255,并且归一化为0-1范围)。

But, you shouldn't really need to know this at all. 但是,您根本不需要真正了解这一点。 It converts into HSB. 它将转换为HSB。 Can you get rounding errors if you convert back and forth a bunch. 如果来回转换一堆,会舍入错误吗? Sure, you can. 你当然可以。 Other than this it doesn't matter if it scales RGB to 1 or 1,000,000, it converts to a completely different way of representing colors in ranges between 0-1. 除此之外,将RGB缩放到1或1,000,000都没有关系,它转换为一种表示0-1范围内颜色的完全不同的方式。

Note: the hue returned from Color.RGBtoHSB is normalized between 0.0 and 1.0, not between 0 and 255: ``` 注意:从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));
        }
    }

``` ```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM