colorsys.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. # References:
  15. # http://en.wikipedia.org/wiki/YIQ
  16. # http://en.wikipedia.org/wiki/HLS_color_space
  17. # http://en.wikipedia.org/wiki/HSV_color_space
  18. __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
  19. "rgb_to_hsv","hsv_to_rgb"]
  20. # Some floating point constants
  21. ONE_THIRD = 1.0/3.0
  22. ONE_SIXTH = 1.0/6.0
  23. TWO_THIRD = 2.0/3.0
  24. # YIQ: used by composite video signals (linear combinations of RGB)
  25. # Y: perceived grey level (0.0 == black, 1.0 == white)
  26. # I, Q: color components
  27. #
  28. # There are a great many versions of the constants used in these formulae.
  29. # The ones in this library uses constants from the FCC version of NTSC.
  30. def rgb_to_yiq(r, g, b):
  31. y = 0.30*r + 0.59*g + 0.11*b
  32. i = 0.74*(r-y) - 0.27*(b-y)
  33. q = 0.48*(r-y) + 0.41*(b-y)
  34. return (y, i, q)
  35. def yiq_to_rgb(y, i, q):
  36. # r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)
  37. # b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)
  38. # g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
  39. r = y + 0.9468822170900693*i + 0.6235565819861433*q
  40. g = y - 0.27478764629897834*i - 0.6356910791873801*q
  41. b = y - 1.1085450346420322*i + 1.7090069284064666*q
  42. if r < 0.0:
  43. r = 0.0
  44. if g < 0.0:
  45. g = 0.0
  46. if b < 0.0:
  47. b = 0.0
  48. if r > 1.0:
  49. r = 1.0
  50. if g > 1.0:
  51. g = 1.0
  52. if b > 1.0:
  53. b = 1.0
  54. return (r, g, b)
  55. # HLS: Hue, Luminance, Saturation
  56. # H: position in the spectrum
  57. # L: color lightness
  58. # S: color saturation
  59. def rgb_to_hls(r, g, b):
  60. maxc = max(r, g, b)
  61. minc = min(r, g, b)
  62. sumc = (maxc+minc)
  63. rangec = (maxc-minc)
  64. l = sumc/2.0
  65. if minc == maxc:
  66. return 0.0, l, 0.0
  67. if l <= 0.5:
  68. s = rangec / sumc
  69. else:
  70. s = rangec / (2.0-maxc-minc) # Not always 2.0-sumc: gh-106498.
  71. rc = (maxc-r) / rangec
  72. gc = (maxc-g) / rangec
  73. bc = (maxc-b) / rangec
  74. if r == maxc:
  75. h = bc-gc
  76. elif g == maxc:
  77. h = 2.0+rc-bc
  78. else:
  79. h = 4.0+gc-rc
  80. h = (h/6.0) % 1.0
  81. return h, l, s
  82. def hls_to_rgb(h, l, s):
  83. if s == 0.0:
  84. return l, l, l
  85. if l <= 0.5:
  86. m2 = l * (1.0+s)
  87. else:
  88. m2 = l+s-(l*s)
  89. m1 = 2.0*l - m2
  90. return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
  91. def _v(m1, m2, hue):
  92. hue = hue % 1.0
  93. if hue < ONE_SIXTH:
  94. return m1 + (m2-m1)*hue*6.0
  95. if hue < 0.5:
  96. return m2
  97. if hue < TWO_THIRD:
  98. return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
  99. return m1
  100. # HSV: Hue, Saturation, Value
  101. # H: position in the spectrum
  102. # S: color saturation ("purity")
  103. # V: color brightness
  104. def rgb_to_hsv(r, g, b):
  105. maxc = max(r, g, b)
  106. minc = min(r, g, b)
  107. rangec = (maxc-minc)
  108. v = maxc
  109. if minc == maxc:
  110. return 0.0, 0.0, v
  111. s = rangec / maxc
  112. rc = (maxc-r) / rangec
  113. gc = (maxc-g) / rangec
  114. bc = (maxc-b) / rangec
  115. if r == maxc:
  116. h = bc-gc
  117. elif g == maxc:
  118. h = 2.0+rc-bc
  119. else:
  120. h = 4.0+gc-rc
  121. h = (h/6.0) % 1.0
  122. return h, s, v
  123. def hsv_to_rgb(h, s, v):
  124. if s == 0.0:
  125. return v, v, v
  126. i = int(h*6.0) # XXX assume int() truncates!
  127. f = (h*6.0) - i
  128. p = v*(1.0 - s)
  129. q = v*(1.0 - s*f)
  130. t = v*(1.0 - s*(1.0-f))
  131. i = i%6
  132. if i == 0:
  133. return v, t, p
  134. if i == 1:
  135. return q, v, p
  136. if i == 2:
  137. return p, v, t
  138. if i == 3:
  139. return p, q, v
  140. if i == 4:
  141. return t, p, v
  142. if i == 5:
  143. return v, p, q
  144. # Cannot get here