Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/GimpGradientFile.py: 21%

66 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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""" 

17Stuff to translate curve segments to palette values (derived from 

18the corresponding code in GIMP, written by Federico Mena Quintero. 

19See the GIMP distribution for more information.) 

20""" 

21 

22 

23from math import log, pi, sin, sqrt 

24 

25from ._binary import o8 

26 

27EPSILON = 1e-10 

28"""""" # Enable auto-doc for data member 

29 

30 

31def linear(middle, pos): 

32 if pos <= middle: 

33 if middle < EPSILON: 

34 return 0.0 

35 else: 

36 return 0.5 * pos / middle 

37 else: 

38 pos = pos - middle 

39 middle = 1.0 - middle 

40 if middle < EPSILON: 

41 return 1.0 

42 else: 

43 return 0.5 + 0.5 * pos / middle 

44 

45 

46def curved(middle, pos): 

47 return pos ** (log(0.5) / log(max(middle, EPSILON))) 

48 

49 

50def sine(middle, pos): 

51 return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0 

52 

53 

54def sphere_increasing(middle, pos): 

55 return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2) 

56 

57 

58def sphere_decreasing(middle, pos): 

59 return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2) 

60 

61 

62SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing] 

63"""""" # Enable auto-doc for data member 

64 

65 

66class GradientFile: 

67 

68 gradient = None 

69 

70 def getpalette(self, entries=256): 

71 

72 palette = [] 

73 

74 ix = 0 

75 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix] 

76 

77 for i in range(entries): 

78 

79 x = i / (entries - 1) 

80 

81 while x1 < x: 

82 ix += 1 

83 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix] 

84 

85 w = x1 - x0 

86 

87 if w < EPSILON: 

88 scale = segment(0.5, 0.5) 

89 else: 

90 scale = segment((xm - x0) / w, (x - x0) / w) 

91 

92 # expand to RGBA 

93 r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5)) 

94 g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5)) 

95 b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5)) 

96 a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5)) 

97 

98 # add to palette 

99 palette.append(r + g + b + a) 

100 

101 return b"".join(palette), "RGBA" 

102 

103 

104class GimpGradientFile(GradientFile): 

105 """File handler for GIMP's gradient format.""" 

106 

107 def __init__(self, fp): 

108 

109 if fp.readline()[:13] != b"GIMP Gradient": 

110 raise SyntaxError("not a GIMP gradient file") 

111 

112 line = fp.readline() 

113 

114 # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do 

115 if line.startswith(b"Name: "): 

116 line = fp.readline().strip() 

117 

118 count = int(line) 

119 

120 gradient = [] 

121 

122 for i in range(count): 

123 

124 s = fp.readline().split() 

125 w = [float(x) for x in s[:11]] 

126 

127 x0, x1 = w[0], w[2] 

128 xm = w[1] 

129 rgb0 = w[3:7] 

130 rgb1 = w[7:11] 

131 

132 segment = SEGMENTS[int(s[11])] 

133 cspace = int(s[12]) 

134 

135 if cspace != 0: 

136 raise OSError("cannot handle HSV colour space") 

137 

138 gradient.append((x0, x1, xm, rgb0, rgb1, segment)) 

139 

140 self.gradient = gradient