Skip to content

Commit 3244212

Browse files
got a multiple color animation test to work.
1 parent 9a735cb commit 3244212

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

py_gd/color_ramp.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def __init__(self,
4242
self.start_index = len(base_colorscheme)
4343
except TypeError: # it should be an integer now
4444
self.start_index = base_colorscheme
45-
# else:
46-
# self.start_index = len(base_colorscheme)
4745

4846
self._num_colors = 256 - self.start_index if num_colors is None else num_colors
4947
self._delta = (self.max_value - self.min_value) / self._num_colors
5048

49+
self.name = 'custom'
5150
if isinstance(colors, str):
51+
self.name = colors
5252
colors = [c[1] for c in colorschemes[colors]]
5353
# otherwise assume it's in the right form
5454
if reversed:
@@ -104,7 +104,11 @@ def colorlist(self):
104104
"""
105105
returns a list of colors as needed by Image.add_colors
106106
"""
107-
def make_str(color):
108-
return str(color)
109-
return [(make_str(c), tuple(c)) for c in self.color_index]
107+
colorlist = [(f"{self.name}-{i}", tuple(int(i) for i in c)) for i, c in enumerate(self.color_index)]
110108

109+
# for i, c in enumerate(self.color_index):
110+
# values = tuple(int(i) for i in c)
111+
# name = f"{self.name}-{i}"
112+
# colorlist.append((name, values))
113+
114+
return colorlist

py_gd/py_gd.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ cdef class Animation:
15031503
else:
15041504
if self.prev_frame is not None:
15051505
prev = self.prev_frame._image
1506-
# really wierd gd flag values!
1506+
# really weird gd flag values!
15071507
local_colormap = 0 if self._global_colormap == 1 else 1
15081508
gdImageGifAnimAdd(self.cur_frame._image,
15091509
self._fp,

py_gd/test/test_animation.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,22 @@ def test_animation_multi_images_colors():
113113

114114
cr = ColorRamp('viridis', 0, 36, base_colorscheme=existing_colors)
115115
ramp_colors = cr.colorlist
116-
print(ramp_colors)
117-
print(len(ramp_colors))
118-
print(len(set(ramp_colors)))
119116

120117
first_frame.add_colors(ramp_colors)
121118

122-
# Initial frame:
123-
# file_name, delay=50, global_colormap=1
124-
anim = Animation(outfile("test_animation_multi_colors.gif"), delay=100, global_colormap=0)
119+
# Initial frame: file_name, delay=50, global_colormap=1
120+
121+
# fixme: this could use a global colormap, but we'd need to make sure to
122+
# copy it correctly. From the ligd docs:
123+
124+
# gdImageGifAnimAdd
125+
# Setting the LocalCM flag to 1 adds a local palette for this image to the
126+
# animation. Otherwise the global palette is assumed and the user must
127+
# make sure the palettes match. Use gdImagePaletteCopy to do that.
128+
129+
anim = Animation(outfile("test_animation_multi_colors.gif"),
130+
delay=20,
131+
global_colormap=0)
125132

126133
anim.begin_anim(first_frame, 0)
127134

@@ -136,7 +143,7 @@ def test_animation_multi_images_colors():
136143

137144
last_frame = Image(200, 200, preset_colors='BW')
138145
last_frame.add_colors(ramp_colors)
139-
last_frame.draw_line(np.array((0, 100)), np.array((200, 100)), 'green')
146+
last_frame.draw_line(np.array((0, 100)), np.array((200, 100)), cr.get_color_indices([count+1])[0])
140147
anim.add_frame(last_frame)
141148
count += 1
142149

py_gd/test/test_color_ramp.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,15 @@ def test_colorlist():
9292

9393
colorlist = cr.colorlist
9494

95+
print(colorlist)
96+
9597
for c in colorlist:
9698
assert isinstance(c[0], str)
9799
assert len(c[1]) == 3
98100

101+
# make sure there aren't any duplicates
102+
# Image doesn't let you add duplicate color names
103+
assert len(set(colorlist)) == len(colorlist)
99104

100105
def test_image_with_colorramp():
101106
"""

0 commit comments

Comments
 (0)