1
0

colorsys.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. h = h / 6.0
  82. h = h - int(h)
  83. return h, l, s
  84. def hls_to_rgb(h, l, s):
  85. if s == 0.0:
  86. return l, l, l
  87. if l <= 0.5:
  88. m2 = l * (1.0+s)
  89. else:
  90. m2 = l+s-(l*s)
  91. m1 = 2.0*l - m2
  92. return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
  93. def _v(m1, m2, hue):
  94. # hue = hue % 1.0
  95. hue = hue - int(hue)
  96. if hue < ONE_SIXTH:
  97. return m1 + (m2-m1)*hue*6.0
  98. if hue < 0.5:
  99. return m2
  100. if hue < TWO_THIRD:
  101. return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
  102. return m1
  103. # HSV: Hue, Saturation, Value
  104. # H: position in the spectrum
  105. # S: color saturation ("purity")
  106. # V: color brightness
  107. def rgb_to_hsv(r, g, b):
  108. maxc = max(r, g, b)
  109. minc = min(r, g, b)
  110. rangec = (maxc-minc)
  111. v = maxc
  112. if minc == maxc:
  113. return 0.0, 0.0, v
  114. s = rangec / maxc
  115. rc = (maxc-r) / rangec
  116. gc = (maxc-g) / rangec
  117. bc = (maxc-b) / rangec
  118. if r == maxc:
  119. h = bc-gc
  120. elif g == maxc:
  121. h = 2.0+rc-bc
  122. else:
  123. h = 4.0+gc-rc
  124. # h = (h/6.0) % 1.0
  125. h = h / 6.0
  126. h = h - int(h)
  127. return h, s, v
  128. def hsv_to_rgb(h, s, v):
  129. if s == 0.0:
  130. return v, v, v
  131. i = int(h*6.0) # XXX assume int() truncates!
  132. f = (h*6.0) - i
  133. p = v*(1.0 - s)
  134. q = v*(1.0 - s*f)
  135. t = v*(1.0 - s*(1.0-f))
  136. i = i%6
  137. if i == 0:
  138. return v, t, p
  139. if i == 1:
  140. return q, v, p
  141. if i == 2:
  142. return p, v, t
  143. if i == 3:
  144. return p, q, v
  145. if i == 4:
  146. return t, p, v
  147. if i == 5:
  148. return v, p, q
  149. # Cannot get here