簡體   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實現。

這是直接從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;
}

只需打開Eclipse-Ctrl + Shift + T(打開類型),鍵入Color,在java.awt中找到一個-瞧。 適用於大多數內置類型。

RGB沒有先標准化。 通常在正確范圍內將其標准化。 因此,亮度是最大的組成部分,亮度從0-255范圍標准化到0-1范圍。 飽和度也是如此,它是從最大成分到最小成分並被壓縮到0-1范圍內的距離。 色相是色輪中的角度。 但是,不,它不能直接轉換為HSV,也不能通過sRGB之類的東西進行歸一化(sRGB是RGB / 255,並且歸一化為0-1范圍)。

但是,您根本不需要真正了解這一點。 它將轉換為HSB。 如果來回轉換一堆,會舍入錯誤嗎? 你當然可以。 除此之外,將RGB縮放到1或1,000,000都沒有關系,它轉換為一種表示0-1范圍內顏色的完全不同的方式。

注意:從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