Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

GimpGradientFile.py 3.3KB

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