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
« 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#
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"""
23from math import log, pi, sin, sqrt
25from ._binary import o8
27EPSILON = 1e-10
28"""""" # Enable auto-doc for data member
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
46def curved(middle, pos):
47 return pos ** (log(0.5) / log(max(middle, EPSILON)))
50def sine(middle, pos):
51 return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
54def sphere_increasing(middle, pos):
55 return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
58def sphere_decreasing(middle, pos):
59 return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
62SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
63"""""" # Enable auto-doc for data member
66class GradientFile:
68 gradient = None
70 def getpalette(self, entries=256):
72 palette = []
74 ix = 0
75 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
77 for i in range(entries):
79 x = i / (entries - 1)
81 while x1 < x:
82 ix += 1
83 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
85 w = x1 - x0
87 if w < EPSILON:
88 scale = segment(0.5, 0.5)
89 else:
90 scale = segment((xm - x0) / w, (x - x0) / w)
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))
98 # add to palette
99 palette.append(r + g + b + a)
101 return b"".join(palette), "RGBA"
104class GimpGradientFile(GradientFile):
105 """File handler for GIMP's gradient format."""
107 def __init__(self, fp):
109 if fp.readline()[:13] != b"GIMP Gradient":
110 raise SyntaxError("not a GIMP gradient file")
112 line = fp.readline()
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()
118 count = int(line)
120 gradient = []
122 for i in range(count):
124 s = fp.readline().split()
125 w = [float(x) for x in s[:11]]
127 x0, x1 = w[0], w[2]
128 xm = w[1]
129 rgb0 = w[3:7]
130 rgb1 = w[7:11]
132 segment = SEGMENTS[int(s[11])]
133 cspace = int(s[12])
135 if cspace != 0:
136 raise OSError("cannot handle HSV colour space")
138 gradient.append((x0, x1, xm, rgb0, rgb1, segment))
140 self.gradient = gradient