Skip to content

Commit fee9b06

Browse files
authored
🔀 Merge pull request #9 from davep/more-colour
Add a couple more colour options
2 parents 0a12fff + 4fcf2fb commit fee9b06

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Complexitty ChangeLog
22

3+
## v0.4.0
4+
5+
**Released: WiP**
6+
7+
- Added a couple more colour options.
8+
([#9](https://github.com/davep/complexitty/pull/9))
9+
310
## v0.2.0
411

512
**Released: 2025-04-22**

src/complexitty/commands/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# Local imports.
55
from .colouring import (
66
SetColourToBluesAndBrowns,
7+
SetColourToBluesAndPinks,
78
SetColourToDefault,
9+
SetColourToRainbow,
810
SetColourToShadesOfBlue,
911
SetColourToShadesOfGreen,
1012
SetColourToShadesOfRed,
@@ -59,7 +61,9 @@
5961
"Quit",
6062
"Reset",
6163
"SetColourToBluesAndBrowns",
64+
"SetColourToBluesAndPinks",
6265
"SetColourToDefault",
66+
"SetColourToRainbow",
6367
"SetColourToShadesOfBlue",
6468
"SetColourToShadesOfGreen",
6569
"SetColourToShadesOfRed",

src/complexitty/commands/colouring.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,20 @@ class SetColourToShadesOfBlue(Command):
4545
ACTION = "set_colour('shades_of_blue')"
4646

4747

48+
##############################################################################
49+
class SetColourToBluesAndPinks(Command):
50+
"""Set the colours to a blue/pink palette"""
51+
52+
BINDING_KEY = "6"
53+
ACTION = "set_colour('blues_and_pinks')"
54+
55+
56+
##############################################################################
57+
class SetColourToRainbow(Command):
58+
"""Set the colours to a rainbow palette"""
59+
60+
BINDING_KEY = "7"
61+
ACTION = "set_colour('rainbow')"
62+
63+
4864
### colouring.py ends here

src/complexitty/mandelbrot/colouring.py

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
##############################################################################
99
# Textual imports.
10-
from textual.color import Color
10+
from textual.color import Color, Gradient
1111

1212
##############################################################################
1313
ColourMap: TypeAlias = Callable[[int, int], Color]
1414
"""Type for a colour map."""
1515

1616

1717
##############################################################################
18-
@lru_cache()
18+
@lru_cache
1919
def default_map(value: int, max_iteration: int) -> Color:
2020
"""Calculate a colour for an escape value.
2121
@@ -69,7 +69,7 @@ def blue_brown_map(value: int, _: int) -> Color:
6969

7070

7171
##############################################################################
72-
@lru_cache()
72+
@lru_cache
7373
def shades_of_red(value: int, _: int) -> Color:
7474
"""Calculate a colour for an escape value.
7575
@@ -87,7 +87,7 @@ def shades_of_red(value: int, _: int) -> Color:
8787

8888

8989
##############################################################################
90-
@lru_cache()
90+
@lru_cache
9191
def shades_of_green(value: int, _: int) -> Color:
9292
"""Calculate a colour for an escape value.
9393
@@ -105,7 +105,7 @@ def shades_of_green(value: int, _: int) -> Color:
105105

106106

107107
##############################################################################
108-
@lru_cache()
108+
@lru_cache
109109
def shades_of_blue(value: int, _: int) -> Color:
110110
"""Calculate a colour for an escape value.
111111
@@ -118,13 +118,70 @@ def shades_of_blue(value: int, _: int) -> Color:
118118
return BLUES[value % 16]
119119

120120

121+
##############################################################################
122+
@lru_cache
123+
def blues_and_pinks(value: int, max_iteration: int) -> Color:
124+
"""Calculate a colour for an escape value.
125+
126+
Args:
127+
value: An escape value from a Mandelbrot set.
128+
129+
Returns:
130+
The colour to plot the point with.
131+
"""
132+
return (
133+
Gradient(
134+
(0, Color(245, 169, 184)),
135+
(0.125, Color(91, 206, 250)),
136+
(0.25, Color(245, 169, 184)),
137+
(0.375, Color(91, 206, 250)),
138+
(0.5, Color(245, 169, 184)),
139+
(0.625, Color(91, 206, 250)),
140+
(0.75, Color(245, 169, 184)),
141+
(0.875, Color(91, 206, 250)),
142+
(1, Color(245, 169, 184)),
143+
quality=max_iteration,
144+
).get_color((1 / max_iteration) * value)
145+
if value
146+
else Color(0, 0, 0)
147+
)
148+
149+
150+
##############################################################################
151+
@lru_cache
152+
def rainbow(value: int, max_iteration: int) -> Color:
153+
"""Calculate a colour for an escape value.
154+
155+
Args:
156+
value: An escape value from a Mandelbrot set.
157+
158+
Returns:
159+
The colour to plot the point with.
160+
"""
161+
return (
162+
Gradient(
163+
(0, Color(288, 3, 3)),
164+
(0.2, Color(255, 140, 0)),
165+
(0.4, Color(255, 237, 0)),
166+
(0.6, Color(0, 128, 38)),
167+
(0.8, Color(0, 76, 255)),
168+
(1, Color(115, 41, 130)),
169+
quality=max_iteration,
170+
).get_color((1 / max_iteration) * value)
171+
if value
172+
else Color(0, 0, 0)
173+
)
174+
175+
121176
##############################################################################
122177
COLOUR_MAPS: Final[dict[str, ColourMap]] = {
123178
"blue_brown_map": blue_brown_map,
124179
"default_map": default_map,
125180
"shades_of_blue": shades_of_blue,
126181
"shades_of_green": shades_of_green,
127182
"shades_of_red": shades_of_red,
183+
"blues_and_pinks": blues_and_pinks,
184+
"rainbow": rainbow,
128185
}
129186
"""Name to colour map function map."""
130187

src/complexitty/providers/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
Quit,
3232
Reset,
3333
SetColourToBluesAndBrowns,
34+
SetColourToBluesAndPinks,
3435
SetColourToDefault,
36+
SetColourToRainbow,
3537
SetColourToShadesOfBlue,
3638
SetColourToShadesOfGreen,
3739
SetColourToShadesOfRed,
@@ -74,7 +76,9 @@ def commands(self) -> CommandHits:
7476
yield Quit()
7577
yield Reset()
7678
yield SetColourToBluesAndBrowns()
79+
yield SetColourToBluesAndPinks()
7780
yield SetColourToDefault()
81+
yield SetColourToRainbow()
7882
yield SetColourToShadesOfBlue()
7983
yield SetColourToShadesOfGreen()
8084
yield SetColourToShadesOfRed()

src/complexitty/screens/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
Quit,
4141
Reset,
4242
SetColourToBluesAndBrowns,
43+
SetColourToBluesAndPinks,
4344
SetColourToDefault,
45+
SetColourToRainbow,
4446
SetColourToShadesOfBlue,
4547
SetColourToShadesOfGreen,
4648
SetColourToShadesOfRed,
@@ -90,7 +92,9 @@ class Main(EnhancedScreen[None]):
9092
MoveUpSlowly,
9193
Reset,
9294
SetColourToBluesAndBrowns,
95+
SetColourToBluesAndPinks,
9396
SetColourToDefault,
97+
SetColourToRainbow,
9498
SetColourToShadesOfBlue,
9599
SetColourToShadesOfGreen,
96100
SetColourToShadesOfRed,

0 commit comments

Comments
 (0)