Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/GimpGradientFile.py: 26%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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"""
22from __future__ import annotations
24from math import log, pi, sin, sqrt
26from ._binary import o8
28TYPE_CHECKING = False
29if TYPE_CHECKING:
30 from collections.abc import Callable
31 from typing import IO
33EPSILON = 1e-10
34"""""" # Enable auto-doc for data member
37def linear(middle: float, pos: float) -> float:
38 if pos <= middle:
39 if middle < EPSILON:
40 return 0.0
41 else:
42 return 0.5 * pos / middle
43 else:
44 pos = pos - middle
45 middle = 1.0 - middle
46 if middle < EPSILON:
47 return 1.0
48 else:
49 return 0.5 + 0.5 * pos / middle
52def curved(middle: float, pos: float) -> float:
53 return pos ** (log(0.5) / log(max(middle, EPSILON)))
56def sine(middle: float, pos: float) -> float:
57 return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
60def sphere_increasing(middle: float, pos: float) -> float:
61 return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
64def sphere_decreasing(middle: float, pos: float) -> float:
65 return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
68SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
69"""""" # Enable auto-doc for data member
72class GradientFile:
73 gradient: (
74 list[
75 tuple[
76 float,
77 float,
78 float,
79 list[float],
80 list[float],
81 Callable[[float, float], float],
82 ]
83 ]
84 | None
85 ) = None
87 def getpalette(self, entries: int = 256) -> tuple[bytes, str]:
88 assert self.gradient is not None
89 palette = []
91 ix = 0
92 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
94 for i in range(entries):
95 x = i / (entries - 1)
97 while x1 < x:
98 ix += 1
99 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
101 w = x1 - x0
103 if w < EPSILON:
104 scale = segment(0.5, 0.5)
105 else:
106 scale = segment((xm - x0) / w, (x - x0) / w)
108 # expand to RGBA
109 r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
110 g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
111 b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
112 a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
114 # add to palette
115 palette.append(r + g + b + a)
117 return b"".join(palette), "RGBA"
120class GimpGradientFile(GradientFile):
121 """File handler for GIMP's gradient format."""
123 def __init__(self, fp: IO[bytes]) -> None:
124 if not fp.readline().startswith(b"GIMP Gradient"):
125 msg = "not a GIMP gradient file"
126 raise SyntaxError(msg)
128 line = fp.readline()
130 # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
131 if line.startswith(b"Name: "):
132 line = fp.readline().strip()
134 count = int(line)
136 self.gradient = []
138 for i in range(count):
139 s = fp.readline().split()
140 w = [float(x) for x in s[:11]]
142 x0, x1 = w[0], w[2]
143 xm = w[1]
144 rgb0 = w[3:7]
145 rgb1 = w[7:11]
147 segment = SEGMENTS[int(s[11])]
148 cspace = int(s[12])
150 if cspace != 0:
151 msg = "cannot handle HSV colour space"
152 raise OSError(msg)
154 self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))