Skip to content

Commit c8f7a14

Browse files
authored
Merge pull request #931
[BUG] ColorsGenerator never returns random_colors
2 parents 84d4f7e + d4a8126 commit c8f7a14

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

gempy/core/color_generator.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
from functools import cached_property
23
from typing import Optional
34

45
import numpy as np
@@ -36,11 +37,10 @@ def __init__(self):
3637
@staticmethod
3738
def _random_hexcolor() -> str:
3839
"""Generates a random hex color string."""
39-
return "#"+str(hex(np.random.randint(0, 16777215))).lstrip("0x")
40+
return f"#{np.random.randint(0, 0xffffff):06x}"
4041

4142
def regenerate_color_palette(self, seaborn_palettes: Optional[list[str]] = None):
4243
try:
43-
4444
import seaborn as sns
4545
seaborn_installed = True
4646
except ImportError:
@@ -54,7 +54,7 @@ def regenerate_color_palette(self, seaborn_palettes: Optional[list[str]] = None)
5454
elif seaborn_palettes and not seaborn_installed:
5555
raise ImportError("Seaborn is not installed. Please install it to use color palettes.")
5656
else:
57-
hex_colors = self._gempy_default_colors
57+
hex_colors = list(self._gempy_default_colors)
5858

5959
self.hex_colors = hex_colors
6060

@@ -65,14 +65,18 @@ def __iter__(self) -> 'ColorsGenerator':
6565

6666
def __next__(self) -> str:
6767
"""Generator that yields the next color."""
68-
for color in self.hex_colors:
69-
result = self.hex_colors[self._index]
70-
self._index += 1
71-
return result
68+
color = self.up_next()
69+
self._index += 1
70+
del self._next_color
71+
return color
7272

73-
while True:
74-
return self._random_hexcolor()
75-
7673
def up_next(self) -> str:
7774
"""Returns the next color without incrementing the index."""
78-
return self.hex_colors[self._index]
75+
return self._next_color
76+
77+
@cached_property
78+
def _next_color(self) -> str:
79+
if self._index < len(self.hex_colors):
80+
return self.hex_colors[self._index]
81+
82+
return self._random_hexcolor()
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from gempy.core.color_generator import ColorsGenerator
1+
import gempy.core.color_generator as cg
22

33

44
def test_color_generator():
55

66
# create an instance of ColorsGenerator
7-
color_iterator: iter = ColorsGenerator()
7+
color_iterator: iter = cg.ColorsGenerator()
88

99
# generate color dictionary (with optional seaborn palettes if you want)
1010

@@ -13,4 +13,32 @@ def test_color_generator():
1313
# now you can get colors one by one
1414
print(next(color_iterator)) # prints first color
1515
print(next(color_iterator)) # prints second color
16+
17+
left_colors = color_iterator._gempy_default_colors[2:]
18+
19+
assert left_colors == [
20+
color for color, _ in zip(color_iterator, left_colors)
21+
]
22+
23+
# get a random color
24+
assert color_iterator.up_next() == next(color_iterator)
25+
26+
# override the random color generator
27+
cg.np.random.randint = lambda a, b: 0x00ff42
28+
29+
assert color_iterator._random_hexcolor() == "#00ff42"
30+
assert next(color_iterator) == "#00ff42"
31+
32+
# trigger up_next method for the cache
33+
assert color_iterator.up_next() == "#00ff42"
34+
35+
cg.np.random.randint = lambda a, b: 0x000000
36+
37+
# ensure that the up_next method still returns the same color
38+
assert color_iterator.up_next() == "#00ff42"
39+
40+
# trigger the cache
41+
assert next(color_iterator) == "#00ff42"
42+
assert next(color_iterator) == "#000000"
43+
1644
# etc...

0 commit comments

Comments
 (0)