2121from __future__ import annotations
2222
2323from math import log , pi , sin , sqrt
24+ from typing import IO , Callable
2425
2526from ._binary import o8
2627
2728EPSILON = 1e-10
2829"""""" # Enable auto-doc for data member
2930
3031
31- def linear (middle , pos ) :
32+ def linear (middle : float , pos : float ) -> float :
3233 if pos <= middle :
3334 if middle < EPSILON :
3435 return 0.0
@@ -43,19 +44,19 @@ def linear(middle, pos):
4344 return 0.5 + 0.5 * pos / middle
4445
4546
46- def curved (middle , pos ) :
47+ def curved (middle : float , pos : float ) -> float :
4748 return pos ** (log (0.5 ) / log (max (middle , EPSILON )))
4849
4950
50- def sine (middle , pos ) :
51+ def sine (middle : float , pos : float ) -> float :
5152 return (sin ((- pi / 2.0 ) + pi * linear (middle , pos )) + 1.0 ) / 2.0
5253
5354
54- def sphere_increasing (middle , pos ) :
55+ def sphere_increasing (middle : float , pos : float ) -> float :
5556 return sqrt (1.0 - (linear (middle , pos ) - 1.0 ) ** 2 )
5657
5758
58- def sphere_decreasing (middle , pos ) :
59+ def sphere_decreasing (middle : float , pos : float ) -> float :
5960 return 1.0 - sqrt (1.0 - linear (middle , pos ) ** 2 )
6061
6162
@@ -64,9 +65,22 @@ def sphere_decreasing(middle, pos):
6465
6566
6667class GradientFile :
67- gradient = None
68-
69- def getpalette (self , entries = 256 ):
68+ gradient : (
69+ list [
70+ tuple [
71+ float ,
72+ float ,
73+ float ,
74+ list [float ],
75+ list [float ],
76+ Callable [[float , float ], float ],
77+ ]
78+ ]
79+ | None
80+ ) = None
81+
82+ def getpalette (self , entries : int = 256 ) -> tuple [bytes , str ]:
83+ assert self .gradient is not None
7084 palette = []
7185
7286 ix = 0
@@ -101,7 +115,7 @@ def getpalette(self, entries=256):
101115class GimpGradientFile (GradientFile ):
102116 """File handler for GIMP's gradient format."""
103117
104- def __init__ (self , fp ) :
118+ def __init__ (self , fp : IO [ bytes ]) -> None :
105119 if fp .readline ()[:13 ] != b"GIMP Gradient" :
106120 msg = "not a GIMP gradient file"
107121 raise SyntaxError (msg )
@@ -114,7 +128,7 @@ def __init__(self, fp):
114128
115129 count = int (line )
116130
117- gradient = []
131+ self . gradient = []
118132
119133 for i in range (count ):
120134 s = fp .readline ().split ()
@@ -132,6 +146,4 @@ def __init__(self, fp):
132146 msg = "cannot handle HSV colour space"
133147 raise OSError (msg )
134148
135- gradient .append ((x0 , x1 , xm , rgb0 , rgb1 , segment ))
136-
137- self .gradient = gradient
149+ self .gradient .append ((x0 , x1 , xm , rgb0 , rgb1 , segment ))
0 commit comments