RGB HSL Color Conversion

RGB to HSL Color Conversion
public static int RGB2HSL(int red,int green, int blue)
{
    int H,S,L,var_Min,var_Max,del_Max,del_R,del_G,del_B;
    H=0;
 
    var_Min = Math.min(red, Math.min(blue, green));    
    var_Max = Math.max(red, Math.max(blue, green));    
    del_Max = var_Max - var_Min;             
 
    L = ( var_Max + var_Min ) / 2;
 
    if ( del_Max == 0 )                     
    {
       H = 0;                                
       S = 0;
    }else                                    
    {
        if ( L < 128 ) 
            S = 256*del_Max / ( var_Max + var_Min );
        else           
            S = 256*del_Max / ( 512 - var_Max - var_Min );
 
        del_R = ( ( 360*( var_Max - red ) / 6) + ( 360*del_Max / 2 ) ) / del_Max;
        del_G = ( ( 360*( var_Max - green ) / 6) + ( 360*del_Max / 2 ) ) / del_Max;
        del_B = ( ( 360*( var_Max - blue ) / 6) + ( 360*del_Max / 2 ) ) / del_Max;            
 
        if      ( red == var_Max ) 
            H = del_B - del_G;
        else if ( green == var_Max ) 
            H = 120 + del_R - del_B;
        else if ( blue == var_Max ) 
            H = 240 + del_G - del_R;
 
        if ( H < 0 ) 
            H += 360;
        if ( H >= 360 ) 
            H -= 360;
 
        if ( L >= 256 ) 
            L = 255;
        if ( S >= 256 ) 
            S = 255;            
    }
 
   return H+S*256+L*256*256;
}

 

HSL to RGB Color Conversion

public static int HSL2RGB(int H, int S, int L)
{
    int R,G,B,var_1,var_2;
 
    if ( S == 0 )                       
    {
       R = L;                      
       G = L;
       B = L;
    }
    else
    {
        if ( L < 128 ) 
            var_2 = (L * ( 256 + S ))/256;
        else           
            var_2 = ( L + S ) - ( S * L )/256;
 
        var_1 = 2 * L - var_2;
 
        R = RGBFromHue( var_1, var_2, H + 120 );
        G = RGBFromHue( var_1, var_2, H );
        B = RGBFromHue( var_1, var_2, H - 120 );
    }
 
    return R+G*256+B*256*256;
}
 
public static int RGBFromHue(int a,int b,int h )          
{
    if ( h < 0 ) 
        h += 360;
    if ( h >= 360 ) 
        h -= 360;
 
    if ( h < 60 )                                      
        return a + (( b - a ) * h) / 60;                                
    if (  h  < 180 ) 
        return b;
    if (  h  < 240 ) 
        return a + (( b - a ) * ( 240 - h )) / 60;
 
    return a;
}

 

Note: Saturation and lightness both range from 0 to 255 while hue ranges from 0 to 360.

RGB2HSL returns an integer containing all three hue, saturation and lightness values. To extract them separately use the following method:

int hsl = RGB2HSL(red,green,blue);
int hue=hsl%256;
int saturation=(hsl/256)%256;
int lightness=hsl/(256*256);

HSL2RGB return red, green and blue channels in the same manner.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值