You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

colorsys.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Inputs outside this range may cause exceptions or invalid outputs.
  7. Supported color systems:
  8. RGB: Red, Green, Blue components
  9. YIQ: used by composite video signals
  10. HLS: Hue, Luminance, Saturation
  11. HSV: Hue, Saturation, Value
  12. """
  13. # References:
  14. # XXX Where's the literature?
  15. __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
  16. "rgb_to_hsv","hsv_to_rgb"]
  17. # Some floating point constants
  18. ONE_THIRD = 1.0/3.0
  19. ONE_SIXTH = 1.0/6.0
  20. TWO_THIRD = 2.0/3.0
  21. # YIQ: used by composite video signals (linear combinations of RGB)
  22. # Y: perceived grey level (0.0 == black, 1.0 == white)
  23. # I, Q: color components
  24. def rgb_to_yiq(r, g, b):
  25. y = 0.30*r + 0.59*g + 0.11*b
  26. i = 0.60*r - 0.28*g - 0.32*b
  27. q = 0.21*r - 0.52*g + 0.31*b
  28. return (y, i, q)
  29. def yiq_to_rgb(y, i, q):
  30. r = y + 0.948262*i + 0.624013*q
  31. g = y - 0.276066*i - 0.639810*q
  32. b = y - 1.105450*i + 1.729860*q
  33. if r < 0.0: r = 0.0
  34. if g < 0.0: g = 0.0
  35. if b < 0.0: b = 0.0
  36. if r > 1.0: r = 1.0
  37. if g > 1.0: g = 1.0
  38. if b > 1.0: b = 1.0
  39. return (r, g, b)
  40. # HLS: Hue, Luminance, S???
  41. # H: position in the spectrum
  42. # L: ???
  43. # S: ???
  44. def rgb_to_hls(r, g, b):
  45. maxc = max(r, g, b)
  46. minc = min(r, g, b)
  47. # XXX Can optimize (maxc+minc) and (maxc-minc)
  48. l = (minc+maxc)/2.0
  49. if minc == maxc: return 0.0, l, 0.0
  50. if l <= 0.5: s = (maxc-minc) / (maxc+minc)
  51. else: s = (maxc-minc) / (2.0-maxc-minc)
  52. rc = (maxc-r) / (maxc-minc)
  53. gc = (maxc-g) / (maxc-minc)
  54. bc = (maxc-b) / (maxc-minc)
  55. if r == maxc: h = bc-gc
  56. elif g == maxc: h = 2.0+rc-bc
  57. else: h = 4.0+gc-rc
  58. h = (h/6.0) % 1.0
  59. return h, l, s
  60. def hls_to_rgb(h, l, s):
  61. if s == 0.0: return l, l, l
  62. if l <= 0.5: m2 = l * (1.0+s)
  63. else: m2 = l+s-(l*s)
  64. m1 = 2.0*l - m2
  65. return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
  66. def _v(m1, m2, hue):
  67. hue = hue % 1.0
  68. if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0
  69. if hue < 0.5: return m2
  70. if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
  71. return m1
  72. # HSV: Hue, Saturation, Value(?)
  73. # H: position in the spectrum
  74. # S: ???
  75. # V: ???
  76. def rgb_to_hsv(r, g, b):
  77. maxc = max(r, g, b)
  78. minc = min(r, g, b)
  79. v = maxc
  80. if minc == maxc: return 0.0, 0.0, v
  81. s = (maxc-minc) / maxc
  82. rc = (maxc-r) / (maxc-minc)
  83. gc = (maxc-g) / (maxc-minc)
  84. bc = (maxc-b) / (maxc-minc)
  85. if r == maxc: h = bc-gc
  86. elif g == maxc: h = 2.0+rc-bc
  87. else: h = 4.0+gc-rc
  88. h = (h/6.0) % 1.0
  89. return h, s, v
  90. def hsv_to_rgb(h, s, v):
  91. if s == 0.0: return v, v, v
  92. i = int(h*6.0) # XXX assume int() truncates!
  93. f = (h*6.0) - i
  94. p = v*(1.0 - s)
  95. q = v*(1.0 - s*f)
  96. t = v*(1.0 - s*(1.0-f))
  97. if i%6 == 0: return v, t, p
  98. if i == 1: return q, v, p
  99. if i == 2: return p, v, t
  100. if i == 3: return p, q, v
  101. if i == 4: return t, p, v
  102. if i == 5: return v, p, q
  103. # Cannot get here