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.

138 lines
3.3 KiB

4 years ago
  1. #
  2. # Python Imaging Library
  3. # $Id$
  4. #
  5. # stuff to read (and render) GIMP gradient files
  6. #
  7. # History:
  8. # 97-08-23 fl Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1997.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15. from math import pi, log, sin, sqrt
  16. from ._binary import o8
  17. # --------------------------------------------------------------------
  18. # Stuff to translate curve segments to palette values (derived from
  19. # the corresponding code in GIMP, written by Federico Mena Quintero.
  20. # See the GIMP distribution for more information.)
  21. #
  22. EPSILON = 1e-10
  23. def linear(middle, pos):
  24. if pos <= middle:
  25. if middle < EPSILON:
  26. return 0.0
  27. else:
  28. return 0.5 * pos / middle
  29. else:
  30. pos = pos - middle
  31. middle = 1.0 - middle
  32. if middle < EPSILON:
  33. return 1.0
  34. else:
  35. return 0.5 + 0.5 * pos / middle
  36. def curved(middle, pos):
  37. return pos ** (log(0.5) / log(max(middle, EPSILON)))
  38. def sine(middle, pos):
  39. return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
  40. def sphere_increasing(middle, pos):
  41. return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
  42. def sphere_decreasing(middle, pos):
  43. return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
  44. SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
  45. class GradientFile(object):
  46. gradient = None
  47. def getpalette(self, entries=256):
  48. palette = []
  49. ix = 0
  50. x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  51. for i in range(entries):
  52. x = i / float(entries-1)
  53. while x1 < x:
  54. ix += 1
  55. x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  56. w = x1 - x0
  57. if w < EPSILON:
  58. scale = segment(0.5, 0.5)
  59. else:
  60. scale = segment((xm - x0) / w, (x - x0) / w)
  61. # expand to RGBA
  62. r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
  63. g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
  64. b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
  65. a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
  66. # add to palette
  67. palette.append(r + g + b + a)
  68. return b"".join(palette), "RGBA"
  69. ##
  70. # File handler for GIMP's gradient format.
  71. class GimpGradientFile(GradientFile):
  72. def __init__(self, fp):
  73. if fp.readline()[:13] != b"GIMP Gradient":
  74. raise SyntaxError("not a GIMP gradient file")
  75. line = fp.readline()
  76. # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
  77. if line.startswith(b"Name: "):
  78. line = fp.readline().strip()
  79. count = int(line)
  80. gradient = []
  81. for i in range(count):
  82. s = fp.readline().split()
  83. w = [float(x) for x in s[:11]]
  84. x0, x1 = w[0], w[2]
  85. xm = w[1]
  86. rgb0 = w[3:7]
  87. rgb1 = w[7:11]
  88. segment = SEGMENTS[int(s[11])]
  89. cspace = int(s[12])
  90. if cspace != 0:
  91. raise IOError("cannot handle HSV colour space")
  92. gradient.append((x0, x1, xm, rgb0, rgb1, segment))
  93. self.gradient = gradient