github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/colorsys.py (about)

     1  """Conversion functions between RGB and other color systems.
     2  This modules provides two functions for each color system ABC:
     3    rgb_to_abc(r, g, b) --> a, b, c
     4    abc_to_rgb(a, b, c) --> r, g, b
     5  All inputs and outputs are triples of floats in the range [0.0...1.0]
     6  (with the exception of I and Q, which covers a slightly larger range).
     7  Inputs outside the valid range may cause exceptions or invalid outputs.
     8  Supported color systems:
     9  RGB: Red, Green, Blue components
    10  YIQ: Luminance, Chrominance (used by composite video signals)
    11  HLS: Hue, Luminance, Saturation
    12  HSV: Hue, Saturation, Value
    13  """
    14  
    15  # References:
    16  # http://en.wikipedia.org/wiki/YIQ
    17  # http://en.wikipedia.org/wiki/HLS_color_space
    18  # http://en.wikipedia.org/wiki/HSV_color_space
    19  
    20  __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
    21             "rgb_to_hsv","hsv_to_rgb"]
    22  
    23  # Some floating point constants
    24  
    25  ONE_THIRD = 1.0/3.0
    26  ONE_SIXTH = 1.0/6.0
    27  TWO_THIRD = 2.0/3.0
    28  
    29  # YIQ: used by composite video signals (linear combinations of RGB)
    30  # Y: perceived grey level (0.0 == black, 1.0 == white)
    31  # I, Q: color components
    32  #
    33  # There are a great many versions of the constants used in these formulae.
    34  # The ones in this library uses constants from the FCC version of NTSC.
    35  
    36  def rgb_to_yiq(r, g, b):
    37      y = 0.30*r + 0.59*g + 0.11*b
    38      i = 0.74*(r-y) - 0.27*(b-y)
    39      q = 0.48*(r-y) + 0.41*(b-y)
    40      return (y, i, q)
    41  
    42  def yiq_to_rgb(y, i, q):
    43      # r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)
    44      # b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)
    45      # g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
    46  
    47      r = y + 0.9468822170900693*i + 0.6235565819861433*q
    48      g = y - 0.27478764629897834*i - 0.6356910791873801*q
    49      b = y - 1.1085450346420322*i + 1.7090069284064666*q
    50  
    51      if r < 0.0:
    52          r = 0.0
    53      if g < 0.0:
    54          g = 0.0
    55      if b < 0.0:
    56          b = 0.0
    57      if r > 1.0:
    58          r = 1.0
    59      if g > 1.0:
    60          g = 1.0
    61      if b > 1.0:
    62          b = 1.0
    63      return (r, g, b)
    64  
    65  
    66  # HLS: Hue, Luminance, Saturation
    67  # H: position in the spectrum
    68  # L: color lightness
    69  # S: color saturation
    70  
    71  def rgb_to_hls(r, g, b):
    72      maxc = max(r, g, b)
    73      minc = min(r, g, b)
    74      # XXX Can optimize (maxc+minc) and (maxc-minc)
    75      l = (minc+maxc)/2.0
    76      if minc == maxc:
    77          return 0.0, l, 0.0
    78      if l <= 0.5:
    79          s = (maxc-minc) / (maxc+minc)
    80      else:
    81          s = (maxc-minc) / (2.0-maxc-minc)
    82      rc = (maxc-r) / (maxc-minc)
    83      gc = (maxc-g) / (maxc-minc)
    84      bc = (maxc-b) / (maxc-minc)
    85      if r == maxc:
    86          h = bc-gc
    87      elif g == maxc:
    88          h = 2.0+rc-bc
    89      else:
    90          h = 4.0+gc-rc
    91      h = (h/6.0) % 1.0
    92      return h, l, s
    93  
    94  def hls_to_rgb(h, l, s):
    95      if s == 0.0:
    96          return l, l, l
    97      if l <= 0.5:
    98          m2 = l * (1.0+s)
    99      else:
   100          m2 = l+s-(l*s)
   101      m1 = 2.0*l - m2
   102      return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
   103  
   104  def _v(m1, m2, hue):
   105      hue = hue % 1.0
   106      if hue < ONE_SIXTH:
   107          return m1 + (m2-m1)*hue*6.0
   108      if hue < 0.5:
   109          return m2
   110      if hue < TWO_THIRD:
   111          return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
   112      return m1
   113  
   114  
   115  # HSV: Hue, Saturation, Value
   116  # H: position in the spectrum
   117  # S: color saturation ("purity")
   118  # V: color brightness
   119  
   120  def rgb_to_hsv(r, g, b):
   121      maxc = max(r, g, b)
   122      minc = min(r, g, b)
   123      v = maxc
   124      if minc == maxc:
   125          return 0.0, 0.0, v
   126      s = (maxc-minc) / maxc
   127      rc = (maxc-r) / (maxc-minc)
   128      gc = (maxc-g) / (maxc-minc)
   129      bc = (maxc-b) / (maxc-minc)
   130      if r == maxc:
   131          h = bc-gc
   132      elif g == maxc:
   133          h = 2.0+rc-bc
   134      else:
   135          h = 4.0+gc-rc
   136      h = (h/6.0) % 1.0
   137      return h, s, v
   138  
   139  def hsv_to_rgb(h, s, v):
   140      if s == 0.0:
   141          return v, v, v
   142      i = int(h*6.0) # XXX assume int() truncates!
   143      f = (h*6.0) - i
   144      p = v*(1.0 - s)
   145      q = v*(1.0 - s*f)
   146      t = v*(1.0 - s*(1.0-f))
   147      i = i%6
   148      if i == 0:
   149          return v, t, p
   150      if i == 1:
   151          return q, v, p
   152      if i == 2:
   153          return p, v, t
   154      if i == 3:
   155          return p, q, v
   156      if i == 4:
   157          return t, p, v
   158      if i == 5:
   159          return v, p, q