logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

colorsys.py (4017B)


  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-sumc)
  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. v = maxc
  108. if minc == maxc:
  109. return 0.0, 0.0, v
  110. s = (maxc-minc) / maxc
  111. rc = (maxc-r) / (maxc-minc)
  112. gc = (maxc-g) / (maxc-minc)
  113. bc = (maxc-b) / (maxc-minc)
  114. if r == maxc:
  115. h = bc-gc
  116. elif g == maxc:
  117. h = 2.0+rc-bc
  118. else:
  119. h = 4.0+gc-rc
  120. h = (h/6.0) % 1.0
  121. return h, s, v
  122. def hsv_to_rgb(h, s, v):
  123. if s == 0.0:
  124. return v, v, v
  125. i = int(h*6.0) # XXX assume int() truncates!
  126. f = (h*6.0) - i
  127. p = v*(1.0 - s)
  128. q = v*(1.0 - s*f)
  129. t = v*(1.0 - s*(1.0-f))
  130. i = i%6
  131. if i == 0:
  132. return v, t, p
  133. if i == 1:
  134. return q, v, p
  135. if i == 2:
  136. return p, v, t
  137. if i == 3:
  138. return p, q, v
  139. if i == 4:
  140. return t, p, v
  141. if i == 5:
  142. return v, p, q
  143. # Cannot get here