Conversion between RGB and HSL in Python

From , 2 Years ago, written in Python, viewed 182 times.
URL https://pastebin.vip/view/358f9e7b
  1. def HSL_to_RGB(h,s,l):
  2.     ''' Converts HSL colorspace (Hue/Saturation/Value) to RGB colorspace.
  3.        Formula from http://www.easyrgb.com/math.php?MATH=M19#text19
  4.      
  5.        Input:
  6.            h (float) : Hue (0...1, but can be above or below
  7.                              (This is a rotation around the chromatic circle))
  8.            s (float) : Saturation (0...1)    (0=toward grey, 1=pure color)
  9.            l (float) : Lightness (0...1)     (0=black 0.5=pure color 1=white)
  10.      
  11.        Ouput:
  12.            (r,g,b) (integers 0...255) : Corresponding RGB values
  13.      
  14.        Examples:
  15.            >>> print HSL_to_RGB(0.7,0.7,0.6)
  16.            (110, 82, 224)
  17.            >>> r,g,b = HSL_to_RGB(0.7,0.7,0.6)
  18.            >>> print g
  19.            82
  20.    '''
  21.     def Hue_2_RGB( v1, v2, vH ):
  22.         while vH<0.0: vH += 1.0
  23.         while vH>1.0: vH -= 1.0
  24.         if 6*vH < 1.0 : return v1 + (v2-v1)*6.0*vH
  25.         if 2*vH < 1.0 : return v2
  26.         if 3*vH < 2.0 : return v1 + (v2-v1)*((2.0/3.0)-vH)*6.0
  27.         return v1
  28.    
  29.     if not (0 <= s <=1): raise ValueError,"s (saturation) parameter must be between 0 and 1."
  30.     if not (0 <= l <=1): raise ValueError,"l (lightness) parameter must be between 0 and 1."
  31.    
  32.     r,b,g = (l*255,)*3
  33.     if s!=0.0:
  34.        if l<0.5 : var_2 = l * ( 1.0 + s )
  35.        else     : var_2 = ( l + s ) - ( s * l )
  36.        var_1 = 2.0 * l - var_2
  37.        r = 255 * Hue_2_RGB( var_1, var_2, h + ( 1.0 / 3.0 ) )
  38.        g = 255 * Hue_2_RGB( var_1, var_2, h )
  39.        b = 255 * Hue_2_RGB( var_1, var_2, h - ( 1.0 / 3.0 ) )
  40.      
  41.     return (int(round(r)),int(round(g)),int(round(b)))
  42.  
  43.  
  44. def RGB_to_HSL(r,g,b):
  45.     ''' Converts RGB colorspace to HSL (Hue/Saturation/Value) colorspace.
  46.        Formula from http://www.easyrgb.com/math.php?MATH=M18#text18
  47.      
  48.        Input:
  49.            (r,g,b) (integers 0...255) : RGB values
  50.      
  51.        Ouput:
  52.            (h,s,l) (floats 0...1): corresponding HSL values
  53.      
  54.        Example:
  55.            >>> print RGB_to_HSL(110,82,224)
  56.            (0.69953051643192476, 0.69607843137254899, 0.59999999999999998)
  57.            >>> h,s,l = RGB_to_HSL(110,82,224)
  58.            >>> print s
  59.            0.696078431373
  60.    '''
  61.     if not (0 <= r <=255): raise ValueError,"r (red) parameter must be between 0 and 255."
  62.     if not (0 <= g <=255): raise ValueError,"g (green) parameter must be between 0 and 255."
  63.     if not (0 <= b <=255): raise ValueError,"b (blue) parameter must be between 0 and 255."
  64.    
  65.     var_R = r/255.0
  66.     var_G = g/255.0
  67.     var_B = b/255.0
  68.    
  69.     var_Min = min( var_R, var_G, var_B )    # Min. value of RGB
  70.     var_Max = max( var_R, var_G, var_B )    # Max. value of RGB
  71.     del_Max = var_Max - var_Min             # Delta RGB value
  72.    
  73.     l = ( var_Max + var_Min ) / 2.0
  74.     h = 0.0
  75.     s = 0.0
  76.     if del_Max!=0.0:
  77.        if l<0.5: s = del_Max / ( var_Max + var_Min )
  78.        else:     s = del_Max / ( 2.0 - var_Max - var_Min )
  79.        del_R = ( ( ( var_Max - var_R ) / 6.0 ) + ( del_Max / 2.0 ) ) / del_Max
  80.        del_G = ( ( ( var_Max - var_G ) / 6.0 ) + ( del_Max / 2.0 ) ) / del_Max
  81.        del_B = ( ( ( var_Max - var_B ) / 6.0 ) + ( del_Max / 2.0 ) ) / del_Max
  82.        if    var_R == var_Max : h = del_B - del_G
  83.        elif  var_G == var_Max : h = ( 1.0 / 3.0 ) + del_R - del_B
  84.        elif  var_B == var_Max : h = ( 2.0 / 3.0 ) + del_G - del_R
  85.        while h < 0.0: h += 1.0
  86.        while h > 1.0: h -= 1.0
  87.      
  88.     return (h,s,l)
  89. #//python/1867

Reply to "Conversion between RGB and HSL in Python"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website