CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
GimpGradientFile.py154 linesDownload Raw Back to PIL
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"""
21from __future__ import annotations
22
23from math import log, pi, sin, sqrt
24
25from ._binary import o8
26
27TYPE_CHECKING = False
28if TYPE_CHECKING:
29    from collections.abc import Callable
30    from typing import IO
31
32EPSILON = 1e-10
33""""""  # Enable auto-doc for data member
34
35
36def linear(middle: float, pos: float) -> float:
37    if pos <= middle:
38        if middle < EPSILON:
39            return 0.0
40        else:
41            return 0.5 * pos / middle
42    else:
43        pos = pos - middle
44        middle = 1.0 - middle
45        if middle < EPSILON:
46            return 1.0
47        else:
48            return 0.5 + 0.5 * pos / middle
49
50
51def curved(middle: float, pos: float) -> float:
52    return pos ** (log(0.5) / log(max(middle, EPSILON)))
53
54
55def sine(middle: float, pos: float) -> float:
56    return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
57
58
59def sphere_increasing(middle: float, pos: float) -> float:
60    return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
61
62
63def sphere_decreasing(middle: float, pos: float) -> float:
64    return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
65
66
67SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
68""""""  # Enable auto-doc for data member
69
70
71class GradientFile:
72    gradient: (
73        list[
74            tuple[
75                float,
76                float,
77                float,
78                list[float],
79                list[float],
80                Callable[[float, float], float],
81            ]
82        ]
83        | None
84    ) = None
85
86    def getpalette(self, entries: int = 256) -> tuple[bytes, str]:
87        assert self.gradient is not None
88        palette = []
89
90        ix = 0
91        x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
92
93        for i in range(entries):
94            x = i / (entries - 1)
95
96            while x1 < x:
97                ix += 1
98                x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
99
100            w = x1 - x0
101
102            if w < EPSILON:
103                scale = segment(0.5, 0.5)
104            else:
105                scale = segment((xm - x0) / w, (x - x0) / w)
106
107            # expand to RGBA
108            r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
109            g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
110            b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
111            a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
112
113            # add to palette
114            palette.append(r + g + b + a)
115
116        return b"".join(palette), "RGBA"
117
118
119class GimpGradientFile(GradientFile):
120    """File handler for GIMP's gradient format."""
121
122    def __init__(self, fp: IO[bytes]) -> None:
123        if not fp.readline().startswith(b"GIMP Gradient"):
124            msg = "not a GIMP gradient file"
125            raise SyntaxError(msg)
126
127        line = fp.readline()
128
129        # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
130        if line.startswith(b"Name: "):
131            line = fp.readline().strip()
132
133        count = int(line)
134
135        self.gradient = []
136
137        for i in range(count):
138            s = fp.readline().split()
139            w = [float(x) for x in s[:11]]
140
141            x0, x1 = w[0], w[2]
142            xm = w[1]
143            rgb0 = w[3:7]
144            rgb1 = w[7:11]
145
146            segment = SEGMENTS[int(s[11])]
147            cspace = int(s[12])
148
149            if cspace != 0:
150                msg = "cannot handle HSV colour space"
151                raise OSError(msg)
152
153            self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))
154 
Aluode/PerceptionLabPortable · CoolFace