Android Camera 即時濾鏡(七)

來源:互聯網
上載者:User

標籤:android   camera   代碼   

HSL色彩模式與RGB色彩模式轉碼實現RGB色彩模式

HSL色彩模式

HSL色彩模式與RGB色彩模式轉換演算法

HSL與RGB轉換

  • RGB類
public class ColorRGB {    public int r;    public int g;    public int b;}
  • HSL類
public class ColorHSL {    public int h;    public float s;    public float l;}

a) RGB→HSL的演算法描述。

步驟1:把RGB值轉成【0,1】中數值。
步驟2:找出R,G和B中的最大值。
步驟3:設L=(maxcolor + mincolor)/2
步驟4:如果最大和最小的顏色值相同,即表示灰色,那麼S定義為0,而H未定義並在程 序中通常寫成0。
步驟5:否則,測試L:
  If L<0.5, S=(maxcolor-mincolor)/(maxcolor + mincolor)
  If L>=0.5, S=(maxcolor-mincolor)/(2.0-maxcolor-mincolor)
步驟6: If R=maxcolor, H=(G-B)/(maxcolor-mincolor)
  If G=maxcolor, H=2.0+(B-R)/(maxcolor-mincolor)
  If B=maxcolor, H=4.0+(R-G)/(maxcolor-mincolor)
步驟7:從第6步的計算看,H分成0~6地區。RGB色彩空間是一個立方體而HSL色彩空間是兩個六角形錐體,其中的L是RGB立方體的主對角線。因此,RGB立方體的頂點:紅、黃、綠、青、藍和品紅就成為HSL六角形的頂點,而數值0~6就告訴我們H在哪個部分。H=H*60.0,如果H為負值,則加360。

private static ColorHSL colorRGBToHSL(ColorRGB rgb) {    ColorHSL hsl = new ColorHSL();    float r, g, b, h, s, l;    r = rgb.r / 255.0f;    g = rgb.g / 255.0f;    b = rgb.b / 255.0f;    float maxColor = Math.max(r, Math.max(g, b));    float minColor = Math.min(r, Math.min(g, b));    if (maxColor == minColor) {        h = 0.0f;        s = 0.0f;        l = r;    } else {        l = (minColor + maxColor) / 2;        if (l < 0.5)            s = (maxColor - minColor) / (maxColor + minColor);        else            s = (float) ((maxColor - minColor) / (2.0 - maxColor -                  minColor));        if (r == maxColor)            h = (g - b) / (maxColor - minColor);        else if (g == maxColor)            h = (float) (2.0 + (b - r) / (maxColor - minColor));        else            h = (float) (4.0 + (r - g) / (maxColor - minColor));        h /= 6;        if (h < 0)            h++;    }    hsl.h = (int) Math.round(h * 360.0);    hsl.s = s;    hsl.l = l;    return hsl;}

b) HSL→RGB的演算法描述。

步驟1:If S=0,表示灰色,定義R,G和B都為L.
步驟2:否則,測試L:
   If L<0.5,temp2=L*(1.0+S)
   If L>=0.5,temp2=L+S-L*S
步驟3:temp1=2.0*-temp2
步驟4:把H轉換到0~1。
步驟5:對於R,G,B,計算另外的臨時值temp3。方法如下:
   for R, temp3=H+1.0/3.0
   for G, temp3=H
   for B, temp3=H-1.0/3.0
   if temp3<0, temp3=temp3+1.0
   if temp3>1, temp3=temp3-1.0
步驟6:對於R,G,B做如下測試:
   If 6.0*temp3<1,color=temp1+(temp2-temp1)*6.0*temp3
   Else if 2.0*temp3<1,color=temp2
   Else if 3.0*temp3<2,
   color=temp1+(temp2-temp1)*((2.0/3.0)-temp3)*6.0
   Else color=temp1

private static ColorRGB colorHSLToRGB(ColorHSL hsl) {    ColorRGB rgb = new ColorRGB();    float r, g, b, h, s, l;    float temp1, temp2, tempr, tempg, tempb;    h = hsl.h / 360.0f;    s = hsl.s;    l = hsl.l;    if (s == 0) {        r = g = b = l;    } else {        if (l < 0.5)            temp2 = l * (1 + s);        else            temp2 = (l + s) - (l * s);        temp1 = 2 * l - temp2;        tempr = (float) (h + 1.0 / 3.0);        if (tempr > 1)            tempr--;        tempg = h;        tempb = (float) (h - 1.0 / 3.0);        if (tempb < 0)            tempb++;        // Red        if (tempr < 1.0 / 6.0)            r = (float) (temp1 + (temp2 - temp1) * 6.0 * tempr);        else if (tempr < 0.5)            r = temp2;        else if (tempr < 2.0 / 3.0)            r = (float) (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempr)                        * 6.0);        else            r = temp1;        // Green        if (tempg < 1.0 / 6.0)            g = (float) (temp1 + (temp2 - temp1) * 6.0 * tempg);        else if (tempg < 0.5)            g = temp2;        else if (tempg < 2.0 / 3.0)            g = (float) (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempg)                        * 6.0);        else            g = temp1;        // Blue        if (tempb < 1.0 / 6.0)            b = (float) (temp1 + (temp2 - temp1) * 6.0 * tempb);        else if (tempb < 0.5)            b = temp2;        else if (tempb < 2.0 / 3.0)            b = (float) (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempb)                        * 6.0);        else            b = temp1;    }    rgb.r = (int) Math.round(r * 255.0);    rgb.g = (int) Math.round(g * 255.0);    rgb.b = (int) Math.round(b * 255.0);    return rgb;}

Android Camera 即時濾鏡(七)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.